简体   繁体   English

在运行时将具有事件和自定义类的xaml加载到html托管的Silverlight框架中

[英]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. 托管在html页面上的Silverlight应用程序(仅是线框)(没有aspx /服务器端解决方案,仅是从c:\\ somefolder \\ mysilverlight.html加载的客户端html页面),实际上是使用WPF应用程序托管WebBrowser控件。

The WPF app needs to inject Silverlight xaml in to the Silverlight wireframe dynamically during runtime. WPF应用程序需要在运行时动态地将Silverlight xaml注入Silverlight线框。 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. 被抽入Wireframe的silverlight xaml是一个具有事件处理程序和自定义类的xaml字符串。 So I can't use XamlReader.Load. 所以我不能使用XamlReader.Load。

The InitializeFromXaml was removed in Silverlight 3 so I can't use that either. 在Silverlight 3中删除了InitializeFromXaml,所以我也不能使用它。

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. 更新:我正在尝试使用快速预览构建轻量级的Xaml编辑器,并且确实需要从xaml字符串中加载它。 Application.LoadComponent works only for compiled SL assemblies Application.LoadComponent仅适用于已编译的SL程序集

InitializeComponent was replaced by Application.LoadComponent. InitializeComponent替换为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. 查看Silverlight项目中FooPage.g.cs生成的SilverlightProject / obj / debug,以获取完整示例。

Just calling out, Silverlight cannot compile C# code at runtime. 只是调用,Silverlight无法在运行时编译C#代码。
Your options are: 您的选择是:

  1. Move your code behind to a dynamic language that Silverlight can interpret at runtime. 将您的代码移到Silverlight可以在运行时解释的动态语言中。 Using the DLR you can have Silverlight code-behind in IronPython or IronRuby. 使用DLR,您可以在IronPython或IronRuby中隐藏Silverlight代码。 The DLR allows you to dynamically load IronPython/IronRuby code and assosciate it with codebehind. DLR允许您动态加载IronPython / IronRuby代码并将其与代码隐藏关联。 Check out this sample by Jimmy Schmenti . 查看Jimmy Schmenti的样本

  2. Compile the Silverlight code on the full desktop CLR using System.CompilerServices. 使用System.CompilerServices在完整的桌面CLR上编译Silverlight代码。 Nokola has an excellent sample of server-side compiling that you can easily convert to WPF. Nokola具有出色的服务器端编译示例 ,您可以轻松地将其转换为WPF。

  3. Remove code references from dynamically loaded XAML. 从动态加载的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? 您要做什么才能确保采取这种极端的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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