首页 > 解决方案 > 当我使用对象属性(get-set)时,Xamarin 表单崩溃的 android 项目

问题描述

我在创建项目时遇到了问题。如果我使用属性 (get;set;),android 应用程序会在为属性分配值时崩溃。

例如:我创建了一个干净的 xamarin 项目来消除我的代码的影响。

我班的财产:

 public class Item
    {
     public string Id
            {
                get { return Id; }
                set { Id = value; }
            }
    }

物业用途:

 public AboutPage()
        {
            Item gg = new Item();
            gg.Id = "test";
            InitializeComponent();
        }

应用程序崩溃在线:

set { Id = value; }

错误不显示。 错误

帮助。这是我第一次看到这种情况。我已经降级了平台。使用干净的项目。我究竟做错了什么?

UPD:链接到我的解决方案

标签: xamarinproperties

解决方案


您可以尝试更改属性,如下所示:

public class Item
{
    public string Id { get; set; }

}

或者

public class Item
{
    private string id;
    public string Id
    {
        get { return id; }
        set { id = value; }
    }
}

当你实现INotifyPropertyChanged 接口时:

public class Item : INotifyPropertyChanged
{
    private string id;
    public string Id
    {
        get { return id; }
        set { id = value; OnPropertyChanged("Id"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

推荐阅读