【发布时间】:2018-02-20 13:48:13
【问题描述】:
我是 Xamarin 的新手,目前正在研究我面临以下问题的解决方案。 我有一个 A 类,它是我的模型类,B 类是我的 viewModel。 模型类
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));
}
}
我正在将 A 类的对象创建为 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)不会调用我的viewModel的命令并执行我的方法DoMakeCall(string s)。
谁能帮我理解这段代码有什么问题?以及如何实现这种情况?
我的 Command 属性属于 VM 类,而不是 Model 类。我唯一需要知道的是如何正确设置上下文以使其工作。我不想使用中继命令。
【问题讨论】:
标签: xaml xamarin xamarin.forms