Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tombstoning in Caliburn Micro

I have MainPageViewModel with Items (ObservableCollection). On this page I also have a button, that add new items to Items.

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

Adding new items works perfect.

But data from Items not recovers when app activates after tombstoning. Try create StorageHandler for my ViewModel. Doesn't help. What I'm doing wrong?

public class MainPageViewModelStorage : StorageHandler<MainPageViewModel> {
  public override void Configure() {
    Property(x => x.Items)
        .InAppSettings()
        .RestoreAfterActivation();
  }
}

Also try add [SurviveTombstone] for class and for property but Visual Studio don't know that attribute.

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);
      }
    }
  }
}
like image 892
feeeper Avatar asked Oct 06 '22 21:10

feeeper


1 Answers

  1. You should store not InAppSettings but InPhoneState.
  2. Check with breakpoint if method Configure is called. If not - something wrong with your bootstrapper. Probably PhoneContainer.RegisterPhoneServices() is missing
  3. Turn on catching first chance exception in Visual Studio (Ctrl+Alt+E, and put checkbox against CLR Exceptions). Probably your view model cannot be deserialized properly.
like image 89
Grigory Avatar answered Oct 10 '22 22:10

Grigory