在ContentPage中,我在ListView中有一个“房间”列表。当我点击其中一个时,它应该导航到另一个ContentPage以及房间的详细信息。调用新页面的ViewModel上的OnNavigatedTo()(显然没有错误),但页面没有显示。它保留在页面中的房间列表。我用Visual Studio 2015用Android(模拟器和手机)测试它。
这里是列表页面的XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:views="clr-namespace:StudentRooms.Views;assembly=StudentRooms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="StudentRooms.Views.RoomsList">
<StackLayout Orientation="Vertical">
<ListView ItemsSource="{Binding Rooms}"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Spacing="10">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer CommandParameter="{Binding Id}"
Command="{Binding BindingContext.RoomSelected, Source={views:RoomsList}}">
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
<Label Text="{Binding Name}" FontAttributes="Bold"/>
<Label Text="{Binding Description}"></Label>
<ProgressBar Progress="{Binding Occupancy}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
这是ViewModel的片段:
public RoomsListViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
RoomSelected = new DelegateCommand<object>(NavigateToSelectedRoom);
}
private void NavigateToSelectedRoom(object id)
{
//var navParams = new NavigationParameters
//{
// { "RoomId", id }
//};
//_navigationService.NavigateAsync("RoomDetail", navParams);
_navigationService.NavigateAsync("RoomDetail");
}
详情页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="StudentRooms.Views.RoomDetail">
<StackLayout>
<Label Text="AAA"></Label>
</StackLayout>
</ContentPage>
ViewModel的片段:
public void OnNavigatedTo(NavigationParameters parameters)
{
// on Debug I enter in this method!
}
答案 0 :(得分:1)
确保在App.cs / App.xaml.cs文件中注册要导航的页面。