2011-08-23 75 views
0

我遇到了一些Caliburn.Micro中的邏輯錯誤問題。我有一個數據透視控件,其中我們在每個透視控件項目中都有一個列表框。我只是用要保存的列表(一個或多個)caliburn.micro的內置支持我該如何才能在caliburn.micro上刻錄子虛擬機

我有一個MainViewModel這將初始化一系列兒童虛擬機的先導控制

public class MainViewModel : Conductor<IScreen>.Collection.OneActive 
{ 
    private readonly Func<QiushiListViewModel> _createQiushiListViewModel; 
    private readonly INavigationService _navigationService; 
    private readonly IEventAggregator _events; 
    readonly IWindowManager _windowManager; 
    readonly Func<AboutUsViewModel> _aboutUsViewModelFactory; 
    readonly Func<SettingsViewModel> _settingsViewModelFactory; 

    public MainViewModel(Func<QiushiListViewModel> createQiushiListViewModel, 
         INavigationService navigationService, 
         IEventAggregator events, 
         IWindowManager windowManager, 
         Func<AboutUsViewModel> aboutUsViewModelFactory, 
         Func<SettingsViewModel> settingsViewModelFactory 
         ) 
    { 
     _createQiushiListViewModel = createQiushiListViewModel; 
     _navigationService = navigationService; 
     _events = events; 
     _windowManager = windowManager; 
     _aboutUsViewModelFactory = aboutUsViewModelFactory; 
     _settingsViewModelFactory = settingsViewModelFactory; 
    } 

    protected override void OnInitialize() 
    { 
     if(Items.Count==0) 
     { 
      Enumerable.Range(1, 5).Apply(x => 
      { 
       var pivot = _createQiushiListViewModel(); 
       switch (x) 
       { 
        case 1: 
         pivot.DisplayName = AppResources.App_Pivot_First; 
         pivot.Section = "late"; 
         break; 
        case 2: 
         pivot.DisplayName = AppResources.App_Pivot_Second; 
         pivot.Section = "pic"; 
         break; 
        case 3: 
         pivot.DisplayName = AppResources.App_Pivot_Third; 
         pivot.Section = "8hr"; 
         break; 
        case 4: 
         pivot.DisplayName = AppResources.App_Pivot_Fourth; 
         pivot.Section = "week"; 
         break; 
        case 5: 
         pivot.DisplayName = AppResources.App_Pivot_Fifth; 
         pivot.Section = "month"; 
         break; 
        default: 
         break; 
       } 
       Items.Add(pivot); 
      }); 
      ActivateItem(Items[0]);  
     } 
     ... 
    } 

} 

而且孩子VM是如下

public class QiushiListViewModel : 
     Screen, 
     IHandle<RefreshListMessage>, 
     IHandle<RetrieveNewItemsMessage>, 
     IHandle<TaskCompleted<EmailComposeTask>>, 
     IHandle<TaskCompleted<SmsComposeTask>> 
    { 
     private const int DefaultItemsPerPage = 20; 
     readonly IEventAggregator _events; 
     private readonly INavigationService _navigationService; 

     public QiushiListViewModel(
      IEventAggregator events, 
      INavigationService navigationService) 
     { 
      ItemsPerPage = DefaultItemsPerPage; 
      _events = events; 
      _navigationService = navigationService; 
      _qiushiItems = new SortableObservableCollection<QiushiItem>(); 
     } 

     protected override void OnActivate() 
     { 
      _events.Subscribe(this); 
      base.OnActivate(); 

     } 

     protected override void OnDeactivate(bool close) 
     { 
      SelectedItem = null; 
      _events.Unsubscribe(this); 
      base.OnDeactivate(close); 
     } 

     public string Section { get; set; } 

     public int Page { get; set; } 

     public int ItemsPerPage { get; set; } 

     public int CountOfItemsUpdated { get; set; } 

     public bool HasErrors { get; set; } 

     private SortableObservableCollection<QiushiItem> _qiushiItems; 

     public IEnumerable<QiushiItem> QiushiItems 
     { 
      get { return _qiushiItems; } 
     } 

     private bool _isBusy; 
     public bool IsBusy 
     { 
      get { return _isBusy; } 
      set 
      { 
       if (_isBusy == value) 
        return; 
       _isBusy = value; 
       NotifyOfPropertyChange(() => IsBusy); 
      } 
     } 

     private bool _canLoadMore; 
     public bool CanLoadMore 
     { 
      get { return _canLoadMore; } 
      set 
      { 
       if (_canLoadMore == value) 
        return; 
       _canLoadMore = value; 
       NotifyOfPropertyChange(() => CanLoadMore); 
      } 
     } 

     private QiushiItem _selectedItem; 
     public QiushiItem SelectedItem 
     { 
      get { return _selectedItem; } 
      set 
      { 
       if(_selectedItem == value) 
        return; 
       _selectedItem = value; 
       NotifyOfPropertyChange(() => SelectedItem); 
      } 
     } 

    ... 
    } 

我試圖實現QiushiListViewModelStorage

public override void Configure() 
     { 
      Id(x=>x.Section); 

      Property(x => x.CanLoadMore).InAppSettings(); 
      Property(x => x.IsBusy).InAppSettings(); 
      Property(x => x.Page).InAppSettings(); 
      Property(x => x.QiushiItems).InAppSettings(); 
     } 

它不起作用,什麼都沒有發生,沒有錯誤,沒有例外。 QiushiItem類是可序列,請參見下面的代碼片段

[DataContract] 
    public class QiushiItem : IComparable, IEqualityComparer<QiushiItem> 
    { 
     [DataMember] 
     public int Id { get; set; } 
     [DataMember] 
     public string Content { get; set; } 
     [DataMember] 
     public string ImageUrl { get; set; } 
     [DataMember] 
     public string MobileImageUrl { get; set; } 
     [DataMember] 
     public string Tags { get; set; } 
     [DataMember] 
     public int Tops { get; set; } 
     [DataMember] 
     public int Flops { get; set; } 
     [DataMember] 
     public int CommentsCount { get; set; } 
     [DataMember] 
     public DateTime PublishDateTime { get; set; } 

     public string Age 
     { 
      get 
      { 
       if (DateTime.Now.Subtract(PublishDateTime).Minutes < 10) 
        return AppResources.App_TimeSpan_JustNow; 
       if (DateTime.Now.Subtract(PublishDateTime).Minutes < 30) 
        return AppResources.App_TimeSpan_Recently; 
       if (DateTime.Now.Subtract(PublishDateTime).Minutes < 60) 
        return AppResources.App_TimeSpan_Half_Hour_Ago; 
       if (DateTime.Now.Subtract(PublishDateTime).Hours < 2) 
        return AppResources.App_TimeSpan_An_Hour_Ago; 
       if (DateTime.Now.Subtract(PublishDateTime).Hours < 3) 
        return AppResources.App_TimeSpan_Two_Hours_Ago; 
       if (DateTime.Now.Subtract(PublishDateTime).Hours < 24) 
        return AppResources.App_TimeSpan_In_One_Day; 
       if (DateTime.Now.Subtract(PublishDateTime).Days < 2) 
        return AppResources.App_TimeSpan_One_Day_Ago; 
       if (DateTime.Now.Subtract(PublishDateTime).Days < 3) 
        return AppResources.App_TimeSpan_Two_Days_Ago; 
       if (DateTime.Now.Subtract(PublishDateTime).Days < 7) 
        return AppResources.App_TimeSpan_In_A_Week; 
       return DateTime.Now.Subtract(PublishDateTime).Days < 30 ? AppResources.App_TimeSpan_In_A_Month : AppResources.App_TimeSpan_Long_Ago; 
      } 
     } 

     int IComparable.CompareTo(object right) 
     { 
      if (!(right is QiushiItem)) throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "right"); 
      QiushiItem righItem = (QiushiItem) right; 
      return CompareTo(righItem); 
     } 

     public int CompareTo(QiushiItem right) 
     { 
      return Id.CompareTo(right.Id); 
     } 

     // Relational Operators. 
     public static bool operator <(QiushiItem left, QiushiItem right) 
     { 
      return left.CompareTo(right) < 0; 
     } 
     public static bool operator <=(QiushiItem left, QiushiItem right) 
     { 
      return left.CompareTo(right) <= 0; 
     } 
     public static bool operator >(QiushiItem left, QiushiItem right) 
     { 
      return left.CompareTo(right) > 0; 
     } 
     public static bool operator >=(QiushiItem left, QiushiItem right) 
     { 
      return left.CompareTo(right) >= 0; 
     } 

     private static DateTimeComparer _datetimeComparer; 
     public static IComparer DateTimeCompare 
     { 
      get 
      { 
       if (_datetimeComparer == null) 
        _datetimeComparer = new DateTimeComparer(); 
       return _datetimeComparer; 
      } 
     } 

     private class DateTimeComparer : IComparer 
     { 
      #region IComparer Members 
      int IComparer.Compare(object left, object right) 
      { 
       if (!(left is QiushiItem)) 
        throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "left"); 
       if (!(right is QiushiItem)) 
        throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "right"); 
       QiushiItem leftItem = (QiushiItem)left; 
       QiushiItem rightItem = (QiushiItem)right; 
       return leftItem.PublishDateTime.CompareTo(
       rightItem.PublishDateTime); 
      } 
      #endregion 

     } 

     public bool Equals(QiushiItem x, QiushiItem y) 
     { 
      if (x.Id == y.Id) 
       return true; 
      return false; 
     } 

     public int GetHashCode(QiushiItem obj) 
     { 
      return obj.Id.GetHashCode(); 
     } 
    } 

除了上面,我還使用自定義序列化嘗試

public override void Configure() 
     { 
      Id(x=>x.Section); 
      Property(x => x.QiushiItems).InAppSettings(); 
      Property(x => x.QiushiItems).InAppSettings().Configure(x => 
                     { 
                      x.Save = SaveStrokes; 
                      x.Restore = RestoreStrokes; 
                     }); 
     } 

     void SaveStrokes(QiushiListViewModel vm, Func<string> serialize, StorageMode nMode) 
     { 
      IsolatedStorageHelper.SaveObject(vm.Section, vm.QiushiItems); 
     } 

     void RestoreStrokes(QiushiListViewModel vm, Func<string> serialize, StorageMode nMode) 
     { 
      // use IsolatedStorageSettings.ApplicationSettings[vm.DisplayName + "ThePropertyKey"] 
      // to check if the key exists, and if it is there get the serialized data and deserialize 
      if (vm.Section == null) 
       return; 
      if(IsolatedStorageSettings.ApplicationSettings[vm.Section]!=null) 
       IsolatedStorageHelper.GetObject<IEnumerable<QiushiItem>>(vm.Section); 
     } 

然而,問題是,有5個QiushiListViewModels,立碑時,保存/恢復將被稱爲5次,我不知道哪個是哪個。

有什麼建議嗎?

在此先感謝-Peng

回答

相關問題