views:

318

answers:

1

Here's the scenario:

A Silverlight app (just a wireframe) that is hosted on an html page (no aspx/server side solution, just a client side html page loaded from c:\somefolder\mysilverlight.html), actually it is hosted in a WPF app using WebBrowser control.

The WPF app needs to inject Silverlight xaml in to the Silverlight wireframe dynamically during runtime. So that the page gets rendered.

The silverlight xaml being pumped in to the Wireframe is a xaml string that has event handlers and custom classes. So I can't use XamlReader.Load.

The InitializeFromXaml was removed in Silverlight 3 so I can't use that either.

Any other alternatives?

Update: I am trying to build a lightweight Xaml Editor with quick preview, and I do need to load it from a xaml string. Application.LoadComponent works only for compiled SL assemblies

+1  A: 

InitializeComponent was replaced by Application.LoadComponent. You can invoke the method like this:

System.Windows.Application.LoadComponent(this, new
 System.Uri("/SL_40308_VS;component/MainPage.xaml", System.UriKind.Relative));

Have a look at the FooPage.g.cs generated SilverlightProject/obj/debug in Silverlight projects for a full sample.

Just calling out, Silverlight cannot compile C# code at runtime.
Your options are:

  1. Move your code behind to a dynamic language that Silverlight can interpret at runtime. Using the DLR you can have Silverlight code-behind in IronPython or IronRuby. The DLR allows you to dynamically load IronPython/IronRuby code and assosciate it with codebehind. Checkout this sample by Jimmy Schmenti @ http://is.gd/4Lj1G

  2. Compile the Silverlight code on the full desktop CLR using System.CompilerServices. Nokola has an excellent sample of server-side compiling that you can easily convert to WPF.

  3. Remove code references from dynamically loaded XAML. It sounds like a very unique technical solution and maybe it's the wrong one. What are you trying to do that warrants such an extreme approach?

Sincerely,
--- Justin Angel

JustinAngel
Thanks for your comments, I am trying to build a lightweight Xaml Editor with quick preview, and I do need to load it from a string. Application.LoadComponent works only for compiled SL assemblies
Vin
BTW, I looked at your codeproject article, and wanted to thank you for posting it. Awesome stuff there. http://www.codeproject.com/KB/silverlight/PathToReusableXAML.aspx
Vin