xamarin - 当我使用对象属性(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:链接到我的解决方案
解决方案
您可以尝试更改属性,如下所示:
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));
}
}
推荐阅读
- javascript - 由于 useState 挂钩,我无法在 Safari 上正确处理状态
- graphics - 如何在 Windows 10 上更新 CUDA 驱动程序 nvidia?
- java - 我保存了一个待办事项列表,如何将其加载到我的待办事项列表应用程序中?
- python - 判断一个字符串是否为 a。另一个字符串的子集。不要使用 contains 或 in 关键字
- powershell - PowerShell Invoke-Command 与本地计算机上的 FilePath - 模糊参数错误?
- python - 使用 selenium webdriver,python 检索动态值
- sql - 根据不同的值顺序聚合列
- javascript - 未捕获的类型错误:无法读取未定义的属性“地图”(REACT NATIVE - Javascript)
- python - ModuleNotFoundError:没有名为“请求”的模块,但它已安装?
- r - 如何将 S3 方法声明为默认加载环境?