2015-03-03 133 views
0

我試着用Xamarin'經典'MvvmCross。 我已經使用Android。MVVMCross Xamarin和故事板

但我不能在iOS上工作。我已經看過這裏提到的示例(EH):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); 
    } 

} 

的核心項目

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

      RegisterAppStart<ViewModels.MainViewModel>(); 
     } 
    } 

視圖模型默認App.cs:

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。但我不知道如何。

我應該如何讓MvvmCross與iOs一起工作? 我應該如何命名這些零件?

回答

0

MvvmCross的默認視圖查找器將查找名爲MainView的視圖。該視圖應該來自MvxViewController或另一種IMvxTouchView類型。如果你不想命名你的視圖控制器「MainView」,那麼你需要創建一個自定義視圖解析器。

我的建議:只需將您的RootViewController重命名爲MainView。

+0

我得到一個MissingMethodException:找不到類型MainView的默認構造函數。與故事板一起使用mvvmcross是否不常見?我無法找到最新版本的mvvmcross實際工作的示例。 – 2015-03-09 21:20:48