我正在使用Caliburn Micro 2.0.0-alpha2开发一个简单的Crud应用程序(Windows 8.1商店应用程序)
我遇到了在视图模型之间导航,传递对象的问题。
我多次阅读
提出的解决方案Anders Gustafsson(How to pass parameter to navigated view model with 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<Transporter> _transporters;
public BindableCollection<Transporter> 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<TransporterCrudPageViewModel>(_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; }
答案 0 :(得分:0)
在构造函数TransporterCrudViewModel
中,您有:
this.InitializeTransporterForm(Parameter);
其中Parameter
是未初始化的Transporter
类型的属性,您将使用InitializeTransporterForm
参数调用方法null
。然后,您将使用参数SetUpForm
的空值调用Transporter t
方法。我认为你应该以某种方式初始化这个属性。
然后,假设您继续使用此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);
确保Parameter
对象的属性NavigationEventArgs
不为空。