2012-02-05 44 views
0

I'm在WP7工作application.-嵌套MVVM与定位

摘要
我要显示一个页面(1)好友列表,如果有朋友被窃听i'll秀一个页面(2)与这个朋友信息加上这个朋友看过的电影列表。然后,如果我点击一个电影,我将获得一个带有电影信息的页面(3)。我正尝试在MVVM Light Toolkit中使用MVVM模式。

服务
在每一页上,我需要打个电话给API
第1页:/friend返回好友列表
第2页:/friend/5436返回此观看电影朋友
第3页:/movie/87968返回关于电影

我做什么
信息10我创建具有电影的的ObservableCollection

public class Friend: ObservableObject 
{ 
    public Friend(){} 
    public ObservableCollection<Movie> Movies{ get; set; } 
} 

还要创建一个电影类电影的资讯朋友类。

另外一个视图模型来包装朋友类

public class FriendViewModel : ViewModelBase 
{ 
    public Friend Model 
    { 
     get; 
     private set; 
    } 

    public FriendViewModel(Friend model) 
    { 
     Model = model 
    } 
} 

然后一个视图模型让所有的朋友

public class FriendListViewModel : ViewModelBase 
{ 

    // To store the Friend service returned by the locator 
    private readonly IFriendService _friendService; 

    // To store the friends. It should be bind the the listbox in the view 
    public ObservableCollection<FriendViewModel> Friends 
    { 
     get; 
     private set; 
    } 

    /// <summary> 
    /// Initializes a new instance of the FriendListViewModel class. 
    /// </summary> 
    public FriendListViewModel(IFriendService friendService) 
    { 
     // The data service 
     _friendService = friendService; 

     // Get all the friends and add them to the Friends collection 
     Friends = new ObservableCollection<FriendViewModel>(); 
     _friendService.GetFriends((result, error) => 
     { 
      if (error != null) 
      { 
       MessageBox.Show(error.Message); 
       return; 
      } 

      if (result == null) 
      { 
       MessageBox.Show("Nothing found"); 
       return; 
      } 

      foreach (var friend in friends) 
      { 
       Friends.Add(new FriendViewModel(friend)); 
      } 

     }); 
    } 
} 

每一件事的工作与定位是在App静态资源。 xaml和在视图中使用的我列出朋友作为视图的datacontext
这是定位器

public class FriendViewModelLocator 
{ 
    static FriendViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     if (ViewModelBase.IsInDesignModeStatic) 
     { 
      SimpleIoc.Default.Register<IFriendService, Design.DesignFriendDataService>(); 

     } 
     else 
     { 
      SimpleIoc.Default.Register<IFriendService, FriendService>(); 
     } 

     SimpleIoc.Default.Register<FriendListViewModel>(); 
    } 

    /// <summary> 
    /// Gets the Main property. 
    /// </summary> 
    public FriendListViewModel FriendList 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<FriendListViewModel>(); 
     } 
    } 
} 

问题
第一页工作,我得到的朋友列表,我可以显示它们。第一页也是可混合的。但是我不知道如何继续下一页。我以为我应该通过相同的FriendViewModel我点击页面2并调用api来获取电影列表。但是在某些方面,它被朋友模式抛弃,我无法填写Friend上的Movies集合。我应该为每个页面创建不同的视图模型包装器。如何配置定位器能够获得电影和电影信息的页面3.

任何想法,想法,资源检查?

回答

0

我会做到以下几点:

  • 你有没有查看模型作为应用程序的性能,使其在应用程序的每一个页面访问。
  • 使您的视图模型反映数据的结构,即PersonViewModelMovieViewModels的集合具有关系,该集合然后返回电影信息。
  • 在页面之间导航时,传递查询字符串,该查询字符串允许下一页在查看模型中查找应该用于DataContext的项目。例如,点击电影时,导航至MovieView.xaml?id=567,其中567标识电影。然后您可以在NavigatedTo方法中找到该影片,并将其设置为DataContext

这种模式也将帮助您理解您的应用程序,在我的博文中阅读更多A Simple WP7 MVVM tombstoning example

+0

我读了yout blogpost,我不太明白模型如何在_“场景”_中扮演它的角色,我有这样的想法,即你没有模型,但模式是MVVM .. – blackjid 2012-02-05 14:19:48

+0

The视图模型与您的服务层交互以检索模型实例。无论如何,视图和视图模型交互都是相同的。 – ColinE 2012-02-05 14:28:50

+0

@blackjid在博客文章中,我链接到模型是从Twitter搜索返回的XML数据。 – ColinE 2012-02-05 14:42:14