我試圖使用與MvvmCross一個TabActivity但我正在逐漸的框架代碼一個NullReferenceException因爲viewModelLoader傳遞到OnViewCreate使用的是空如何使用MonoDroid的TabActivity MvvmCross框架
namespace Cirrious.MvvmCross.ExtensionMethods
{
public static class MvxViewExtensionMethods
{
public static void OnViewCreate<TViewModel>(this IMvxView<TViewModel> view, Func<TViewModel> viewModelLoader)
where TViewModel : class, IMvxViewModel
{
if (view.ViewModel != null)
return;
var viewModel = viewModelLoader();
viewModel.RegisterView(view);
view.ViewModel = (TViewModel)viewModel;
}
我懷疑這是因爲我試圖加載視圖,而不是通過ViewModel。我的TabHost Activity中的代碼如下所示:
[Activity(Label = "TabHost")]
public class TabHostView : MvxBindingTabActivityView<TabHostViewModel>
{
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Page_TabHostView);
var tabHostWidget = this.TabHost;
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent(this, typeof(HomeView));
intent.AddFlags(ActivityFlags.NewTask);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHostWidget.NewTabSpec("home");
spec.SetIndicator("Home", Resources.GetDrawable(Resource.Drawable.icon_home));
spec.SetContent(intent);
tabHostWidget.AddTab(spec);
//... more tabs
我將如何解決此問題?
此外,我的ViewModels設置,以便TabHostViewModel具有每個標籤頁ViewModel的屬性。這是懶惰的,因爲它們只在調用屬性的get訪問器時才從模型中獲取數據。
因此,如果我有我的標籤頁axml佈局的數據綁定,大概路徑必須假定TabHostViewModel是上下文(根)?
非常感謝, 傑森
太棒了。這與使用CreateIntentFor一樣簡單。我使用ViewModels作爲返回時間字符串的標籤頁來證明他們正在被加載 - 它們是什麼。但是,當我在選項卡之間滑動時,時間不會被刷新(即,當您重新訪問選項卡頁時,bindiung不會再次發生)。你認爲什麼是解決這個問題的最好方法?再次感謝! –
2012-04-20 11:17:01
這是一個不同的問題 - 這是一個普通的mvvm! – Stuart 2012-04-20 11:30:36
Stackoverflow讓我懶! :( 爲了回答我自己以利於他人,我向ViewModel添加了一個Refresh()方法,並在View中從OnResume()中調用它,以便每次查看該選項卡時都會調用它。Refresh會更新ViewModel屬性需要調用FirePropertyChanged()。 – 2012-04-20 12:55:53