MVVMCross传递值到具有2个构造函数的ViewModel [英] MVVMCross Passing values to ViewModel that has 2 constructors

查看:186
本文介绍了MVVMCross传递值到具有2个构造函数的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 ViewModels ConfigurationViewModel EditConfigurationViewModel )。在 ConfigurationViewModel 中,我有以下代码:

I´ve got 2 ViewModels (ConfigurationViewModel and EditConfigurationViewModel). In the ConfigurationViewModel I've got the following code:

    public ConfigurationViewModel()
    {
        NewConfigCommand = new MvxRelayCommand(DoNewConfig);
        EditConfigCommand = new MvxRelayCommand<ConfigurationSet>(DoEditConfig);
    }

    private void DoNewConfig()
    {
        this.RequestNavigate<EditConfigurationViewModel>();
    }

    private void DoEditConfig(ConfigurationSet config)
    {
        this.RequestNavigate<EditConfigurationViewModel>(new { id = config.Id.ToString() });
    }



在EditConfigurationViewModel中,我有以下代码:

In the EditConfigurationViewModel I've got the following code:

    public EditConfigurationViewModel()
    {
        Configuration = new ConfigurationSet();
    }

    public EditConfigurationViewModel(string id)
    {
        Configuration = ConfigDataStore.GetConfiguration(Guid.Parse(id));
    }

我想实现的是非常简单的...在 ConfigurationViewModel NewConfigCommand 被触发时,我想导航到 EditConfigurationViewModel ,并使用无参参数的构造函数。当 EditConfigCommand 被触发时,我想使用接收 string 的构造函数。

What I want to achieve is something very simple... In the ConfigurationViewModel when the NewConfigCommand is fired, I want to navigate to the EditConfigurationViewModel, and use the parameterless constructor. When the EditConfigCommand is fired I want to use the constructor that receives a string.

这个代码的问题是,无论使用什么命令,无参数构造函数都会被使用,并且代码不会到达另一个构造函数。

The problem with this code is that no matter what command is fired, the parameterless constructor is allways used and the code never reaches the other constructor.

我做了一些实验,通过删除无参数的构造函数,结果是另一个构造函数被调用,我得到预期的结果 EditConfigurationCommand ,但如果我尝试fire NewConfigurationCommand 一个异常也是由于一个无参数的构造函数的inesxistence(到目前为止很好)。

I did some experiments, by removing the parameterless constructor, and the result was that the other constructor is called and I get the expected result for the EditConfigurationCommand, but if I try to fire the NewConfigurationCommand an exception is throw due too the inesxistence of a parameterless constructor (so far so good).

不幸的是,在这一刻,我没有安装VS2010,所以我不能通过PCL代码调试...我做了一些眼调试,发现这个类 MvxViewModelLocator 。我认为问题在这里。可能在 DoLoad 方法中尝试获取 MethodInfo ...

Unfortunately, at this moment I don't have VS2010 installed, so I'm not able to debug through PCL code... I've done some "eye debug" and found this class MvxViewModelLocator. I think the problem is somewhere here. Maybe in the DoLoad method when it tries to get the MethodInfo...

在这一点上,我只是想知道我做错了什么,或者这是预期的结果。同时我想我会安装VS2010的机会,并祈祷它不会破坏任何东西...

At this point I just wanted to know if I'm doing something wrong or if this is the expected result. Meanwhile I think I'll take a chance on installing VS2010 and pray that it won´t break anything...

推荐答案

PCL调试问题,为什么不只是添加一个Win8或WP7 / 8 UI - 然后你可以通过PCL代码调试...

On the PCL debugging issue, why not just add a Win8 or WP7/8 UI - then you can debug through the PCL code...

关于主要问题 - 如何使用多个构造函数...我建议您不要

On the main question - about how to use multiple constructors... I'd suggest you don't.

我,编辑和新的是两个不同的视图和两个不同的viewmodels - 他们可能共享公共属性和通用布局 - 但这可以使用继承,使用UserControls,使用 include ,等等。

For me, edit and new are two different views and two different viewmodels - they may share common properties and common layout - but this can be achieved using inheritance, using UserControls, using include axml, etc.

有关我通常用于new和edit的示例,请参阅 https://github.com/slodge/MvvmCross/tree/vnext/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement/ViewModels

For an example of what I generally use for new and edit see https://github.com/slodge/MvvmCross/tree/vnext/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement/ViewModels

如果你坚持使用一个viewmodel,那么你可以考虑使用'magic value'新的 - 例如如果通过Guid.Empty,则意味着新的?

If you do insist on carry on using one viewmodel, then you could consider using a 'magic value' for New - e.g. if Guid.Empty is passed then that means new?

或者,你可以删除无参数的构造函数,默认值为第二个:

Alternatively, you could just drop your parameterless constructor and could add a default value to the second one:

public EditConfigurationViewModel(string id = null)
{
    Guid value;
    if (id == null || !Guid.TryParse(id, out value))
    {
        Configuration = new ConfigurationSet();
    }
    else
    {
        Configuration = ConfigDataStore.GetConfiguration(value);
    }
}

我认为这会有效?

最后,如果没有一个适合你,那么你可以考虑重写ViewModel构造机制。

Finally, if none of that seems suitable to you, then you could consider overriding the ViewModel construction mechanism.

为了帮助这一点,最近有一篇有关如何为MvvmCross编写自己的默认ViewModelLocator的相关详细信息 - 请参阅 http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

To help with this, there's a fairly detailed recent post on how to write your own default ViewModelLocator for MvvmCross - see http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

使用这种方法,您可以创建更多的自定义导航模型 - 或者如果这是唯一的特殊视图模型,那么我怀疑您可以创建一个默认的viewModelLocator:

Using this approach, you could create a much more custom navigation model - or if this is the only special view model, then I suspect you could create a default viewModelLocator like:

public class MyViewModelLocator
    : MvxDefaultViewModelLocator
{
    public override bool TryLoad(Type viewModelType, IDictionary<string, string> parameterValueLookup,
                                 out IMvxViewModel model)
    {
        if (viewModelType == typeof(EditConfigurationViewModel))
        {
            string id;
            if (parameterValueLookup.TryGetValue("id", out id))
            {
                model = new EditConfigurationViewModel(id);
            }
            else
            {
                model = new EditConfigurationViewModel();
            }
            return true;
        }
        return base.TryLoad(viewModelType, parameterValueLookup, IMvxViewModel model);
    }
}

并在App.cs中注册该定位器: / p>

and register that locator in App.cs using:

protected override IMvxViewModelLocator CreateDefaultViewModelLocator()
{
     return new MyViewModelLocator();
}

这篇关于MVVMCross传递值到具有2个构造函数的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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