MvvmCross绑定在Windows Phone参数命令 [英] MvvmCross bind command with parameter in Windows Phone

查看:129
本文介绍了MvvmCross绑定在Windows Phone参数命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与Xamarin和MvvmCross一个简单的应用程序developped。
我有一个项目一个简单的列表,我想上的项目点击打开一个新的视角。

我能够做到,在与Android的:

这code视图的.axml

ListCustomersView.xaml

 < MvxListView
        安卓了minWidth =25像素
        安卓了minHeight =25像素
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        本地:MvxItemTemplate =@布局/ listcustomerscell
        本地:MvxBind =的ItemsSource ListCustomers;项目单击ShowCommand
        机器人:ID =@ + ID / listCustomersView/>

这code。在视图模型文件:

ListCustomersViewModel.cs

 私人MvxCommand< CustomerListDTO> _showCommand;
公众的ICommand ShowCommand
{
     {返回_showCommand? (_showCommand =新Mv​​xCommand< CustomerListDTO>(C => this.ShowCustomerDetail(C))); }
}公共无效ShowCustomerDetail(CustomerListDTO C)
{
    ShowViewModel&所述; CustomerDetailViewModel>(新CustomerDetailParameters(){客户id = c.Id});
}

正如你所看到的,我通过命令传递一个参数:该CustomerDetailParameters类的一个实例。它的工作原理以及在Android中,但我不设法做,在Windows Phone的。

我米的ListCustomersView.xaml使用这种code:

 <列表框名称=列表框的ItemsSource ={结合ListCustomers}>
        < ListBox.ItemContainerStyle>
            <风格的TargetType =ListBoxItem的>
                < setter属性=Horizo​​ntalContentAlignmentVALUE =弹力/>
            < /样式和GT;
        < /ListBox.ItemContainerStyle>
        < ListBox.ItemTemplate>
            <&DataTemplate的GT;
                <控制:ListCustomersCell />
            < / DataTemplate中>
        < /ListBox.ItemTemplate>
        < I:Interaction.Triggers>
            < I:的EventTrigger事件名称=的SelectionChanged>
                < I:InvokeCommandAction命令={结合ShowCommand}/>
            < /我:&的EventTrigger GT;
        < /我:Interaction.Triggers>
< /列表框>

该ShowCommand很好调用,但应该发送的CustomerListDTO为空。我想我没有使用好code。如果您有什么想法,我会带他们。

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


解决方案

命令放慢参数是不是传递到命令

。试试下面code。通过传递CommandParameter到VM

  CommandParameter ={绑定的ElementName =列表框,路径=的SelectedItem}

i am currently working on a simple app developped with Xamarin and MvvmCross. I have a simple list of items and i want a click on an item open a new view.

I managed to do that in Android with :

this code in the .axml of the view

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" />

and this code in the ViewModel file :

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 });
}

As you can see, i send a parameter through the command : an instance of the CustomerDetailParameters class. It works well in Android, but i do not manage to do that in Windows Phone.

I m using this code in the 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>

The ShowCommand is well called but the CustomerListDTO which should be sent is null. I imagine i am not using the good code. If you have any idea, i will take them.

Thanks for any help and have a good day !

解决方案

Command paramter is not passed to the command. Try below code by passing CommandParameter to the VM

CommandParameter="{Binding ElementName=listBox, Path=SelectedItem}

这篇关于MvvmCross绑定在Windows Phone参数命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆