2

下载最新版本的 mvvmcross (V3) 后,我有一些工作要做,将我的一些项目升级到新的状态。我无法完成的最后一件事是将参数从 tabhost 传递给 viewmodel。在旧版本中它工作正常(但它不同),现在我得到了一个错误。

但首先这里的代码(第 19 行有问题(在代码中查看注释),第 18 行有效,但只有没有参数):

[Activity]
public class MainActivity : MvxTabActivity
{
    public new MainViewModel ViewModel
    {
        get { return (MainViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    protected override void OnViewModelSet()
    {
        SetContentView(Resource.Layout.Main);

        TabHost.TabSpec spec;
        Intent intent;

        spec = TabHost.NewTabSpec("listeaktiv");
        spec.SetIndicator(App.indicatorActive, Resources.GetDrawable(Resource.Drawable.green));
        //spec.SetContent(this.CreateIntentFor(ViewModel.ListViewModel)); -> It works (But only without Parameters! How could I pass them here?)
        spec.SetContent(this.CreateIntentFor<ListViewModel>(new { parameter = App.indicatorActive })); //Exception (on the next Line)
        TabHost.AddTab(spec);
    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    }
}

(App.indicatorActive 是我要传递的参数,它在 App.cs 中:(public static string indicatorActive = "Active";

我的 ListViewModel 看起来像这样:

public class ListViewModel : BaseViewModel
{
        public ListViewModel(string parameter)
        {

        }
}

错误:

Unhandled Exception:
Cirrious.CrossCore.Exceptions.MvxException: Failed to load ViewModel for type 
INApplikationsMonitor.Core.ViewModels.ListViewModel from locator MvxDefaultViewModelLocator
4

1 回答 1

1

我的猜测是,这只是因为您使用的是旧的 ViewModel 生命周期。

在 v3 中:

  • ViewModel 构造函数参数用于 IoC - 用于服务的依赖注入。
  • for passing parameters you need to instead use an Init method within the ViewModel

For more on this, see: http://slodge.blogspot.co.uk/2013/03/v3-new-viewmodel-lifecycle.html :

The default ViewModelLocator in v3 builds new ViewModel instances using a 4-step process - CIRS:

  • Construction - using IoC for Dependency Injection
  • Init() - initialisation of navigation parameters
  • ReloadState() - rehydration after tombstoning
  • Start() - called when initialisation and rehydration are complete
于 2013-04-24T11:05:04.850 回答