如何在ElementHost中使用Prism [英] How to use Prism within an ElementHost
问题描述
我是Prism的新手,我试图在ElementHost中托管Prisim控件.我似乎缺少一些非常基本的东西.我有一个包含ElementHost的WinForm.以下代码的格式为:
I'm new to Prism and I am attempting to host a Prisim control within an ElementHost. I seem to be missing something very basic. I have a single WinForm that contains an ElementHost. The following code is in the form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
var child = bootstrapper.Container.Resolve<Shell>();
elementHost.Child = child;
}
BootStrapper处理注册
The BootStrapper handles regisration
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Container.RegisterType<MyView>();
var shell = Container.Resolve<Shell>();
return shell;
}
protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(MyModule));
return catalog;
}
}
此时MyView.xaml仅仅是一个标签.
The MyView.xaml is nothing more than a label at this point.
Shell.xaml是一个包含以下XAML的UserControl:
Shell.xaml is a UserControl that contains the following XAML:
<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
模块代码最少:
public class MyModule : IModule
{
private readonly IRegionViewRegistry _regionViewRegistry;
public MyModule(IRegionViewRegistry registry)
{
_regionViewRegistry = registry;
}
public void Initialize()
{
_regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(MyView));
}
}
我一直在深入探究Prism代码,试图弄清楚为什么视图从未设置到该区域中.我缺少基本的东西吗?
I've been tracing deep into the Prism code trying to figure out why the View is never set into the region. Am I missing something basic?
推荐答案
原因是Prism中的以下代码:
The reason is this code in Prism:
private static bool RegionManager::IsInDesignMode(DependencyObject element)
{
// Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
|| Application.Current.GetType() == typeof(Application);
}
原因是对于非WPF应用程序,Application.Current为NULL!
The reason is that for the non-WPF application the Application.Current is NULL!
解决方案:
- 创建一个将从System.Windows.Application继承的空类. (名称无关紧要):
在插入插件时,执行以下代码:
At the point of entry to a plug-in execute the following code:
public class MyApp : System.Windows.Application
{
}
if (System.Windows.Application.Current == null)
{
// create the Application object
new MyApp();
}
就这样–现在您有了一个Application.Current不为null,并且不等于typeof(Application).
This is it – now you have an Application.Current that is not null and it’s not equal to typeof(Application).
这篇关于如何在ElementHost中使用Prism的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!