之間對象我正在開發使用卡利微2.0.0-α-2卡利微:通過視圖模型
我在用的ViewModels之間的導航麻煩,傳遞對象的簡單的CRUD應用程序(在Windows 8.1存儲應用程序)。
我看了很多遍提出
安德斯·古斯塔夫鬆(How to pass parameter to navigated view model with WinRT Caliburn.Micro?)
的解決方案,我想它適應我的範圍。 但該對象是alwais null。
我需要將從listView中選擇的單個對象傳遞給我的crudPage。 crudPage由顯示FormView的userControl組成。 所以我想用傳遞對象的值初始化這個表單。
我認爲問題是隻有在創建ViewModel後才初始化「參數」,但我不知道如何解決這個問題。
有我的代碼,安德斯·古斯塔夫鬆的想法
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(即cointains的用戶控件初始化頁面)
根據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; }
所以,問題不是參數,參數不爲null,但是當設置函數被調用'await SetUpForm(enumerable)'我得到了這個消息異常時>對象引用沒有設置爲對象的一個實例 「 – ArghArgh
@ArghArgh這個實現有些東西我不明白,我認爲它們與'null'參數有關,這是我的觀點。看看TransporterListViewModel類的'SelectedItem'屬性的值。該值作爲事件處理程序的'args'對象的參數傳遞。 –
我已經插入frew行代碼,顯示對象的一些屬性Transporter'Transporter tmp = new Transporter(); tmp = args.Parameter as Transporter; (tmp!= null) Debug.WriteLine(tmp.trName);'在輸出值(對象的值是正確的)之前,輸出顯示異常「輸出的第一個機會異常」 .NullReferenceException'發生在CrudSampleMVVM.exe 未設置對象實例的對象引用 marco「 – ArghArgh