Frage

Ich bin neu in Prism und ich bin versucht, eine prisim Kontrolle innerhalb eines Elements Host. Ich scheine etwas sehr einfach zu fehlen. Ich habe einen einzigen WinForm, die ein Element enthält. Der folgende Code ist in der Form:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Bootstrapper bootstrapper = new Bootstrapper();
        bootstrapper.Run();

        var child = bootstrapper.Container.Resolve<Shell>();
        elementHost.Child = child;

    }

Die Bootstrap Griffe 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;
    }
}

Die MyView.xaml ist nichts anderes als ein Etikett an dieser Stelle.

Shell.xaml ist ein Benutzersteuerelement, das den folgenden XAML enthält:

<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />

Das Modul Code ist minimal:

public class MyModule : IModule
{
    private readonly IRegionViewRegistry _regionViewRegistry;

    public MyModule(IRegionViewRegistry registry)
    {
        _regionViewRegistry = registry;   
    }

    public void Initialize()
    {
        _regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(MyView));
    }
}

Ich habe tief in den Prism-Code wurde verfolgt, um herauszufinden, warum die Ansicht ist, niemals in die Region gesetzt. Bin ich etwas Grundsätzliches fehlt?

War es hilfreich?

Lösung

Der Grund ist dieser 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);
}

Der Grund dafür ist, dass für die nicht-WPF-Anwendung der Application.Current NULL ist!

Die Lösung:

  1. Erstellen Sie eine leere Klasse, die von System.Windows.Application erben. (Name spielt keine Rolle):

An der Eintrittsstelle zu einem Plug-in ausführen den folgenden Code:

public class MyApp : System.Windows.Application
{
}

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

Das ist es - jetzt haben Sie eine Application.Current, die nicht null ist und es ist gleich nicht typeof (Anwendung)

.

Andere Tipps

@ Mark Lindell oben für mich gearbeitet. Die einzigen Dinge, die ich ändern musste sind unten.

Mein Bootstrap-Programm

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

und meine Form Klasse

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


        }

Und nur klar zu sein, habe ich unten Klasse in der Anwendung WPF PRISM enthält Shell.

public class MyApp : System.Windows.Application
{
}
  

Edit: Beachten Sie, dass die Methode Load-Handler (die Form) erstellt werden muss durch   Rechtsklick Form, im Eigenschaftenfenster, gehen Sie zu Veranstaltungen und Doppel   clicl laden. Kopieren und Einfügen von Load-Ereignishandler nicht funktioniert.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top