【问题标题】:Xamarin forms Prism - Navigation from grid to content page. Page not shownXamarin 形成 Prism - 从网格导航到内容页面。页面未显示
【发布时间】:2017-01-17 02:40:06
【问题描述】:

在 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 的 sn-p:

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 的 sn-p:

public void OnNavigatedTo(NavigationParameters parameters)
{
    // on Debug I enter in this method!
}

【问题讨论】:

    标签: c# xamarin.forms prism


    【解决方案1】:

    确保在 App.cs/App.xaml.cs 文件中注册导航页面。

    【讨论】:

    • 已注册。如果在 OnInitialized() 我输入: NavigationService.NavigateAsync("RoomDetail");页面出现。
    • 那么房间列表页面可能有问题。尝试将 TapGesture 放在 ListView 而不是 StackPanel 上。
    • 也许你是对的。如果我把这个放在列表页面中: 它正在工作。您知道从列表视图的元素链接命令的另一种方法吗?我知道的唯一选择是使用丑陋的事件处理程序,但这会破坏 MVVM。谢谢! (棱镜很棒!)
    • 实际上,现在我认为 ListView 已损坏,需要一个丑陋的事件处理程序。否则,您会将 COMmandParameter 绑定到 ListView.SelectedItem.Id,但它第一次不起作用,但第二次会起作用。我认为 ListView 中存在错误。您必须先尝试确认。
    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多