简体   繁体   中英

Loading xaml with events and custom classes in to Silverlight frame hosted in html, during runtime

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

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. Check out this sample by Jimmy Schmenti .

  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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM