これがよくある質問であることは知っていますが、少なくとも 1 週間は解決しようとしています...非常に多くのスレッドを読み、何百万もの異なる MVVM-Pattern-Examples などをダウンロードしました...
MVVMモデルビューの最初のアプローチで愚かなラベルを更新したいだけです:
void StartUpProcess_DoWork(object sender, DoWorkEventArgs e)
{
SplashWindow splash = new SplashWindow();
var ViewModel_Splash = new VM_SplashWindow();
splash.DataContext = ViewModel_Splash;
splash.Topmost = true;
splash.Show();
ViewModel_Splash.DoWork();
}
完全な ViewModel:
public class VM_SplashWindow:VM_Base
{
#region Properties
private string _TextMessage;
public string TextMessage
{
get
{
return _TextMessage;
}
set
{
if(_TextMessage != value)
{
_TextMessage = value;
base.OnPropertyChanged("TextMessage");
}
}
}
#endregion
#region Methods
public void DoWork()
{
this.TextMessage = "Initialize";
for(int aa = 0; aa < 1000; aa++)
{
this.TextMessage = "Load Modul: " + aa.ToString();
Thread.Sleep(5);
}
this.TextMessage = "Done";
Thread.Sleep(1000);
}
#endregion
}
ベースからの小さなピース:
public abstract class VM_Base:INotifyPropertyChanged, IDisposable
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
#endregion
}
そして最後にビュー:
<Label Height="28" Margin="19,0,17,15" Name="label2" VerticalAlignment="Bottom"
Content="{Binding Path=TextMessage}" Foreground="White" />
ビューモデルのコンストラクターで TextMessage プロパティの初期値を設定すると、この初期値は、splash.Show() コマンドの後に表示されます。
DoWork-Method で TextMessage プロパティを設定すると onPropertyChangedEvent が発生しますが、残念ながらウィンドウのラベルは更新されません。どうすればいいのかわからない… 助けてくれるのを本当に楽しみにしています。よろしくお願いします!
おそらく、StartUpProcess_DoWork は独自の STAThread で実行されていることに言及する必要があります
よろしく、フロー