我有MainPageViewModel与Items(ObservableCollection).在这个页面上,我还有一个按钮,可以向项目中添加新项目.
public class MainPageViewModel : Screen { private DateTime StartActivity = DateTime.MinValue; public ObservableCollection<ActivityViewModel> Items { get; set; } public MainPageViewModel(INavigationService navigationService) { this.Items = new ObservableCollection<ActivityViewModel>(); } public void AddActivity(string activityName) { if (this.Items.Count == 0) { this.Items.Add(new ActivityViewModel() { Activity = activityName,Duration = 0 }); StartActivity = DateTime.Now; } else { this.Items[this.Items.Count - 1].Duration = 10; this.Items.Add(new ActivityViewModel() { Activity = activityName,Duration = 0 }); StartActivity = DateTime.Now; } } }
添加新项目非常有效.
但是,在逻辑删除后应用程序激活时,来自项目的数据无法恢复.尝试为我的ViewModel创建StorageHandler.没有帮助.我做错了什么?
public class MainPageViewModelStorage : StorageHandler<MainPageViewModel> { public override void Configure() { Property(x => x.Items) .InAppSettings() .RestoreAfterActivation(); } }
另外,尝试为类和属性添加[SurviveTombstone],但Visual Studio不知道该属性.
public class ActivityViewModel : PropertyChangedBase { private string _activity; public string Activity { get { return _activity; } set { if (value != _activity) { _activity = value; NotifyOfPropertyChange(() => Activity); } } } private double _duration; public double Duration { get { return _duration; } set { if (value != _duration) { _duration = value; NotifyOfPropertyChange(() => Duration); } } } }
>您应该不存储InAppSettings而是存储InPhoneState. >如果调用方法Configure,则使用断点检查.如果不是 – 你的引导程序有问题.可能缺少PhoneContainer.RegisterPhoneServices() >在Visual Studio中启用捕获第一次机会异常(Ctrl Alt E,并将CLR异常放入复选框).可能您的视图模型无法正确反序列化.