문제
저는 Prism을 처음 접했고 Elementhost 내에서 Prisim Control을 주최하려고합니다. 나는 매우 기본적인 것을 놓치고있는 것 같습니다. 요소 호스트가 포함 된 단일 윈 포름이 있습니다. 다음 코드는 양식입니다.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
var child = bootstrapper.Container.Resolve<Shell>();
elementHost.Child = child;
}
부트 스트랩퍼는 레지브레이션을 처리합니다
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을 포함하는 Usercontrol입니다.
<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 코드를 깊이 평가하고있는 이유를 알아 내려고 노력하고 있습니다. 내가 기본적인 것을 놓치고 있습니까?
해결책
그 이유는 프리즘 의이 코드입니다.
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이기 때문입니다!
해결책:
- System.windows.application에서 상속되는 빈 클래스를 만듭니다. (이름은 중요하지 않음) :
플러그인 입력 시점에서 다음 코드를 실행합니다.
public class MyApp : System.Windows.Application
{
}
if (System.Windows.Application.Current == null)
{
// create the Application object
new MyApp();
}
이것이 바로 - 이제 당신은 null이 아닌 application.current를 가지고 있으며 (응용 프로그램) typeof (Application)와 같지 않습니다.
다른 팁
위의 @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);
}
}
}
그리고 분명히, 나는 쉘이 포함 된 WPF Prism 응용 프로그램에 클래스 아래를 추가했습니다.
public class MyApp : System.Windows.Application
{
}
편집 :로드 메소드 핸들러 (양식)는 속성 창에서 양식을 마우스 오른쪽 클릭하여 이벤트로 이동하여 이벤트로 이동하여 작성해야합니다. 로드 이벤트 핸들러 복사 및 붙여 넣기는 작동하지 않습니다.