【问题标题】:Why Xamarin renders nothing when I try to access a property of the binded object?为什么当我尝试访问绑定对象的属性时 Xamarin 什么也不呈现?
【发布时间】:2021-03-18 06:50:32
【问题描述】:

这里展示了我的一个 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
      }
}

最后,MyType 类定义为:

public class MyClass {
   public string MyProperty { get; set; }
}

我无法弄清楚为什么 &lt;Label Text="{Binding MyClass.MyProperty}"/&gt; 什么都没有显示,即使我在调试期间检查过,属性 gest 是从视图模型的 GetFromAPI() 方法中正确填充的。

【问题讨论】:

  • 您的MyClass 属性是News 类型,而不是MyClass 类型。 News 是否有一个名为 MyProperty 的属性?
  • 对不起,我修好了。
  • 请花时间准确发布您的代码。当我发现每个问题都只是您在发布代码时犯的“错字”时,花时间试图提供帮助是非常令人沮丧的。正如当前发布的那样,您的属性 MyString 不包含任何值,因此它当然不会在您的 UI 中显示任何内容。
  • 您可以通过设置默认值public string MyProperty { get; set; } = "test";来轻松测试这一点

标签: c# xaml xamarin data-binding binding


【解决方案1】:

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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2012-05-28
    • 2014-10-17
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多