【问题标题】:MvvmCross vnext: monodroid CommandParameter similar to wp7MvvmCross vnext:类似于wp7的monodroid CommandParameter
【发布时间】:2012-10-15 09:23:22
【问题描述】:

我在教程示例的 MainMenuView 中使用字典而不是列表。在 wp7 中,我是这样绑定的:

 <ListBox ItemsSource="{Binding Items}" x:Name="TheListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Key}" Margin="12" FontSize="24" TextWrapping="Wrap">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Tap">
                                <commandbinding:MvxEventToCommand Command="{Binding Path=DataContext.ShowItemCommand, ElementName=TheListBox}" CommandParameter="{Binding Value}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

但是对于 monodroid,我不知道在 mvxListView 中将 CommandParameter="{Binding Value}" 放在哪里,我收到此错误:"MvxBind:Error: 2,71 Problem seen during binding execution for from Items to ItemsSource -问题 ArgumentException: failed to convert parameters”来自我的 axml 代码:

<Mvx.MvxBindableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="{'ItemsSource':{'Path':'Items'},'ItemClick':{'Path':'ShowItemCommand'}}"
local:MvxItemTemplate="@layout/listitem_viewmodel"

/>

如何像 wp7 一样设置 CommandParameter 属性?

提前感谢您的帮助。

按照您的说明 1,我在 Tutorial.Core 中更改 MainMenuViewModel,如下所示:

`公共词典项目{get;放; }

    public ICommand ShowItemCommand
    {
        get
        {
            return new MvxRelayCommand<KeyValuePair<string, Type>>((type) => DoShowItem(type.Value));
        }
    }

    public void DoShowItem(Type itemType)
    {
        this.RequestNavigate(itemType);
    }

    public MainMenuViewModel()
    {
        Items = new Dictionary<string, Type>()
                    {
                        {"SimpleTextProperty",  typeof(Lessons.SimpleTextPropertyViewModel)},
                        {"PullToRefresh",  typeof(Lessons.PullToRefreshViewModel)},
                        {"Tip",  typeof(Lessons.TipViewModel)},
                        {"Composite",typeof(Lessons.CompositeViewModel)},
                        {"Location",typeof(Lessons.LocationViewModel)}
                    };
    }`

该示例在 wp7 中按预期工作,但使用 monodroid 我得到与前一个相同的错误,因为我认为 KeyValuePair 键属性导致问题:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
          android:layout_margin="12dp"
        android:orientation="vertical">
<TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="View Model:"
        />
  <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          local:MvxBind="{'Text':{'Path':'Key'}}"
        />
</LinearLayout>

【问题讨论】:

    标签: c# windows-phone-7 xamarin.ios xamarin.android mvvmcross


    【解决方案1】:

    Mvx 目前没有单独的 CommandParameter 依赖目标,因此您目前无法以相同的方式解决此问题。

    没有包含 CommandParameters 的原因是设计选择,并且与缺少行为有关。因为没有一个行为对象将命令和命令参数包装在一个控件事件周围,所以 Click、LongClick、Swipe 等需要单独的 CommandParameter 绑定 - 这些可能会变得非常冗长和丑陋 - 所以,到目前为止,我们已经避开了这种方法。

    但是,您可以通过多种方式获得与您正在寻找的效果相似的效果。


    首先,列表上的 ItemClick 事件始终是绑定的,因此参数始终是已单击的对象 - 因此,如果您可以在 MvxRelayCommand 操作中进行 .Value 投影,那么代码将在 WP7 中都有效在 MonoDroid 中。

    即这是可以实现的:

    <ListBox ItemsSource="{Binding Items}" x:Name="TheListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Key}" Margin="12" FontSize="24" TextWrapping="Wrap">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Tap">
                                <commandbinding:MvxEventToCommand Command="{Binding Path=DataContext.ShowItemCommand, ElementName=TheListBox}" CommandParameter="{Binding}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    使用:

    <Mvx.MvxBindableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    local:MvxBind="{'ItemsSource':{'Path':'Items'},'ItemClick':{'Path':'ShowItemCommand'}}"
    local:MvxItemTemplate="@layout/listitem_viewmodel"
    />
    

    您的命令处理程序在哪里执行 .Value 工作:

    公共 ShowItemCommand { get { return new MvxRelayCommand( item => { DoShowFor(item.Value); } ); } }


    其次,您可以选择绑定到每个列表项中的视图/控件上的 Click 事件,而不是绑定到列表级事件。有关这方面的一些讨论,请参阅MVVMCross changing ViewModel within a MvxBindableListView上的答案


    第三,如果你真的想的话,你可以在这种情况下编写自己的绑定......我认为这对于这种情况来说有点过头了,但在其他情况下它可能会很有用。


    有关更多列表选择示例,请查看 BestSellers 和 CustomerManagement 示例。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多