2012-10-15 31 views
1

我從教程樣品MainMenuView使用,而不是列表的字典。在WP7,我綁定是這樣的:MvvmCross vnext:MonoDroid的CommandParameter類似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 = 「{綁定值}」 在mvxListView,我得到這個錯誤:「MvxBind:錯誤:2, 71問題從項目到綁定的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" 

/>

如何設置一個CommandParameter財產在WP7?

在此先感謝您的幫助。

按照你的指令1,我改變MainMenuViewModel在Tutorial.Core這樣的:

'公共字典項{獲得;組; }

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> 

回答

1

MVX沒有按」牛逼目前有一個單獨依賴CommandParameter目標,所以你目前不能以同樣的方式解決這個問題。

爲什麼不包括CommandParameters的原因是設計選擇,並綁在缺乏行爲。因爲沒有一個行爲對象將命令和命令參數一起封裝在一個控件事件周圍,那麼單擊,長按,滑動等就需要單獨的CommandParameter綁定 - 並且這些可能會變得非常冗長和難看 - 所以,到目前爲止,我們已經避開了這種方法。

但是,有幾種方法可以達到與您正在查找的效果類似的效果。


首先,清單上的項目單擊事件總是難免使該參數始終是被點擊的對象 - 所以,如果你能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(項目=> {DoShowFor(item.Value);}); }}


其次,你也可以選擇到每個列表項目中結合的Click事件上的意見/控制,而不是綁定到列表級別的事件。對於一些這方面的討論,請參閱答案上MVVMCross changing ViewModel within a MvxBindableListView


第三,你可以編寫自己的綁定在這種情況下,如果你真的想......我想這將是矯枉過正針對這種情況,但它可能在其他情況下有用。


有關更多列表選擇示例,請查看BestSellers和CustomerManagement示例。

+0

我更新了我的問題,因爲我得到了同樣的錯誤。 – Alphapage

+0

我提出了這裏的問題:http://stackoverflow.com/questions/12899593/mvvmcross-vnext-monodroid-binding-dictionary-key-similar-to-wp7 – Alphapage