[英]MvvmCross vnext: monodroid CommandParameter similar to wp7
我在Tutorial示例的MainMenuView中使用Dictionary而不是List。 在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,我不知道將CommandParameter =“ {Binding Value}”放在mvxListView中的位置,會出現以下錯誤:“ MvxBind:Error:2,71在從Items到ItemsSource的綁定執行過程中看到的問題-問題ArgumentException:無法從我的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,如下所示:
公共字典項{ 組; }
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 Key屬性會導致以下問題:
<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>
Mvx當前沒有單獨的CommandParameter依賴關系目標,因此您當前無法以相同的方式解決此問題。
不包含CommandParameters的原因是設計選擇,並且與缺乏行為有關。 由於沒有行為對象將命令和commandparameter圍繞一個控制事件包裝在一起,因此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 {獲取{返回新的MvxRelayCommand(item => {DoShowFor(item.Value);}); }}
其次,您可以選擇綁定到每個列表項中“視圖/控件”上的Click事件,而不是綁定到列表級事件。 有關此問題的一些討論,請參見MVVMCross更改MvxBindableListView中的ViewModel的答案
第三,如果您確實想這樣做,則可以在這種情況下編寫自己的綁定。我認為這對於這種情況可能是過大了,但是在其他情況下可能會有用。
有關更多列表選擇示例,請查看BestSellers和CustomerManagement示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.