Xamarin forms Prism - 从网格到内容页面的导航。页面未显示
Xamarin forms Prism - Navigation from grid to content page. Page not shown
在 ContentPage 中,我在 ListView 中有一个 "rooms" 的列表。当我单击其中一个时,它应该导航到另一个包含房间详细信息的 ContentPage。新页面的 ViewModel 上的 OnNavigatedTo() 被调用(显然没有错误),但页面没有显示。它留在房间列表的页面中。我用 Android(模拟器和 phone)和 Visual Studio 2015 测试它。
这里是列表页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!
}
确保在您的应用程序中注册用于导航的页面。cs/App。xaml.cs 文件。
在 ContentPage 中,我在 ListView 中有一个 "rooms" 的列表。当我单击其中一个时,它应该导航到另一个包含房间详细信息的 ContentPage。新页面的 ViewModel 上的 OnNavigatedTo() 被调用(显然没有错误),但页面没有显示。它留在房间列表的页面中。我用 Android(模拟器和 phone)和 Visual Studio 2015 测试它。
这里是列表页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!
}
确保在您的应用程序中注册用于导航的页面。cs/App。xaml.cs 文件。