2012-01-13 113 views
2

场景:我从我的应用程序主页面开始。我导航到子页面A,更改值,点击后退按钮,主页面中的绑定TextBlock不会更改。如果我导航到子页面B,则使用相同绑定的TextBlock会发生变化。同样,如果我再次访问页面A,我会看到更改后的值。如果我退出应用程序,新的值显示在主页面上。只是在使用后退按钮时,刷新不会被触发。使用MVVM-Light刷新导航返回导航

我已经得到了我所有的INotifyPropertyChanged工作。就像我所说的那样,除了导航返回到主页面外,绑定还可以在每种场景中使用。如何发送消息或以其他方式触发该页面上绑定的刷新?谢谢!

编辑:

基于从willmel接受的答案,这里是我所做的:

我MainPage.xaml中的文件有这个标记:

<TextBlock Text="{Binding Title, Mode=OneWay}" /> 

我MainViewModel.cs文件有:

 public string Title 
    { 
     get { return ProfileModel.Instance.DescriptionProfile.Title; } 
    } 

An d我已将此添加到MainViewModel构造:

Messenger.Default.Register<PropertyChangedMessage<string>>(this, 
     (action) => DispatcherHelper.CheckBeginInvokeOnUI(
     () => RaisePropertyChanged("Title"))); 

另一种观点认为,我有以下的标记:

<TextBox Grid.Row="1" Width="250" Height="100" Text="{Binding TitleEdit, Mode=TwoWay}" /> 

在其视图模型获取/设置字符串时,我使用这个:

 public string TitleEdit 
    { 
     get { return ProfileModel.Instance.DescriptionProfile.Title; } 

     set 
     { 
      if (ProfileModel.Instance.DescriptionProfile.Title == value) return; 

      string oldValue = ProfileModel.Instance.DescriptionProfile.Title; 


      ProfileModel.Instance.DescriptionProfile.Title = value; 

      RaisePropertyChanged("Title", oldValue, value, true); 
     } 
    } 

回答

2

在您的视图模型中,如果子页面更改属性,则希望对其进行修改。 (这里需要注意,该属性的类型是布尔的,但可以是任何东西)

Messenger.Default.Register<PropertyChangedMessage<bool>>(this, 
    (action) => DispatcherHelper.CheckBeginInvokeOnUI(
    () => 
     { 
     MessageBox.Show(action.newValue.ToString()); 
     //do what you want here (i.e. RaisePropertyChanged on a value they share) 
    })); 

当你在子类中使用RaisePropertyChanged,使用广播超载。

RaisePropertyChanged("Preference", oldValue, value, true); 

最后,请注意使用DispatcherHelper,你需要添加以下到您App构造(App.xaml.cs

DispatcherHelper.Initialize(); 
+0

谢谢!基于此做出更改后,我能够做到我需要的东西。我正在修改我的OP,以显示基于此的一些细节。 – Stonetip 2012-01-14 02:13:00