将参数传递给mvvmcross视图模型时出错 [英] Error passing parameters to mvvmcross viewmodels

查看:85
本文介绍了将参数传递给mvvmcross视图模型时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在畅销书范例中找到了这个例子

I found this example in the Bestseller exmaple

public ICommand ViewDetailCommand
{
    get { return new MvxRelayCommand(() => RequestNavigate<BookViewModel>(new { category= CategoryEncoded, book=ISBN })); }
}

public BookViewModel(string category = null, string book = null)
{
    category = category ?? string.Empty;
    book = book ?? string.Empty;

    AsyncFindBook(category, book);
 }

所以我尝试做

public IMvxCommand GetGpsCommand
{
    get
    {
        return new MvxRelayCommand<SetupViewModel>(type => RequestNavigate<GpsViewModel>(new {installedMeter = _installedMeter}));
    }
}

public GpsViewModel(InstalledMeter installedMeter = null)
{
    _installedMeter = installedMeter;

    Latitude = 0.0;
    Longitude = 0.0;
    ButtonStartReading = "Start";

    this.GetService<IGpsService>().CoordinatesFoundEvent += CoordinatesFound;
    //this.GetService<IGpsService>().StartReading();
}

但是当我尝试此操作时,我会得到

But when I try this I just get

Cirrious.MvvmCross.Exceptions.MvxException:无法加载ViewModel 定位器中用于类型Core.ViewModels.InstallUnit.GpsViewModel的方法 MvxDefaultVi…

Cirrious.MvvmCross.Exceptions.MvxException: Failed to load ViewModel for type Core.ViewModels.InstallUnit.GpsViewModel from locator MvxDefaultVi…

推荐答案

更新:对于MvvmCross,此答案不再是最新的

Update: this answer is no longer up-to-date for MvvmCross

MvvmCross现在支持int,long,enum,double和double.此外,您可以根据以下建议使用自己的可序列化类型进行导航: http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

MvvmCross now supports ints, longs, enums, doubles and strings. Further, you can navigate by your own serializable types using the advice on: http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

原始答案:

这是一个经常遇到的错误.

This is an error that is often encountered.

Mvvm跨导航参数必须为字符串

之所以这样做,是因为导航本身需要序列化为WP7/8上的Xaml Url和Android上的Intent.

The reason for this is because the navigation itself needs to be serialized to a Xaml Url on WP7/8 and to an Intent on Android.

有关更多信息,请参见:

For more info, see:

有关更常见的导航问题,请查看以下导航部分: http: //slodge.blogspot.co.uk/p/mvvmcross-quicklist.html

For more frequent navigation problems, check out the Navigation section on: http://slodge.blogspot.co.uk/p/mvvmcross-quicklist.html

如果您认为应支持更多类型,则有一个未解决的问题请求- https://github.com/slodge/MvvmCross/issues/45

If you believe more types should be supported then there is an open issue request - https://github.com/slodge/MvvmCross/issues/45

对于仅用于数据的小对象,您可以将它们序列化为文本,然后通过此构造函数机制传递它们.唯一的限制实际上是Xaml Urls的大小限制(这些限制可能会变得很小).

For small data-only objects, you can serialize them to text and pass them through this constructor mechanism. The only limits really are the size limits placed on Xaml Urls (these can get quite small).

但是,在大多数情况下,我希望viewModel-viewModel导航将传递某种查找键-用于持久服务. MvvmCross可以帮助导航,但是应用程序开发人员需要了解其操作系统中每个应用程序的生命周期.

However, for most cases, I would expect that viewModel-viewModel navigation will pass some kind of lookup key - using to a persistent-service. MvvmCross can help with the navigation, but it the app developer needs to understand the lifecycle of each app within its OS.

这篇关于将参数传递给mvvmcross视图模型时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆