このプロジェクト アセンブリ構造を持つプロジェクトがあります。
- トレーニング モデル
- mvvm ライト ビュー モデル/ロケーターを介してモデルにデータ バインドされた XAML フォーム
これはすべてうまくいきます。
負荷イベントで、モデルを効果的に再割り当てします。
ViewModelLocator.Main.Training = new Training();
これは正常に機能し、モデルはまだすべてバインドされており、更新されてフォームに新しいデータが表示されます。
きれいにするために、ViewModel を独自のプロジェクト/アセンブリの外部に移動したいと考えています。つまり、次の構造を実現します。
- トレーニング モデル
- XAML フォーム
- mvvm ライト ビュー モデル/ロケーター
ビューモデルは次のとおりです。
public class MainViewModel : ViewModelBase
{
private Training training;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
this.training = new Training();
}
public Training Training
{
get
{
return this.training;
}
set
{
this.training = value;
}
}
}
ViewModel にはデフォルトのロケーターがあります。
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
}
public static MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
ViewModel を独自のアセンブリに移動してプロジェクトを実行すると、最初の実行時に XAML がモデル インスタンスに正しくバインドされたデータになります。上記のように新しい Training オブジェクトを割り当てると、フォームが更新されません。
mvvw-light ビュー モデルが他のアセンブリにあることだけが異なる場合、何が原因でしょうか?
ありがとう