我是新来的棱镜和我试图主办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;

    }

引导程序处理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只不过是一个标签更在这一点上。

Shell.xaml是包含以下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));
    }
}

我已经深深追查到棱镜代码试图找出为什么观点是从来没有进入该地区。我失去了一些基本的东西?

有帮助吗?

解决方案

原因是这样的代码在棱镜:

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!

在解决方案:

  1. 创建一个空类将从System.Windows.Application继承。 (名称并不重要):
  2. 目前进入点到一个插件执行以下代码:

    public class MyApp : System.Windows.Application
    {
    }
    
    if (System.Windows.Application.Current == null)
    {
        // create the Application object
        new MyApp();
    }
    

    这是它 - 现在你有一个Application.Current非空,这不是等于typeof运算(应用程序)

其他提示

@马克林德尔以上为我工作。的唯一的东西我不得不改变如下。

我的引导程序

 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);
        }
    }   


        }

和仅仅是明确的,我添加下面类中含有的壳WPF应用PRISM

public class MyApp : System.Windows.Application
{
}
  

编辑:请注意,(的形式)Load方法处理程序必须通过创建   rightclicking形式,在属性窗口,进入事件和双   clicl负载。复制和粘贴负载事件处理程序不工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top