0

我将对象 OptionSelector 传递给不同的 ViewModel。通过时它不为空。但在接收端它显示为空。有任何想法吗


我的视图包含在这个类中。

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

Viewmodel 接收数据,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");
            }
        }

}

4

2 回答 2

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");
    }
}
于 2012-10-05T15:54:52.643 回答
0

似乎我有一个默认的 OnNavigatedTo() 覆盖了基类中的方法。问题解决了。

于 2012-10-05T20:23:04.913 回答