MvvmCross iPhone和iPad最佳实践

时间:2017-08-30 17:03:57

标签: xamarin xamarin.ios mvvmcross

我对Xamarin.iOSMvvmCross有疑问。针对iPhone和iPad显示View的{​​{1}}具有相同ViewModelViews不同的最佳做法是什么?

如果我将相同的ViewModel添加到两个Views,我会收到以下异常:

  

MvvmCross.Platform.Exception.MvxException创建时出现问题   View-ViewModel查找表 - 您注册了多个View   对于相同的ViewModels

最佳做法是创建从第一个派生的另一个ViewModel,然后从View执行不同的逻辑,如果设备是iPhone或iPad吗?

2 个答案:

答案 0 :(得分:2)

It is not possible to create two $(document).on('submit','#create_node_group'(function() { // catch the form's submit alert("submit called"); $.ajax({ // create an AJAX call... data: $(this).serialize(), // get the form data type: $(this).attr('method'), // GET or POST url: $(this).attr('action'), // the file to call success: function(response) { // on success.. $('#content').html(response); // update the DIV } }); s for one Response.BufferOutput= true; . d = {} for k, v in coinvolume.items(): try: if float(v['BTC']) > 100: d[k] = v except KeyError: d[k] = v except TypeError: if v > 100: d[k] = k , just like other MVVM libraries, uses reflection to create a View of ViewModel and MvvmCross relationships. This is a one-to-one operation. If you try to register several Dictionarys for a single View in your container, a ViewModel will be thrown as you pointed out yourself.

The simplest way to circumvent this behaviour, is to create a new View (and a new ViewModel) which simply derives from the first MvxException.

ViewModel

After creating View, you can now create a new one, ViewModel which derives from the former:

public class ViewModelA
{
    public ViewModelA()
    { 
        // Constructor
    }

    // Properties, Methods, etc.

}

答案 1 :(得分:1)

你需要使用MvvmCross提供给你的MvxFormFactorSpecific属性,当你想要为一个viewModel做一些目标的iPad和iPhone时,如下所示

    [MvxFormFactorSpecific(MvxTouchFormFactor.TallPhone)]
    public class ForTallController : MainController {


        public override void ViewDidLoad() {
            base.ViewDidLoad();
        }
........
........


    }
        [MvxFormFactorSpecific(MvxTouchFormFactor.Pad)]
    public class ForiPadController : MainController {

        public override void ViewDidLoad() {
            base.ViewDidLoad();

        }
........
........
    }

    [MvxFormFactorSpecific(MvxTouchFormFactor.Phone)]
        public partial class MainController : MvxViewController {


            public MainController()
                : base("MainController", null) {

            }

    ......
    ......
    }