我是Prism的新手,我正在尝试在ElementHost中托管Prisim控件。我似乎遗漏了一些非常基本的东西。我有一个包含ElementHost的WinForm。表单中的代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
var child = bootstrapper.Container.Resolve<Shell>();
elementHost.Child = child;
}BootStrapper处理正则化
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只不过是一个标签。
Shell.xaml是一个包含以下UserControl的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代码,试图弄清楚为什么视图从未被设置到区域中。我是不是漏掉了什么基本的东西?
发布于 2009-09-01 16:25:48
原因是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为空!
解决方案:
在插件的入口点执行以下代码:
public class MyApp : System.Windows.Application
{
}
if (System.Windows.Application.Current == null)
{
// create the Application object
new MyApp();
}这就是它-现在你有一个不为空的Application.Current,它不等于typeof(应用程序)。
发布于 2014-02-05 02:29:38
上面的@Mark Lindell为我工作。我唯一需要修改的是下面的内容。
我的引导程序
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return this.Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
if (System.Windows.Application.Current == null)
{
// create the Application object
new HelloWorld.Myapp();
}
//App.Current.MainWindow = (Window)this.Shell;
//App.Current.MainWindow.Show();
//MainWindow = (Window)this.Shell;
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
}和我的窗体类
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Create the ElementHost control for hosting the WPF UserControl
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run(true);
//var uc = bootstrapper.Container.Resolve<Shell>(); This line threw error
//Create the WPF UserControl.
HelloWorld.Shell uc = new HelloWorld.Shell();
//Assign the WPF UserControl to the ElementHost control's Child property.
host.Child = uc;
//Add the ElementHost control to the form's collection of child controls.
this.Controls.Add(host);
}
}
}为了清楚起见,我在包含Shell的WPF PRISM应用程序中添加了下面的类。
public class MyApp : System.Windows.Application
{
}编辑:请注意,必须通过右键单击form来创建Load方法处理程序(of form),在属性窗口中,转到events并双击Load。复制和粘贴加载事件处理程序不起作用。
https://stackoverflow.com/questions/1363369
复制相似问题