MVVM Light などのフレームワークを使用している場合は、コールバックから UI スレッドにメッセージを送信できます (保存に必要なパラメーターを渡します)。
編集。追加例
ほとんどの場合、コード ビハインド ページで、送信するカスタム メッセージのリスナーをセットアップする必要があります。onload または onnavigateto メソッドでこれを行うことをお勧めします。CustomObjectWithParams
コールバックは、db から取得するというカスタム オブジェクトをロードすると仮定します。
あなたのxamlコードビハインドで:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// the "MyViewModel.OpenSaveDialogRequest" can be any string.. it just needs to synch up with what was sent from the viewmodel below
Messenger.Default.Register<CustomObjectWithParams>(this, "MyViewModel.OpenSaveDialogRequest", objParams => ShowSaveDialog(objParams));
base.OnNavigatedTo(e);
}
private void ShowSaveDialog(CustomObjectWithParams obj) {
// do your open save here in the UI thread
}
// be smart, make sure you unregister this listener when you navigate away!
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Messenger.Default.Unregister<CustomObjectWithParams>(this);
base.OnNavigatedFrom(e);
}
ビューモデルのコールバック関数(UIスレッドで実行されていない)で...非同期に取得しているパラメーターを送信したい。これは次のように行うことができます。
protected void your_asynch_callbackfunction(your args) {
CustomObjectWithParams objParams = new CustomObjectWithParams();
// fill up this object here....
// now send the object to the UI to do something with it
Messenger.Default.Send<CustomObjectWithParams>(objParams, "MyViewModel.OpenSaveDialogRequest");
}