MVVMCross Xamarin和故事板

时间:2015-03-03 14:11:31

标签: ios xamarin mvvmcross

我正在试用MvvmCross和Xamarin'经典'。 我已经使用Android了。

但我无法让它适用于iOS。我看了一下这里提到的样本(呃):MVVMCross support for Xamarin.iOS Storyboards

我真的错过了什么。 我有什么:

  • 一个只有3个控件的故事板。一个标签和2个按钮。全部3 有名字所以我得到了RootViewController类中的属性。
  • 基础setup.cs
  • AppDelegate.cs

    [Register("AppDelegate")]
        public partial class AppDelegate : MvxApplicationDelegate
        {
    
    UIWindow _window;
    
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        _window = new UIWindow(UIScreen.MainScreen.Bounds);
    
        StoryBoardTouchViewPresenter sbPresenter = new StoryBoardTouchViewPresenter(this, _window, "MainStoryboard");
    
        var setup = new Setup(this, _window);
        setup.Initialize();
    
        var startup = Mvx.Resolve<IMvxAppStart>();
        startup.Start();
    
        sbPresenter.MasterNavigationController.NavigationBar.Translucent = false;
    
        sbPresenter.MasterNavigationController.SetNavigationBarHidden(false, false);
    
        return true;
    }
    
    }
    

StoryBoardTouchViewPresenter(来自MVVMCross: Is it possible to use Storyboard with ICommand navigation?)但API已更改。

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);
    }

}

Core项目中的默认App.cs

public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
    {
        public override void Initialize()
        {
            CreatableTypes()
                .EndingWith("Service")
                .AsInterfaces()
                .RegisterAsLazySingleton();

            RegisterAppStart<ViewModels.MainViewModel>();
        }
    }

ViewModel:

public class MainViewModel : MvxViewModel
    {
        ITodoTaskService taskService;
        IDataManager<TodoTask> tasks;

        public MainViewModel(ITodoTaskService taskService)
        {
            this.taskService = taskService;
        }

        public async override void Start()
        {
            this.tasks = new DataManager<TodoTask>(await this.taskService.GetTodoTasksAsync());
            this.tasks.MoveFirst();

            Rebind();
            base.Start();
        }

        private void Rebind()
        {
            this.Description = this.tasks.Current.Description;

            NextCommand.RaiseCanExecuteChanged();
            PreviousCommand.RaiseCanExecuteChanged();
        }

        private string description;

        public string Description
        {
            get { return this.description; }
            set
            {
                this.description = value;
                RaisePropertyChanged(() => Description);
            }
        }   

        private MvxCommand nextCommand;
        public MvxCommand NextCommand
        {
            get
            {
                this.nextCommand = this.nextCommand ?? new MvxCommand(NavigateToNext, CanNavigateNext);
                return this.nextCommand;
            }
        }

        private bool CanNavigateNext()
        {
            return this.tasks.CanMoveNext;
        }

        public void NavigateToNext()
        {
            this.tasks.MoveNext();
            Rebind();
        }

        private MvxCommand previousCommand;
        public MvxCommand PreviousCommand
        {
            get
            {
                this.previousCommand = this.previousCommand ?? new MvxCommand(NavigateToPrevious, CanNavigatePrevious);
                return this.previousCommand;
            }
        }

        private bool CanNavigatePrevious()
        {
            return this.tasks.CanMovePrevious;
        }

        public void NavigateToPrevious()
        {
            this.tasks.MovePrevious();
            Rebind();
        }
    }

我尝试过各种各样的事情。目前我得到一个例外,即无法找到MainView。我部分理解。在App.cs中MainViewModel是启动。但是控制器被称为RootViewController。我认为RootviewController应该绑定到我的MainViewModel。但我不知道如何。

我应该如何让iv工作的MvvmCross? 我应该如何命名这些部件?

1 个答案:

答案 0 :(得分:0)

MvvmCross&#39;默认视图查找器将查找名为MainView的视图。该视图应该从MvxViewController或其他IMvxTouchView类型派生。如果您不想命名您的视图控制器&#34; MainView&#34;那么你需要创建一个自定义视图解析器。

我的建议:只需将RootViewController重命名为MainView。