2013-02-24 99 views
0

我是建立基于位置的服务Windows Phone应用程序,这是我的第一个应用程序。通过使用MVVM Light,我在页面导航中遇到了一些困难。我正在关注Jesse Liberty Tutorial,到目前为止,当我单击FirstPage中ListBox上的项目时,它可以导航到SecondPage。列表框导航页面Windows Phone中的MVVM灯

我想要做的是,用户从ListBox中选择的FirstPage与我的ListPicker中的SecondPage绑定。因此,用户可以轻松地从SecondPage更改他们想要搜索的内容。

enter image description here

MainPage.xaml中

<ListBox x:Name="MainMenu" ItemTemplate="{StaticResource MainMenu}" ItemsSource="{Binding Categories}" Margin="0,97,0,0"> 
    <Custom:Interaction.Triggers> 
     <Custom:EventTrigger EventName="SelectionChanged"> 
      <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding MainMenuCommand, Mode=OneWay}"/> 
     </Custom:EventTrigger> 
    </Custom:Interaction.Triggers> 
</ListBox> 

MainPage.xaml.cs中

public MainPage() 
{ 
    InitializeComponent(); 

    Messenger.Default.Register<CategoryModel>(this,c => NavigationService.Navigate(new Uri("/Views/VenueList.xaml", UriKind.Relative))); 
} 

MainViewModel.cs

public MainViewModel() 
{ 
    MainMenuCommand = new RelayCommand<CategoryModel>((msg) => GoToVenueList(msg)); 
} 

public RelayCommand<CategoryModel> MainMenuCommand 
{ 
    get; 
    private set; 
} 

private void GoToVenueList(CategoryModel msg) 
{ 
    Messenger.Default.Send(msg); 
} 

private CategoryModel _selectedItem; 
public CategoryModel SelectedItem 
{ 
    get { return _selectedItem; } 
    set 
    { 
     if (_selectedItem == value) 
     { 
      return; 
     } 

     var oldValue = _selectedItem; 
     _selectedItem = value; 

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

VenueList.xaml

<toolkit:ListPicker Margin="0,153,0,0" Background="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" 
          SelectedItem="{Binding Item, Mode=TwoWay}" 
          ItemsSource="{Binding Categories}" 
          ItemTemplate="{StaticResource CategorySelector}" FullModeHeader="Category" FullModeItemTemplate="{StaticResource FullCategorySelector}" BorderBrush="{StaticResource PhoneAccentBrush}" /> 

希望有人可以帮助我的问题。

VenueListViewModel

public VenueListViewModel() 
{ 
    Messenger.Default.Register<PropertyChangedMessage<CategoryModel>>(
     this, 
     (action) => Item = action.NewValue 
    ); 
} 

private CategoryModel _item; 
public CategoryModel Item 
{ 
    get 
    { 
     return _item; 
    } 
    set 
    { 
     if (_item == value) 
     { 
      return; 
     } 

     _item = value; 

     // Update bindings, no broadcast 
     RaisePropertyChanged("Item"); 
    } 
} 

回答

1

这就是你需要两个的ViewModels海誓山盟通信的典型案例。

在这种情况下,最好的办法,特别是因为您使用mvvm-light是使用该框架的功能Messaging

如果您需要一些帮助,您可以在网上找到大量的示例和文档(查找Laurent Bugnion在channel9上的网络广播,例如this one),它实际上只是注册一个回调某些类型的消息,然后从其他地方发送它们。

这样,你的第一个viewmodel发送消息给你的第二个viewmodel,说明选择了哪个列表项。您的目标视图模型会自行更新以反映这一点。然后,您的初始viewmodel命令导航到目标页面,并且它使用目标视图模型它运作良好。

0

有几种可能性,我主要使用列表框的SelectedItem解决方案......但纯粹的消息传递也是可能的。

看看与解决方案here on stack overflow

+0

类似的问题,我也这样做的解决方案,但得到一个错误说“的SelectedItem必须有效的价值。”在我的详细页面构造函数中:'Messenger.Default.Register >(this,(action)=> Item = action.NewValue);'我更新了我的问题,你能告诉我我做错了什么吗? – yudayyy 2013-02-26 11:18:34

+0

很难从代码中看到(一个测试proj会帮助:))但我总是遵循这个例子:[http://blogs.xamlninja.com/xaml/wp7-contrib-the-last-messenger](http: //blogs.xamlninja。com/xaml/wp7-contrib-the-last-messenger)从列表转到详细信息页面并将选定的项目绑定到某个 – Depechie 2013-02-26 12:22:48