У меня есть синглетон для текущего пользователя (в моем приложении есть доктор):
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 является masterdetail. Я создаю эту панель с привязкой:
![enter image description here](https://i.stack.imgur.com/pdLEV.png)
Код страницы выглядит следующим образом:
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: пользовательские / релиз-ключи»
Редакция: «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 </p>
трассировка:
# 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