我已經實施了最好描述爲一個簡單的模式 -
- 在應用程序的啓動和關閉事件,我將消息發送給訂閱頁面。
- 預訂消息的頁面執行數據的序列化/反序列化。
我正在使用Laurent Bugnion's excellent MVVMLight library for Windows Phone 7。這是圖示出消息廣播一些示例代碼 - 在一個視圖模型類的構造函數
// Ensure that application state is restored appropriately
private void Application_Activated(object sender, ActivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvent>(AppEvent.Activated, string.Empty));
}
// Ensure that required application state is persisted here.
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvent>(AppEvent.Deactivated, string.Empty));
}
,我設置了訂閱的通知消息 -
// Register for application event notifications
Messenger.Default.Register<NotificationMessage<AppEvent>>(this, n =>
{
switch (n.Content)
{
case AppEvent.Deactivated:
// Save state here
break;
case AppEvent.Activate:
// Restore state here
break;
}
}
我發現這種策略,所有的與綁定到ViewModel的頁面相關的數據將被正確保存和恢復。
HTH,indyfromoz
謝謝。這看起來不錯。我也在使用MVVM Light。我會在今天早上發佈這個模式並讓你知道。我對MVVM相當陌生,我還沒有使用消息框架! – NER1808 2010-11-16 09:32:38