在MVVMCross中将参数从主要传递到详细信息 [英] Passing Parameter From Main to Detail in MVVMCross

查看:58
本文介绍了在MVVMCross中将参数从主要传递到详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所选项目从列表传递到详细信息视图,但是myitemDetailViewmodel中为空,即使它不在MyViewModel中.

I am trying to pass the selected item from the list to the detail view, but myitem is null in the DetailViewmodel even though it is not in the MyViewModel.

MyViewModel.cs

    public virtual ICommand ItemSelected
    {
        get
        {
            return new MvxCommand<MyViewModel>(item =>{SelectedItem = item;});
        }
     }

    public MyViewModel SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            // myItem is NOT null here!!!
            ShowViewModel<MyDetailViewModel>(new { date = Date, myItem = _selectedItem });
            RaisePropertyChanged(() => SelectedItem);
        }
    }

MyDetailViewModel.cs

public class  MyDetailViewModel: MvxViewModel
{
    private MyViewModel _myItem;

    public void Init(DateTime date, MyViewModel myItem = null)
    {
        // myItem is NULL here!!!
        _myItem = myItem;
    }
}

推荐答案

您可以使用参数对象,因为您只能传递一个参数.我通常为此创建一个嵌套类Parameter.

You can use a parameter object, because you can only pass one parameter. I usually crate a nested class Parameter for this.

public class  MyDetailViewModel: MvxViewModel
{
    private MyViewModel _myItem;

    public class Parameter
    {
        public DateTime Date {get; set; }
        public string Name {get; set;}
    }

    public void Init(Parameter param)
    {
        Name = param.Name;
    }
}

并显示如下视图模型:

ShowViewModel<MyDetailViewModel>(new MyDetailViewModel.Parameter { Date = Date, Name = _selectedItem.Name });

但是要注意!

由于某些平台问题,参数不能太复杂.您可能只需要在Parameter对象中传递项目的Id,然后在Init函数中加载MyItem.或者,您仅传递一个字符串并使用序列化: https://stackoverflow.com/a/19059938/1489968

But be aware!

The paramters cannot be complex due certain platform issues. You might have to pass only the Id of your Item within the Parameter object and then load MyItem in your Init function. Or you pass only a string and use serialization: https://stackoverflow.com/a/19059938/1489968

这篇关于在MVVMCross中将参数从主要传递到详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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