我是建立基于位置的服务Windows Phone应用程序,这是我的第一个应用程序。通过使用MVVM Light,我在页面导航中遇到了一些困难。我正在关注Jesse Liberty Tutorial,到目前为止,当我单击FirstPage中ListBox上的项目时,它可以导航到SecondPage。列表框导航页面Windows Phone中的MVVM灯
我想要做的是,用户从ListBox
中选择的FirstPage
与我的ListPicker
中的SecondPage
绑定。因此,用户可以轻松地从SecondPage
更改他们想要搜索的内容。
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");
}
}
类似的问题,我也这样做的解决方案,但得到一个错误说“的SelectedItem必须有效的价值。”在我的详细页面构造函数中:'Messenger.Default.Register>(this,(action)=> Item = action.NewValue);'我更新了我的问题,你能告诉我我做错了什么吗? –
yudayyy
2013-02-26 11:18:34
很难从代码中看到(一个测试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