为什么当我尝试访问绑定对象的 属性 时 Xamarin 不呈现任何内容?
Why Xamarin renders nothing when I try to access a property of the binded object?
此处展示了我的 Xamarin MVVM 应用程序页面之一。它的XAML代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...>
<ContentPage.Content>
<RefreshView Command="{Binding Load}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<ScrollView>
<Label Text="{Binding MyClass.MyProperty}"/>
</ScrollView>
</RefreshView>
</ContentPage.Content>
</ContentPage>
这是背后的代码:
namespace MyNamespace {
public partial class MyPage {
private readonly MyModel _viewModel;
public NewsDetailPage() {
InitializeComponent();
BindingContext = _viewModel = new MyModel();
}
protected override void OnAppearing() {
base.OnAppearing();
_viewModel.OnAppearing();
}
}
}
这是视图模型:
namespace MyOtherNamespace {
public class MyModel {
private string _myProperty;
public MyModel() {
MyClass = new MyClass ();
Load= new Command(async () => await GetFromAPI("one"));
}
public Command Load { get; set; }
public MyClass MyClass { get; set; }
public string MyProperty{
get => _myProperty;
set {
_date= _myProperty;
SetProperty(ref _myProperty, value);
}
}
public void OnAppearing() {
IsBusy = true;
}
public async Task GetFromAPI(string x) {
// News = load from a Web API and populates the MyProperty property
}
}
最后,MyTypeclass定义为:
public class MyClass {
public string MyProperty { get; set; }
}
我不明白为什么 <Label Text="{Binding MyClass.MyProperty}"/>
什么也没显示,即使我在调试过程中检查过 属性 gest 从视图的 GetFromAPI()
方法中正确填充型号。
在 MyClass
中实现 INotifyPropertyChanged
接口,它在我这边有效:
public class MyModel
{
public MyModel()
{
MyClass = new MyClass();
MyClass.MyProperty = "abc";
Load = new Command(async () => await GetFromAPI("one"));
}
public Command Load { get; set; }
public MyClass MyClass { get; set; }
public bool IsBusy { get; private set; }
public void OnAppearing()
{
IsBusy = true;
}
public async Task GetFromAPI(string x)
{
// News = load from a Web API and populates the MyProperty property
MyClass.MyProperty = "efg";
}
}
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string myProperty { get; set; }
public string MyProperty
{
set
{
if (myProperty != value)
{
myProperty = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MyProperty"));
}
}
}
get
{
return myProperty;
}
}
}
此处展示了我的 Xamarin MVVM 应用程序页面之一。它的XAML代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...>
<ContentPage.Content>
<RefreshView Command="{Binding Load}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<ScrollView>
<Label Text="{Binding MyClass.MyProperty}"/>
</ScrollView>
</RefreshView>
</ContentPage.Content>
</ContentPage>
这是背后的代码:
namespace MyNamespace {
public partial class MyPage {
private readonly MyModel _viewModel;
public NewsDetailPage() {
InitializeComponent();
BindingContext = _viewModel = new MyModel();
}
protected override void OnAppearing() {
base.OnAppearing();
_viewModel.OnAppearing();
}
}
}
这是视图模型:
namespace MyOtherNamespace {
public class MyModel {
private string _myProperty;
public MyModel() {
MyClass = new MyClass ();
Load= new Command(async () => await GetFromAPI("one"));
}
public Command Load { get; set; }
public MyClass MyClass { get; set; }
public string MyProperty{
get => _myProperty;
set {
_date= _myProperty;
SetProperty(ref _myProperty, value);
}
}
public void OnAppearing() {
IsBusy = true;
}
public async Task GetFromAPI(string x) {
// News = load from a Web API and populates the MyProperty property
}
}
最后,MyTypeclass定义为:
public class MyClass {
public string MyProperty { get; set; }
}
我不明白为什么 <Label Text="{Binding MyClass.MyProperty}"/>
什么也没显示,即使我在调试过程中检查过 属性 gest 从视图的 GetFromAPI()
方法中正确填充型号。
在 MyClass
中实现 INotifyPropertyChanged
接口,它在我这边有效:
public class MyModel
{
public MyModel()
{
MyClass = new MyClass();
MyClass.MyProperty = "abc";
Load = new Command(async () => await GetFromAPI("one"));
}
public Command Load { get; set; }
public MyClass MyClass { get; set; }
public bool IsBusy { get; private set; }
public void OnAppearing()
{
IsBusy = true;
}
public async Task GetFromAPI(string x)
{
// News = load from a Web API and populates the MyProperty property
MyClass.MyProperty = "efg";
}
}
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string myProperty { get; set; }
public string MyProperty
{
set
{
if (myProperty != value)
{
myProperty = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MyProperty"));
}
}
}
get
{
return myProperty;
}
}
}