我是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類。唯一需要了解的是如何正確設置環境以使其工作。我不想使用中繼命令。
[RelayCommand參數傳遞Xamarin](可能的重複https://stackoverflow.com/questions/31812035/relaycommand-parameter -pass-in-xamarin) – Unlockedluca