Caliburn Micro中的墓碑

时间:2012-11-25 17:56:18

标签: windows-phone-7 caliburn.micro tombstoning

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

添加新项目非常有效。

但是,当应用程序在逻辑删除后激活时,来自项目的数据无法恢复。尝试为我的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);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:4)

  1. 您不应该存储InAppSettings而是InPhoneState
  2. 如果调用方法Configure,请使用断点检查。如果不是 - 你的引导程序有问题。可能PhoneContainer.RegisterPhoneServices()缺少
  3. 在Visual Studio中启用捕获第一次机会异常(Ctrl + Alt + E,并针对CLR异常设置复选框)。可能您的视图模型无法正确反序列化。