Question

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);
      }
    }
  }
}
Was it helpful?

Solution

  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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top