Caliburn Micro:在ViewModel之间传递Object
我正在使用Caliburn Micro 2.0.0-alpha2开发一个简单的Crud应用程序(Windows 8.1商店应用程序)
我在视图模型之间导航,传递对象时遇到麻烦。
我多次阅读提出的解决方案
Anders Gustafsson( 如何使用WinRT Caliburn.Micro将参数传递给导航视图模型? )
我试着让它适应我的范围。 但是这个对象是空的。
我需要将从listView中选择的单个对象传递给我的crudPage。 crudPage由显示FormView的userControl组成。 所以我想用传递对象的值初始化这个Form。
我认为问题是只有在创建ViewModel之后才初始化“参数”,但我不知道如何解决这个问题。
根据Anders Gustafsson的想法,有我的代码
TransporterListViewModel(来自数据库的传输器列表)
public class TransporterListViewModel : ViewModelBase { public string Title { get; set; } public TransporterListViewModel(INavigationService navigationService) : base(navigationService) { LoadData(); } public async void LoadData() { _transporters = await TransporterService.GetAll(); } private BindableCollection _transporters; public BindableCollection Transporters { get { return this._transporters; } set { this._transporters = value; NotifyOfPropertyChange(() => this.Transporters); } } private Transporter _selectedItem; public Transporter SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyOfPropertyChange(() => this.SelectedItem); navigationService.Navigated += NavigationServiceOnNavigated; navigationService.NavigateToViewModel(_selectedItem;); navigationService.Navigated -= NavigationServiceOnNavigated; } } private static void NavigationServiceOnNavigated(object sender, NavigationEventArgs args) { FrameworkElement view; TransporterCrudPageViewModel transporterCrudPageViewModel; if ((view = args.Content as FrameworkElement) == null || (transporterCrudPageViewModel = view.DataContext as TransporterCrudPageViewModel) == null) return; transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter as Transporter); }
TransporterCrudViewModel(将UserControl加入初始化的页面)
public class TransporterCrudPageViewModel : ViewModelBase { public string Title { get; set; } public Transporter Parameter { get; set; } public TransporterFormViewModel TransporterFormVM { get; set; } public async void InitializeTransporterForm(Transporter enumerable) { TransporterFormVM = new TransporterFormViewModel(navigationService, enumerable); await SetUpForm(enumerable); } public async Task SetUpForm(Transporter t){ TransporterFormVM.trName = t.trName; TransporterFormVM.trUrl = t.trUrl; } public TransporterCrudPageViewModel(INavigationService navigationService) : base(navigationService) { Title = "TransporterCrud Page"; //this.navigationService = navigationService; this.InitializeTransporterForm(Parameter); }
TransporterFormViewModel(用于初始化的userContol)
public class TransporterFormViewModel :ViewModelBase { public string Title { get; set; } public Transporter Transporter { get; set; } public TransporterFormViewModel(INavigationService navigationService,Transporter trans) : base(navigationService) { Transporter = trans; } private string _trName; public string trName { get { return _trName; } set { _trName = value; NotifyOfPropertyChange(() => trName); } } public string trCode { get; set; } public string trUrl { get; set; } public int trId { get; set; }
在构造函数TransporterCrudViewModel
类中,您有:
this.InitializeTransporterForm(Parameter);
其中Parameter
是未初始化的Transporter
类型的属性,您将使用null
参数调用InitializeTransporterForm
方法。 然后,您将使用参数Transporter t
的空值调用SetUpForm
方法。 我认为你应该以某种方式初始化这个属性。
然后,假设您使用以下内容继续使用TransporterListViewModel
类:
transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter as Transporter);
在InitializeTransporterForm
方法中,不要将传递的参数设置为属性Parameter
值,如下所示:
public async void InitializeTransporterForm(Transporter enumerable) { TransporterFormVM = new TransporterFormViewModel(navigationService, enumerable); this.Parameter = enumerable; //setting the Parameter property.. await SetUpForm(enumerable); }
除了这些注释之外,您还应该在IDE中添加一个断点
transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter as Transporter);
确保NavigationEventArgs
对象的属性Parameter
不为null。