在视图模型中使用单例时崩溃
Crash with using singletone in viewmodel
我有当前用户的单音(在我的应用程序中是医生):
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;
}
}
}
如果我在视图模型中使用它 class 我的应用程序会崩溃。也许 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 是 masterdetail。我用绑定创建这个面板:
页面代码如下所示:
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),故障地址 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 0000000020000000
回溯:
#00 pc 0000000000019d54 /system/lib64/libc.so (memcpy+332)
#01 件 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;
}
}
我有当前用户的单音(在我的应用程序中是医生):
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;
}
}
}
如果我在视图模型中使用它 class 我的应用程序会崩溃。也许 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 是 masterdetail。我用绑定创建这个面板:
页面代码如下所示:
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),故障地址 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 0000000020000000
回溯: #00 pc 0000000000019d54 /system/lib64/libc.so (memcpy+332) #01 件 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;
}
}