Xamarin.Forms - 将对象传递给新视图的 Prism EventToCommandBehavior 转换为字符串

Xamarin.Forms - Prism EventToCommandBehavior passing an object to a new view is converted to a string

我最近一直在尝试 Xamarin.Forms 的 Prism 框架,我遇到了 EventToCommandBehavior 的问题,我很确定它应该可以工作。

当我尝试使用 NavigationParameters() 将对象从 ListView 发送到新视图时,它会自动生成为字符串,而不是之前发送的对象。

当我尝试将其转换为一个对象时,出现了 InvalidCastException。

ListView.cs 我通过 Navigate() 将数据从发送到新视图的位置:

private ObservableCollection<Employee> _employeeList = new ObservableCollection<Employee>();
_employeeList.Add("employee", new Employee{ Id = 1, FirstName = "FirstTest", LastName = "LastTest" });

    private async void Navigate()
    {
        var employeePara = new NavigationParameters();
        employeePara.Add("employee", _selectedEmployee);

        await _navigationService.NavigateAsync("EmployeeDetail" + employeePara);
    }

新视图,我尝试将数据作为对象获取:

private Employee _employee;
public void OnNavigatingTo(NavigationParameters parameters)
{
    try
    {
if (parameters.ContainsKey("employee"))
        _employee = (Employee)parameters["employee"]; // invalid cast happening here
        EmployeeFullName = _employee.FirstName;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

XAML:

  <ContentPage.Resources>
    <ResourceDictionary>
      <c:ItemTappedEventArgsConverter x:Key="itemTappedEventArgs" />
    </ResourceDictionary>
  </ContentPage.Resources>
  <ContentPage.Content>
    <ListView ItemsSource="{Binding EmployeeList}" SelectedItem="{Binding SelectedEmployee}">
      <ListView.Behaviors>
        <b:EventToCommandBehavior
          EventName="ItemTapped"
          Command="{Binding EmployeeTappedCommand}"/>
      </ListView.Behaviors>
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding FullName}" />
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </ContentPage.Content>

Prism for Forms 示例也使用类似的场景,但具有不同的 NavigateAsync 方法签名。 我的猜测,你猜错了吗?

示例代码await _navigationService.NavigateAsync("RecipePage", p);

示例解决方案https://github.com/PrismLibrary/Prism-Samples-Forms/blob/master/ContosoCookbook/ContosoCookbook/ContosoCookbook/ViewModels/MainPageViewModel.cs#L35