0

オブジェクト 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)); 
                });
            }
        }
    }

データを受け取る 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

基本クラスのメソッドをオーバーライドしていたデフォルトの OnNavigatedTo() があったようです。問題は解決された。

于 2012-10-05T20:23:04.913 に答える
0

これを試して。あなたが思っthis.Dataているオブジェクトを取得していないと思います。他のタイプの場合は、asnull を返します。

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 に答える