2017-09-12 37 views
0

我是Xamarin新手,目前正在研究解決方案,我正面臨着以下問題。 我有一個類是我的Model類,而B類是我的viewModel。 模型類Xamarin命令不適用於Observablecollection對象

Class A : INotifyPropertyChanged 
{ 
    public string sampleprop { get; set; } 
    public event PropertyChangedEventHandler PropertyChanged; 
    public virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged == null) 
       return; 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
} 

我在B類

Class B 
    { 
    public Command<string> CallCommand { get; set; } 
    public ObservableCollection<A> AobjectsCollection { get; set; } 

    public B() 
     { 
      AobjectsCollection = new ObservableCollection<A>(); 
      CallCommand = new Command<string>((string arg) => 
    DoMakeCall(arg)); 
     } 
    public void DoMakeCall(string phNumber) 
     { 
      string s = phNumber; 
     } 
} 

創建一個類的對象作爲觀察的集合分配B類作爲我的主視圖頁面綁定上下文。

public partial class Mainview : ContentPage 
{ 
    InitializeComponent(); 
     BindingContext = new B(); 
    } 

在主視圖(XAML)我創造與ClassB的的觀察的集合屬性列表視圖。

 <ListView x:Name="MessagesListView" 
       ItemsSource="{Binding AobjectsCollection }" 
       HasUnevenRows="True" > 
       <ListView.ItemTemplate> 
        <DataTemplate> 
        <ViewCell > 
         <ViewCell.View> 
      <Button x:Name="btnClick" Text="ClickMe" 
        Command="{Binding CallCommand}" 
        CommandParameter="sampleprop"/> 
      </ViewCell.View> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
     </ListView> 

現在點擊在主頁按鈕(btnClick)不調用我的視圖模型的命令並執行我的方法DoMakeCall(字符串s)。

任何人都可以請幫我理解這段代碼有什麼問題嗎?以及我如何實現這種情況?

我的命令屬性是VM類而不是Model類。唯一需要了解的是如何正確設置環境以使其工作。我不想使用中繼命令。

+0

[RelayCommand參數傳遞Xamarin](可能的重複https://stackoverflow.com/questions/31812035/relaycommand-parameter -pass-in-xamarin) – Unlockedluca

回答

0

您目前正在將每個ViewCell綁定到您的A模型。它沒有引用B ViewModel(這是你想要調用的命令所在的位置)爲了解決這個問題,你需要告訴ViewCell按鈕在MessagesListView的BindingContext(在這種情況下是你的B ViewModel)上查找命令。這將是這樣的:

BindingContext="{Binding Source={x:Reference MessagesListView}, Path=BindingContext}" 

而在完整的上下文:

<ListView x:Name="MessagesListView" 
      ItemsSource="{Binding AobjectsCollection }" 
      HasUnevenRows="True" > 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell x:Name="viewCell"> 
         <ViewCell.View> 
          <Button x:Name="btnClick" Text="ClickMe" 
           BindingContext="{Binding Source={x:Reference MessagesListView}, Path=BindingContext}" 
           Command="{Binding CallCommand}" 
           CommandParameter="{Binding Source={x:Reference viewCell}, Path=BindingContext}"/> 
         </ViewCell.View> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
</ListView> 
+0

非常感謝,它適用於這種情況。但是當我有List 而不是Sampleprop in classB,如何將當前對象(someotherclassobject)設置爲commandParameter?當我們將按鈕的Bindingcontext設置爲ModelView時,我無法獲取已填充的listview的當前對象。我試過CommandParameter = {Bindig。},但它沒有奏效。 (你能幫助我嗎?) – Thavudu

+0

我添加了一些代碼,使命令參數綁定到單元格的原始viewModel,這將是您的ClassA實例,我認爲您的命令將接受綁定到ViewCell的ClassA實例點擊。 –

+0

感謝您的答覆。綁定ViewCell不工作,它引發運行時異常。我嘗試過使用ViewCell和ViewCell.BindingContext ...但沒有運氣:'( – Thavudu

0

不使用按鈕,我建議您使用ItemSelected="Handle_ItemSelected

<ListView x:Name="MessagesListView" 
      ItemsSource="{Binding AobjectsCollection }" 
      HasUnevenRows="True" 
      ItemSelected="Handle_ItemSelected"> 

你可以參考這裏:Tap Gesture on List View Items

的交代很詳細。希望能幫助到你。