私はWPFアプリケーションでPrism 6とUnityのDIを使用してビューモデルを解決しようとしています。しかし、どのビューをどのビューモデルとマージするべきかをフレームワークに伝える方法はわかりません。Prism 6 with Unity - 名前解決規約なしのビューのビューモデルを解決する
ViewModelsとViewsの名前空間、クラスViewAとViewAViewModelを使用すると、すべての機能が動作しますが、クラスの名前と構成に柔軟性を持たせたいと思っています。フレームワークはどのビューがどのビューモデルに関係するかを明示的に示します。私は多くのことを試みましたが、実際には何も動作しません。現在の「解決策」はアプリを実行させますが、ビューモデルは設定されません。
ここコードである:
ViewA.xaml
<UserControl x:Class="WPFDITest.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<TextBlock Text="{Binding ViewAMessage}"/>
<TextBox Text="{Binding ViewAMessage, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</UserControl>
MainWindow.xaml
<UserControl x:Class="WPFDITest.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<TextBlock Text="{Binding ViewAMessage}"/>
<TextBox Text="{Binding ViewAMessage, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</UserControl>
ViewAVM.cs
public class ViewAVM : BindableBase
{
private string viewAMessage;
public ViewAVM(IModelA model)
{
viewAMessage = model.HelloMsgA();
}
public string ViewAMessage
{
get { return viewAMessage; }
set { SetProperty(ref viewAMessage, value); }
}
}
Model.cs
ここpublic interface IModelA
{
string HelloMsgA();
}
public class ModelA : IModelA
{
public string HelloMsgA()
{
return "Hello from A!";
}
}
App.xaml.cs
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bootstraper = new Bootstrapper();
bootstraper.Run();
}
}
Bootstrapper.cs
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<IModelA, ModelA>(new ContainerControlledLifetimeManager());
Container.RegisterType<object, ViewAVM>("ViewA");
}
protected override void ConfigureViewModelLocator()
{
ViewModelLocationProvider.SetDefaultViewModelFactory(type => Container.Resolve(type));
}
}
私はそのポストとはるかに多くのものを見ましたが、まだ、私は慣習を望んでいません。私はあなたのアプローチも試みましたが、 'ViewA'が' ViewAVM'をパラメータとして 'DataContext'を設定すると、' InitializeComponent'メソッドの 'MainWindow'コンストラクタで' NullReferenceException'が発生するという問題があります。 –
このレポをクローンすることができますhttps://github.com/lukaszwawrzyk/wpf-prism-di-test.gitと参照してください。 –
私はGitをインストールしていません。これを私がやるようにしたいのであれば、モジュールとブートストラップのCreateModuleCatalogのオーバーライドが必要です。 CreateModuleCatalogは、IModuleインターフェイスを実装するモジュールを登録する場所です。モジュールのInitializeメソッドで、ビューをコンテナに登録します。コンテナはViewにViewModelが必要であると判断し、ViewModelを提供します。これは、あなたが望む、または必要以上のものかもしれません。あなたはDataContext = new ViewAVM();と一緒に行くことができます。あなたはGitHubのPrismLibraryの例を見ましたか? –