【发布时间】: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