在视图模型中使用单调会导致崩溃

时间:2018-11-16 15:24:36

标签: c# xamarin singleton

我对当前用户有单音(在我的应用中是医生):

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

页面代码如下:

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,名称:heckorexamarin >>> 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 pc 00000000001094f4 /data/app/Mono.Android.DebugRuntime-1/lib/arm64/libmonosgen-64bit-2.0.so

1 个答案:

答案 0 :(得分:1)

您的错误很可能与单例无关。您的财产是杀手--它会自行访问:

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;
    }
 }