2012-10-05 89 views
0

I將對象OptionSelector傳遞給不同的ViewModel。傳遞時它不是null。但在接收端顯示爲空。任何想法爲什麼傳遞對象爲null?


我的視圖被包裹在這個類中。

public class CoreView : XboxApplicationPage, IRefAppNavigationItem 
{ 
/// <summary> 
     /// Gets or sets navigation data for nested views. 
     /// </summary> 
     public object Data 
     { 
      get { return GetValue(DataProperty) as object; } 
      set { SetValue(DataProperty, value); } 
     } 

     /// <summary> 
     /// Identifies the Data dependency property. 
     /// </summary> 
     public static readonly DependencyProperty DataProperty = 
     DependencyProperty.Register("Data",typeof(object),typeof(CoreView),new     PropertyMetadata(null)); 

     /// <summary> 
     /// Gets or sets the viewModel used in this page. 
     /// </summary> 
     public CoreViewModel ViewModel 
     { 
      get 
      { 
       return (CoreViewModel)GetValue(ViewModelProperty); 
      } 
      set { SetValue(ViewModelProperty, value); } 
     } 

     /// <summary> 
     /// Sets the View.DataContext to the View.ViewModel. 
     /// </summary> 
     private void SetViewModel() 
     { 
      if (ViewModel != null) 
      { 
       try 
       { 
        if (this.Data != null) 
        { 
         ViewModel.Data = this.Data; 
        } 
        else 
        {  
         ViewModel.Data = this.Tag; 
        } 

        SetDataContext(); 
       } 
       catch(Exception e) 
       { 
        Logger.Log("SetViewModel() error :" + e.StackTrace); 
       } 
      } 
     } 
     /// <summary> 
     /// Sets the DataContext to the ViewModel. 
     /// Override when additional actions might be required after setting the DataContext. 
     /// </summary> 
     protected virtual void SetDataContext() 
     { 
      this.DataContext = ViewModel; 
     } 
     /// <summary> 
     /// Handles on NavigateTo events. 
     /// </summary> 
     /// <param name="e">Event args.</param> 
     /// <remarks>This method is used to get the post navigation data and integrate into CoreView.</remarks> 
     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
     { 
      if (DesignerProperties.IsInDesignTool) 
      { 
       return; 
      } 

      try 
      { 
       if (this.currentPageCulture != AmebaTV_XBOXApplication.Common.Resources.Localization.CurrentCulture) 
       { 
        UpdateLocalizedStrings(this); 
       } 

       Microsoft.Xbox.Controls.Localization.CultureChanged += Localization_CultureChanged; 

       var postNavigationState = IoC.Get<INavigationService>().GetPostNavigationData(NavigationContext); 
       if (postNavigationState != null) 
       { 
        this.Data = postNavigationState; 
       } 

       this.ViewModel.OnNavigatedTo(); 

       if (legendService != null) 
       { 
        legendService.IsNavigateBackEnabled = true; 
       } 
       base.OnNavigatedTo(e); 
      } 
      catch (Exception ex) 
      { 
       Logger.Log("OnNavigatedTo : "+ex.Message); 
      } 


     } 

} 

    /// <summary> 
    /// Base class for all ViewModels. 
    /// </summary> 
    public class CoreViewModel : ViewModelBase 
    { 
     /// <summary> 
     /// Field for Data. 
     /// </summary> 
     private object data; 

     /// <summary> 
     /// To be used with navigation to populate view models with initial content. 
     /// </summary> 
     public virtual void OnDataSet() 
     { 
     } 

     /// <summary> 
     /// Gets or sets ViewModel data. 
     /// </summary> 
     public object Data 
     { 
      get { return this.data; } 
      set 
      { 
       this.data = value; 
       RaisePropertyChanged("Data"); 
       OnDataSet(); 
      } 
     } 
} 

OptionSelectorData對象

/// <summary> 
    /// Contains data for the options selector view. 
    /// </summary> 
    public class OptionSelectorData 
    { 
     /// <summary> 
     /// Gets or sets the list of options. 
     /// </summary> 
     public IList<string> Options { get; set; } 

     /// <summary> 
     /// Gets or sets the option title. 
     /// </summary> 
     public string Title { get; set; } 

     /// <summary> 
     /// Gets or sets the callback that will be invoked when an option is selected 
     /// </summary> 
     public Action<string> NotificationCallback { get; set; } 
    } 
} 

命令觸發導航

public class MoreOverflowViewModel : CoreViewModel 
{ 


     /// <summary> 
     /// Navigate to Property filter page 
     /// </summary> 
     public ICommand gotoViewPagebyCriteria 
     { 
      get 
      { 
       return new RelayCommand(() => 
       { 
        OptionSelectorData option = new OptionSelectorData 
        { 
         Options = filterOptions, Title = 
         Localization.GetByLocalizationKey("OptionTitleFilter"), 
         NotificationCallback = OnFilterOptionsCallback 
        }; 

        Messenger.Default.Send(new NavigateToMessage(new 
        Uri(PageListings.ViewPageByCriteria, UriKind.Relative), option)); 
       }); 
      } 
     } 
    } 

視圖模型到REC eive數據,OnDataSet檢查對象並設置屬性

public class ViewByCriteriaViewModel : CoreViewModel 
    { 
      /// <summary> 
     /// ondataset 
     /// </summary> 
     public override void OnDataSet() 
     { 
      option = this.Data as OptionSelectorData; 
      if (option != null) 
      { 
       OptionTitle = option.Title; 
       itemsSource = option.Options; 
      } 
      else 
      { 
       Logger.Log("NULL Option Data"); 
      } 
     } 

} 

+1

誰在調用'OnDataSet'和什麼時候?誰應該將'this.Data'設置爲'OptionSelectorData'?請張貼更多的上下文! – nemesv

+0

您正在使用哪種MVVM框架? –

+0

看起來像MVVM Light。 –

回答

0

它似乎我有一個默認的OnNavigatedTo()重寫基類中的方法。問題解決了。

0

嘗試此。我在想,this.Data沒有得到你認爲它的對象。如果它是某種其他類型,那麼as將返回null。

public override void OnDataSet() 
{ 
    Logger.Log("this.Data = " + (this.Data == null ? "null" : this.Data.GetType().Name)); 

    option = this.Data as OptionSelectorData; 
    if (option != null) 
    { 
     OptionTitle = option.Title; 
     itemsSource = option.Options; 
    } 
    else 
    { 
     Logger.Log("NULL Option Data"); 
    } 
} 
+0

this.Data = null。所以數據確實是空的。 – Fabii

相關問題