MvvmCross绑定命令与Windows Phone中的参数

Ale*_* D. 1 xaml binding mvvmcross xamarin windows-phone-8

我目前正在开发一个使用Xamarin和MvvmCross开发的简单应用程序.我有一个简单的项目列表,我希望点击一个项目打开一个新的视图.

我设法在Android中执行以下操作:

此视图位于视图的.axml中

ListCustomersView.xaml

<MvxListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxItemTemplate="@layout/listcustomerscell"
        local:MvxBind="ItemsSource ListCustomers; ItemClick ShowCommand"
        android:id="@+id/listCustomersView" />
Run Code Online (Sandbox Code Playgroud)

和ViewModel文件中的此代码:

ListCustomersViewModel.cs

private MvxCommand<CustomerListDTO> _showCommand;
public ICommand ShowCommand
{
     get { return _showCommand ?? (_showCommand = new MvxCommand<CustomerListDTO>(c => this.ShowCustomerDetail(c))); }
}

public void ShowCustomerDetail(CustomerListDTO c)
{
    ShowViewModel<CustomerDetailViewModel>(new CustomerDetailParameters() { CustomerID = c.Id });
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我通过命令发送一个参数:CustomerDetailParameters类的一个实例.它在Android中运行良好,但我无法在Windows Phone中实现这一点.

我在ListCustomersView.xaml中使用此代码:

<ListBox Name="listBox" ItemsSource="{Binding ListCustomers}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <controls:ListCustomersCell />
            </DataTemplate>
        </ListBox.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding ShowCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

ShowCommand调用良好,但应发送的CustomerListDTO为null.我想我没有使用好的代码.如果您有任何想法,我会接受它们.

感谢您的帮助,祝您有个美好的一天!

Muh*_*lah 5

命令参数未传递给命令.通过将CommandParameter传递给VM来尝试以下代码

CommandParameter="{Binding ElementName=listBox, Path=SelectedItem}
Run Code Online (Sandbox Code Playgroud)