Wednesday, July 2, 2008

InitializeFromXaml & XamlReader.Load in Silverlight Beta 2

In this post, we’ll discuss how to resolve two common issues which come while migrating Silverlight Beta 1 Apps to Beta 2. Below I am showing Beta 1 code and its equivalent code in Beta 2
Compile time error1. Does not contain a definition for InitializeFromXaml


//Beta 1
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("MyNamespace.UserControl1.xaml");
FrameworkElement control = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());



//Beta 2
Application.LoadComponent(control, new Uri("
;component/.xaml", UriKind.Relative));

OR


//Beta 2
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("MyNamespace.UserControl1.xaml");
FrameworkElement control = XamlReader.Load(new System.IO.StreamReader(s).ReadToEnd()) as Control;

Please note that <projectname> should be replaced by the VS 2008 project name or assembly dll name. component is a constant word and <classname> should be replaced by your .NET Class name of the User Control

Compile time error
2. No overload for method Load takes 2 arguments


//Beta 1
XamlReader.Load(string,boolean)

This function parses a well-formed XAML fragment and creates a corresponding Silverlight object tree, and returns the root of the object tree.


//Beta 2
XamlReader.Load(string)

No comments: