c# - 在视图模型中使用单色调崩溃
问题描述
我有当前用户的单音(在我的应用程序中是医生):
sealed class CurrentDataStorage
{
private static CurrentDataStorage dataStorage;
private CurrentDataStorage()
{
}
public static CurrentDataStorage DataStorage
{
get
{
if(dataStorage == null)
{
dataStorage = new CurrentDataStorage();
}
return dataStorage;
}
}
public Doctor CurrentDoctor { get
{
return CurrentDoctor;
}
set
{
if(CurrentDoctor != value)
CurrentDoctor = value;
}
}
}
如果我在 viewmodel 类中使用它,我的应用程序会崩溃。也许 CurrentDoctor 为空。我在授权页面中创建了一些医生(它是后端用户创建的存根):
public void OnAuthorization(object sender, EventArgs a)
{
CurrentDataStorage.DataStorage.CurrentDoctor = new Doctor("Иван", "Иванов", "Иванович", "house_md@gmail.com", "superdoctor321",
"Princeton Plainsboro", Model.Enums.Position.Researcher, Model.Enums.Category.Highest, Model.Enums.AcademicDegree.Doctor);
Application.Current.MainPage = new ProfilePage();
}
ProfilePage 是主详细信息。我用绑定创建了这个面板:
页面代码如下所示:
public ProfilePageMaster()
{
InitializeComponent();
this.BindingContext = new ProfilePageMasterViewModel();
}
class ProfilePageMasterViewModel : INotifyPropertyChanged
{
Doctor doctor;
public ProfilePageMasterViewModel()
{
doctor = CurrentDataStorage.DataStorage.CurrentDoctor;
}
public String FullName
{
get
{
return doctor.FullName;
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged == null)
return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
我在使用单音时做错了什么?
UPD:这个崩溃报告(它什么也没告诉我)
构建指纹:'Xiaomi/kenzo/kenzo:6.0.1/MMB29M/8.11.8:user/release-keys' 修订版:'0' ABI:'arm64' pid:10207,tid:10207,名称:hecksorexamarin >>> com.pesiik.checksorexamarin <<< 信号 11 (SIGSEGV),代码 2 (SEGV_ACCERR),fault addr 0x7fc77fad00 x0 0000007fc77fad00 x1 000000559713ab50 x2 0000000000000290 x3 0000007f97712000 x4 0000000000000000 x5 0000007f977126f0 x6 0000007fc77facf0 x7 0000007f79179f60 x8 0000007f79179f70 x9 0000007f79179f60 x10 0000007f92cc1840 x11 0000007f92ccd630 x12 0000007f92cb5748 x13 0000007f92ccaa70 x14 0000000000000003 x15 00000055977c7c58 x16 0000007f8e457df8 x17 0000007f9767bc08 x18 6000000000000000 x19 0000000000000000 x20 0000007f79072868 x21 0000007f79072868 x22 0000007f79117d90 x23 0000007f7907f2b0 x24 0000000000000000 x25 0000000000000000 x26 0000007f79179f70 x27 0000000012cbe640 x28 00000000729a2507 x29 0000007fc77fb020 x30 0000007f8e2344f8 sp 0000007fc77fad00 pc 0000007f9767bd54 pstate 00000000200000000000000000000000 x25 0000000000000000 x26 0000007f79179f70 x27 0000000012cbe640 x28 00000000729a2507 x29 0000007fc77fb020 x30 0000007f8e2344f8 sp 0000007fc77fad00 pc 0000007f9767bd54 pstate 00000000200000000000000000000000 x25 0000000000000000 x26 0000007f79179f70 x27 0000000012cbe640 x28 00000000729a2507 x29 0000007fc77fb020 x30 0000007f8e2344f8 sp 0000007fc77fad00 pc 0000007f9767bd54 pstate 0000000020000000
回溯:#00 pc 0000000000019d54 /system/lib64/libc.so (memcpy+332) #01 pc 00000000001094f4 /data/app/Mono.Android.DebugRuntime-1/lib/arm64/libmonosgen-64bit-2.0.so
解决方案
您的错误很可能与单例无关。您的财产是杀手 - 它访问自己:
public Doctor CurrentDoctor
{
get
{
return CurrentDoctor;
}
set
{
if(CurrentDoctor != value)
CurrentDoctor = value;
}
}
改为使用支持字段
private Doctor _currentDoctor
public Doctor CurrentDoctor
{
get
{
return _currentDoctor;
}
set
{
if(_currentDoctor != value)
_currentDoctor = value;
}
}
推荐阅读
- compiler-errors - Imagemagick 没有此图像格式“PNG”Ubuntu 20.04 的解码委托
- scripting - Ping 解析问题
- ios - Iphone x 及更高版本的安全区域内的图像选择器 cameraoverlay 无法正常工作
- kubernetes - 无头服务在 Kubernetes 中是否“原子”更新?
- google-cloud-platform - 使用 n2-standard-2 机器类型运行数据流作业,数据流作业是否可以随时仅使用 2 个 vCPU 运行,或者它可以自动扩展到配额限制
- c++ - 子类运算符 << 重载 C++
- shapley - shapley-values 如何解释我的模型中没有特征?
- amazon-web-services - 如何使用 Java SDK 和 SSO 向 AWS 进行身份验证
- laravel - 如何在 LARAVEL 中编辑搜索表单的占位符
- postgresql - 在插入PostgreSQL之前剪切一列的内容