MVVMCross:可以将Storyboard与ICommand导航一起使用吗? [英] MVVMCross: Is it possible to use Storyboard with ICommand navigation?

查看:92
本文介绍了MVVMCross:可以将Storyboard与ICommand导航一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我一直在努力寻找解决警告的方法:

I've spent the last few days trying to figure out a way to get around the warning:

mvx:警告:屏蔽了8.93异常MissingMethodException:找不到类型FCX.iOS.TimesheetView的默认构造函数 在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:326中的System.Activator.CreateInstance(System.Type类型,布尔非公共)[0x00094] 在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:222中的System.Activator.CreateInstance(System.Type类型)[0x00000]处 在Cirrious.MvvmCross.Touch.Views.MvxTouchViewsContainer.CreateView(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest请求)[0x00000]在:0中 在Cirrious.MvvmCross.Touch.Views.MvxCanCreateTouchViewExtensionMethods.CreateViewControllerFor(IMvxCanCreateTouchView视图,Cirrious.MvvmCross.ViewModels.MvxViewModelRequest请求)[0x00000]在:0中 在Cirrious .MvvmCross.Touch.Views.Presenters.MvxTouchViewPresenter.Show(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest请求)[0x00000] in:0 在Cirrious.MvvmCross.Touch.Views.MvxTouchViewDispatcher +<> c__DisplayClass4.b__3()[0x00000]在:0中 在Cirrious.CrossCore.Core.MvxMainThreadDispatcher.ExceptionMaskedAction(System.Action action)[0x00000] in:0

mvx: Warning: 8.93 Exception masked MissingMethodException: Default constructor not found for type FCX.iOS.TimesheetView at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00094] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:326 at System.Activator.CreateInstance (System.Type type) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:222 at Cirrious.MvvmCross.Touch.Views.MvxTouchViewsContainer.CreateView (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in :0 at Cirrious.MvvmCross.Touch.Views.MvxCanCreateTouchViewExtensionMethods.CreateViewControllerFor (IMvxCanCreateTouchView view, Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in :0 at Cirrious .MvvmCross.Touch.Views.Presenters.MvxTouchViewPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in :0 at Cirrious.MvvmCross.Touch.Views.MvxTouchViewDispatcher+<>c__DisplayClass4.b__3 () [0x00000] in :0 at Cirrious.CrossCore.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action action) [0x00000] in :0

似乎导航正在寻找此构造函数:

It appears the navigation is looking for this constructor:

    public TimesheetView ()
    {
    }

当我想要使用此构造函数(用于情节提要)时:

When I WANT it to use this constructor (for storyboard purposes):

    public TimesheetView (IntPtr handle) : base (handle)
    {
    }

我一直试图将一个视图中的按钮绑定到以下ICommand:

I've been trying to bind a button from one view to the following ICommand:

    private MvxCommand _NewTimesheetCommand;
    public ICommand NewTimesheetCommand
    {
        get
        {
            _NewTimesheetCommand =  _NewTimesheetCommand ?? new MvxCommand(() => {
                ShowViewModel<TimesheetViewModel>(new TimesheetViewModel.Parameters { Mode = TimesheetViewModel.ModeEnum.ADD });
            });
            return _NewTimesheetCommand;
        }
    }

在查看了许多StackOverFlow问题之后,我遇到了这个问题,但是听起来似乎只有使用情节提要板才能要求我使用segue(换句话说,无法使用ICommand来更改我的视图).

After looking at many StackOverFlow questions, I came across this question, but it almost sounds that using storyboards REQUIRES me to use segues (in other words there isn't a way to use ICommand to change my view).

MvvmCross支持情节提要板

我的主要问题:是否可以使用情节提要板并使用viewmodel更改视图? (而不是使用视图中的segues)

My main question: Is it possible to use storyboards and change the view with the viewmodel? (rather than using segues from the view)

推荐答案

嗯,我刚刚实现了Stuart所说的话.效果很好. (请记住,使用视图模型中的ShowViewModel进行导航时,segue毫无用处)

Hmmm I just implemented what Stuart said. Works great. (Do remember that segue's are useless this way you navigate using ShowViewModel inside the viewmodels)

using System;
using Cirrious.MvvmCross.Touch.Views.Presenters;
using MonoTouch.UIKit;
using MonoTouch.Foundation;     
using Cirrious.MvvmCross.Touch.Views;
using Cirrious.MvvmCross.ViewModels;

 namespace DigitalCatalog.UI.Touch
 {
   public class StoryBoardTouchViewPresenter : MvxTouchViewPresenter
  {
    public static UIStoryboard Storyboard = null;


    public StoryBoardTouchViewPresenter (UIApplicationDelegate applicationDelegate, UIWindow window, string storyboardName, NSBundle StoryboardBundleOrNull = null) : base(applicationDelegate, window)
    {
        Storyboard = UIStoryboard.FromName (storyboardName, StoryboardBundleOrNull);
    }

    public override void Show (IMvxTouchView view)
    {
        MvxViewController sbView = null;

        try{
            sbView = (MvxViewController) Storyboard.InstantiateViewController (view.Request.ViewModelType.Name.Replace("Model", ""));
        }catch(Exception e){
            Console.WriteLine ("Failed to find storyboard view, did you forget to set the Storyboard ID to the ViewModel class name without the Model suffix ?" + e);
        }

        sbView.Request = view.Request;

        base.Show (sbView);
    }

}
 }

您现在要做的就是像这样设置AppDelegate:

All you have to do now is set the AppDelegate like this :

public override void FinishedLaunching (UIApplication application)
    {


        StoryBoardTouchViewPresenter sbPresenter = new StoryBoardTouchViewPresenter(this, Window,"MainStoryboard_iPad");


        var setup = new Setup(this,sbPresenter);
        setup.Initialize();

        var startup = Mvx.Resolve<IMvxAppStart>();
        startup.Start();


        sbPresenter.MasterNavigationController.NavigationBar.Translucent = false;

        sbPresenter.MasterNavigationController.SetNavigationBarHidden(false, false);

    }

最后一次记住,为故事板上的每个视图设置一个带有该视图的类名"的故事板ID!遗憾的是,没有其他方法可以使用UIStoryboard类获取视图,而只能通过使用Storyboard ID来获取视图

这篇关于MVVMCross:可以将Storyboard与ICommand导航一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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