卡利微:通过视图模型之间的对象 [英] Caliburn Micro : passing Object between ViewModel
问题描述
我正在开发使用微卡利2.0.0-α2
我在麻烦的ViewModels之间的导航一个简单的CRUD应用程序(在Windows 8.1存储应用程序) ,传递对象。
我读到
提出的解决方案多次
安德斯古斯塔夫松(如何参数传递到导航的鉴于与WinRT的Caliburn.Micro?)
块引用>
和我的模型试图将其适应范围我。
,而对象是alwais空。
我需要通过从一个ListView我crudPage选定一个对象。
的crudPage是由表示FormView控件的用户控件组成。
所以我想初始化此形式,传递的对象的值。
我认为,问题是,参数创建视图模型后才被初始化,但我不知道如何解决这个问题。
有是我的代码,根据安德斯·古斯塔夫松的想法
TransporterListViewModel(从数据库转运的列表)
公共类TransporterListViewModel:ViewModelBase
{
公共字符串名称{搞定;组; }
公共TransporterListViewModel(INavigationService的NavigationService)
:底座(的NavigationService)
{
LoadData();
}
公共异步无效LoadData(){
_transporters =等待TransporterService.GetAll();
}
私人BindableCollection<转运GT; _transporters;
公共BindableCollection<转运GT;转运
{
得到
{
返回this._transporters;
}
组
{
this._transporters =价值;
NotifyOfPropertyChange(()=> this.Transporters);
}
}
私人运输车_selectedItem;
公共运输车的SelectedItem
{
得到
{
返回_selectedItem;
}
组
{
_selectedItem =价值;
NotifyOfPropertyChange(()=> this.SelectedItem);
navigationService.Navigated + = NavigationServiceOnNavigated;
navigationService.NavigateToViewModel< TransporterCrudPageViewModel>(_将selectedItem;);
navigationService.Navigated - = NavigationServiceOnNavigated;
}
}
私有静态无效NavigationServiceOnNavigated(对象发件人,NavigationEventArgs参数)
{
FrameworkElement的观点;
TransporterCrudPageViewModel transporterCrudPageViewModel;
如果((查看= args.Content为FrameworkElement的)== NULL ||
(transporterCrudPageViewModel = view.DataContext为TransporterCrudPageViewModel)== NULL)回报;
transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter作为转运);
}
TransporterCrudViewModel(该页面cointains的用户控件初始化)
公共类TransporterCrudPageViewModel:ViewModelBase
{
公共字符串名称{搞定;组; }
公共运输车参数{搞定;组; }
公共TransporterFormViewModel TransporterFormVM {搞定;组; }
公共异步无效InitializeTransporterForm(转运枚举)
{
TransporterFormVM =新TransporterFormViewModel(的NavigationService,枚举);
等待SetUpForm(枚举);
}
公共异步任务SetUpForm(转运T){
TransporterFormVM.trName = t.trName;
TransporterFormVM.trUrl = t.trUrl;
}
公共TransporterCrudPageViewModel(INavigationService的NavigationService)
:底座(的NavigationService)
{
标题=TransporterCrud页;
//this.navigationService =的NavigationService;
this.InitializeTransporterForm(参数);
}
TransporterFormViewModel(该userContol初始化)
公共类TransporterFormViewModel:ViewModelBase
{
公共字符串名称{搞定;组; }
公共运输车转运{搞定;组; }
公共TransporterFormViewModel(INavigationService的NavigationService,转运反式)
:底座(的NavigationService)
{
运输车=反;
}
私人字符串_trName;
公共字符串段trname
{
得到
{
返回_trName;
}
组
{
_trName =价值;
NotifyOfPropertyChange(()=>段trname);
}
}
公共字符串trCode {搞定;组; }
公共字符串trUrl {搞定;组; }
公众诠释TRID {搞定;组; }
解决方案在构造函数中
TransporterCrudViewModel
类有:this.InitializeTransporterForm(参数);
其中,
参数
是类型的属性运输车
没有初始化,你会调用该方法InitializeTransporterForm
与空
参数。然后你会打电话给SetUpForm
方法,参数运输车牛逼
的空值。我想你应该以某种方式这个属性初始化。
然后,假设你在你的
TransporterListViewModel
类继续与此:transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter作为转运);
在方法
InitializeTransporterForm
,你不t将传递的参数作为属性的值参数
的东西是这样的:公共异步无效InitializeTransporterForm(转运枚举)
{
TransporterFormVM =新TransporterFormViewModel(的NavigationService,枚举);
this.Parameter =枚举; //设置参数属性..
等待SetUpForm(枚举);
}
除了这些笔记,你应该把断点与你的IDE中的行
transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter作为转运);
确认该财产
参数
的NavigationEventArgs
对象不为null。I'm developing a simple Crud Application (a windows 8.1 store application) using Caliburn Micro 2.0.0-alpha2
I'm in trouble with navigation between viewmodels, passing object.
I read many times the solution proposed by
Anders Gustafsson (How to pass parameter to navigated view model with WinRT Caliburn.Micro?)
and i tried to adapt it to my scope. But the object is alwais null.
I need to pass a single object selected from a listView to my crudPage. The crudPage is composed by an userControl that shown the FormView. So i want to initialize this Form, with the values of the passed object.
I think that the problem is that the "Parameter" is initialized only after the ViewModel is created, but i don't know how to fix that problem.
There is my code, according with the idea of Anders Gustafsson
TransporterListViewModel (a list of Transporters from Database)
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 (the page that cointains the UserControl to initialize)
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 (the userContol to initialize)
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; }
解决方案In the constructor
TransporterCrudViewModel
class you have:this.InitializeTransporterForm(Parameter);
where
Parameter
is a property of typeTransporter
not initialized and you will call the methodInitializeTransporterForm
with anull
parameter. Then you'll callSetUpForm
method with a null value of the parameterTransporter t
. I think you should initialize in some way this property.Then, supposing you're continuing in your
TransporterListViewModel
class with this:transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter as Transporter);
in the method
InitializeTransporterForm
, you don't set the passed parameter as value of the propertyParameter
with something like this:public async void InitializeTransporterForm(Transporter enumerable) { TransporterFormVM = new TransporterFormViewModel(navigationService, enumerable); this.Parameter = enumerable; //setting the Parameter property.. await SetUpForm(enumerable); }
Beside these notes, you should put a breakpoint with your IDE in the line
transporterCrudPageViewModel.InitializeTransporterForm(args.Parameter as Transporter);
Make sure that the property
Parameter
of theNavigationEventArgs
object is not null.这篇关于卡利微:通过视图模型之间的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!