传递从视图模型变量另一种观点(MVVMCross)视图、变量、模型、观点

2023-09-13 00:00:50 作者:挂念 - hcs/u3

在过去的几个星期,我一直在致力于开发使用MVVMCross框架,一个跨平台的应用程序(IOS /安卓/ WP7)的。今天,我遇到了一个问题,我真的不知道该怎么解决,所以希望你可以把我在正确的方向。

For the past couple of weeks I've been working on developing a cross platform app (IOS/Android/WP7) using the MVVMCross framework. Today I ran into a problem I don't really know how to solve, so hopefully you can push me in the right direction.

在IOS我有以下建设导航到另一个页面(在code以下坐落在一个视图模型):

In the IOS I have the following construction for navigating to another page (the code below is located in a ViewModel):

KeyValuePair<string,string> kvpAct1 = new KeyValuePair<string, string>("short", ".countertest5");

public IMvxCommand BeckhoffActuator1
{
    get
    {           
        return new MvxRelayCommand<Type>((type) => this.RequestNavigate<Beckhoff.BeckhoffActuatorViewModel>(kvpAct1));
    }
}

在此IMvxCommand被触发(按钮pressed)下一个视图被加载,在这种情况下,BeckhoffActuatorViewModel。在BeckhoffActuatorView的code我用的是keyvaluepair从上面的:

When this IMvxCommand is fired (button pressed) the next View is loaded, in this case the BeckhoffActuatorViewModel. In the code of the BeckhoffActuatorView I use the keyvaluepair from above:

public class BeckhoffActuatorView : MvxTouchDialogViewController<BeckhoffActuatorViewModel>
{

    ICollection<string> icol;

    public BeckhoffActuatorView(MvxShowViewModelRequest request) : base(request, UITableViewStyle.Grouped, null, true)
    {

        icol = request.ParameterValues.Values;

    }

    public override void ViewDidLoad()
    {
        //Code
    }
}

此建设工作正常的IOS,但我想用同样的结构在我的Andr​​oid应用程序。

This construction is working fine in IOS, but I would like to use the same construction in my android App.

在code。在视图模型并没有改变,因为这是MVVM的整体思路。但BackhoffActuatorView的code是不同的Andr​​oid版本:

The code in the ViewModel hasn't changed since that's the whole idea of MVVM. But the code of the BackhoffActuatorView is different for Android:

public class BeckhoffActuatorView : MvxBindingActivityView<BeckhoffSensorViewModel>
{
    public ICollection<string> icol;

    public BeckhoffActuatorView()
    {
        Debug.WriteLine("Standard");
    }

    public BeckhoffActuatorView(MvxShowViewModelRequest request)
    {
        Debug.WriteLine("Custom");

        icol = request.ParameterValues.Values;
    }

    protected override void OnViewModelSet()
    {

        SetContentView(Resource.Layout.BeckhoffActuatorView);

    }
}

在code以上不工作时,MvxBindingActivityView似乎并没有实施类似的ViewController我在IOS使用的东西。在code只能来自于标准的构造函数,当我离开,一出完全不会编译/运行。

The code above isn't working, the MvxBindingActivityView doesn't seem to implement something similar to the ViewController I use in IOS. The code only come in the standard constructor, and when I leave that one out completely it won't compile/run.

有谁知道知道,我可以访问我送的RequestNavigate的keyvaluepair?谢谢!

Does anyone know know I can access the keyvaluepair I send with the RequestNavigate? Thank you!

推荐答案

MVVMCross是基于非常习惯 - 和它的作品上的ViewModels之间传递消息尽可能的想法

MVVMCross is very convention based - and it works on the idea of passing messages between ViewModels wherever possible.

如果您在使用导航到一个视图模型:

If you navigate to a ViewModel using:

KeyValuePair<string,string> kvpAct1 = new KeyValuePair<string, string>("short", ".countertest5");

public IMvxCommand BeckhoffActuator1
{
    get
    {           
        return new MvxRelayCommand<Type>((type) => this.RequestNavigate<Beckhoff.BeckhoffActuatorViewModel>(kvpAct1));
    }
}

那么你应该能够使用构造来挑选了在BeckhoffActuatorViewModel:

then you should be able to pick that up in the BeckhoffActuatorViewModel using the constructor:

public class BeckhoffActuatorViewModel : MvxViewModel
{
    public BeckhoffActuatorViewModel(string short)
    {
        ShortValue = short;
    }

    private string _shortValue;
    public string ShortValue
    {
        get
        {
            return _shortValue;
        }
        set
        {
            _shortValue = value;
            FirePropertyChanged("ShortValue");
        }
    }
}

和你的意见就可以访问 ViewModel.ShortValue (适用于iOS这可以base.ViewDidLoad(后进行),为Android的OnCreate(后)和WP7后的OnNavigatedTo )

And your views can then access ViewModel.ShortValue (for iOS this can be done after base.ViewDidLoad(), for Android after OnCreate() and for WP7 after OnNavigatedTo)

有关这方面的一个例子,看看在TwitterSearch例如:

For an example of this, take a look at the TwitterSearch example:

https://github.com/slodge/MvvmCrossTwitterSearch

这有HomeViewModel使用它调用导航:

This has a HomeViewModel which calls navigate using:

    private void DoSearch()
    {
        RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });
    }

和一个TwitterViewModel接收使用构造的搜索关键词:

and a TwitterViewModel which receives the searchTerm using the constructor:

    public TwitterViewModel(string searchTerm)
    {
        StartSearch(searchTerm);
    }

请注意,只有字符串,则允许此消息传递在present中 - 但可以使用JSON.Net一直连载自己的对象 - 或者您也可以扩展框架 - 它是开源

Please note that only strings are allowed in this message passing at present - but you can always serialise your own objects using JSON.Net - or you can extend the framework - it's open source.

请注意,只有字符串 S, INT S,布尔,则允许在这个构造函数的参数传递在present - 这是由于XAML的URL和Android的意图系列化的要求。如果你想用你自己的自定义序列化对象与导航试验,然后请参阅的http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html.

Please note that only strings, ints, doubles and bools are allowed in this constructor parameter passing at present - this is due to serialisation requirements for Xaml Urls and for Android Intents. If you want to experiment with navigation using your own custom serialised objects, then please see http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html.

另外,注意,如果你想使用匿名对象导航( RequestNavigate&LT; TwitterViewModel&GT;(新{搜索关键词= SearchText}); ),那么你就需要做确保了 InternalsVisibleTo 属性设置 - 见https://github.com/slodge/MvvmCrossTwitterSearch/blob/master/TwitterSearch.Core/Properties/AssemblyInfo.cs:

Also, note that if you want to use the anonymous object navigation (RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });) then you will need to make sure that an InternalsVisibleTo attribute is set - see https://github.com/slodge/MvvmCrossTwitterSearch/blob/master/TwitterSearch.Core/Properties/AssemblyInfo.cs:

[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]

另外...不适合胆小的人......这不是好MVVM code......但是,如果你真的想/需要访问一个Android活动内部的MvxShowViewModelRequest数据,然后你可以从收到的意向提取出来 - 有包含请求(见 CreateViewModelFromIntent 的deserialisation在https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs)

Further... not for the faint-hearted... and this isn't "good mvvm code"... but if you really want/need to access the MvxShowViewModelRequest data inside an Android activity, then you can extract it from the incoming Intent - there's an Extras string containing the request (see the deserialisation in CreateViewModelFromIntent in https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs)