Xamarin形成棱镜 - 从网格导航到内容页面。页面未显示
问题描述:
在ContentPage中,我有一个ListView中的“房间”列表。当我点击其中一个时,它应该导航到另一个ContentPage与房间的细节。新页面的ViewModel上的OnNavigatedTo()被调用(显然没有错误),但该页面未显示。它留在房间列表的页面上。Xamarin形成棱镜 - 从网格导航到内容页面。页面未显示
<?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>
这里的视图模型的一个片段:
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");
}
细节我与Visual Studio 2015年
这里列表页面的XAML与Android(仿真器和手机)进行测试页:
<?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>
视图模型的一个片段:
public void OnNavigatedTo(NavigationParameters parameters)
{
// on Debug I enter in this method!
}
答
确保您在App.cs/App.xaml.cs文件中注册要导航的页面。
它已注册。如果在OnInitialized()我把:NavigationService.NavigateAsync(“RoomDetail”);该页面出现。 – Alberto
然后,RoomsList页面可能有问题。尝试将TapGesture放置在ListView上而不是StackPanel上。 –
也许你是对的。如果我把眼前这个列表页: TapGestureRecognizer> StackLayout> 它正在工作。 你知道另一种链接列表视图元素的命令吗?我知道唯一的选择是使用丑陋的事件处理程序,但这会破坏MVVM。谢谢! (Prism很棒!) –
Alberto