我正在使用Caliburn Micro 2.0.0-alpha2开发一个简单的Crud应用程序(一个Windows 8.1商店应用程序),但在视图模型之间进行导航并传递对象方面遇到了困难。
我多次阅读了Anders Gustafsson提出的解决方案,试图将其调整为我的范围,但对象始终为null。
我需要将listView中选择的单个对象传递给我的crudPage。 crudPage由一个用户控件组成,显示FormView。因此,我希望使用传递对象的值初始化此表单。
我认为问题是“参数”只有在视图模型创建后才被初始化,但我不知道如何解决这个问题。
以下是根据Anders Gustafsson的想法编写的代码:TransporterListViewModel(来自数据库的Transporters列表)
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(包含初始化用户控件的页面)
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);
}
运输器表单视图模型(用于初始化用户控件)
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; }
null
参数有关,这是我的观点。请查看TransporterListViewModel
类的SelectedItem
属性值。该值作为args
对象的参数传递给您的事件处理程序。 - Alberto SolanoTransporter tmp = new Transporter(); tmp = args.Parameter as Transporter; if (tmp != null) Debug.WriteLine(tmp.trName);
输出显示我在值之前看到异常(对象的值是正确的)。输出如下:“A first chance exception of type 'System.NullReferenceException' occurred in CrudSampleMVVM.exe Object reference not set to an instance of an object. marco”。 - ArghArgh