我有 MainPageViewModel with 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;
}
}
}
添加新项目非常完美。
但是,当应用在逻辑删除后激活时,来自项目的数据不会恢复。尝试为我的视图模型创建存储处理程序。无济于事。我做错了什么?
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异常设置复选框)。您的视图模型可能无法正确反序列化。