2013-03-10 22 views
0

我想问你,你认为,从第二页到第一页的最佳方法是什么?我用的是这样的(MVVM)从第二个子页面获取数据到第一页 - Windows Phone(最佳方法)

第二页:

public partial class AddProfilePageView : PhoneApplicationPage 
{ 
    public AddProfilePageView() 
    { 
     InitializeComponent(); 
     DataContext = new AddProfileViewModel(); 
    } 

    public AddProfileViewModel ViewModel { get { return DataContext as AddProfileViewModel; } }} 

第一页:

public partial class ProfilesPageView : PhoneApplicationPage 
{ 

    public ProfilesPageView() 
    { 
     InitializeComponent(); 
     DataContext = new ProfilesViewModel(); 
    } 

    public ProfilesViewModel ViewModel 
    { 
     get { return DataContext as ProfilesViewModel; } 
    }} 

AddProfileViewModel()类有性能,被绑定的控件在XAML。从这个页面我需要获取数据到第一页ProfilesPageView。

我的解决办法是:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     var content = e.Content as ProfilesPageView; 
     if (content != null && ViewModel.IsOk) 
     { 
       content.ViewModel.ProfilesList.Add(ViewModel.ProfileRecord); 

     } 
    } 

所以,你有什么感想?如何获取数据是不错的解决方案? 谢谢

回答

0

实际上,在这里你不是试图从一个页面获取数据到另一个页面。
在第二个/详细信息页面上添加一个新项目,然后当您返回到第一个/主页面时,您希望它更新显示的数据以显示新项目。

我会假设AddProfileViewModelProfilesViewModel都坚持磁盘/ IsolatedStorage,所以你可以刷新主/列表/首页时返回到它。

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    // ... 

    if (e.NavigationMode == NavigationMode.Back) 
    { 
     (this.DataContext as ProfilesViewModel).Refresh(); 
    } 
} 
相关问题