2016-11-16 55 views
0

我試圖製作使用MvvmCross的選項卡式應用程序,但似乎無法找到顯示如何完成的工作示例。我見過的所有示例都是針對以前版本的,它們尚未更新,似乎缺少對最新更新的一些必要更改。使用MvvmCross中的選項卡

考慮這個例子: https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Touch/Views/TabBarController.cs

我需要什麼樣的變化,使以該類得到它與MvvmCross的最新版本?

回答

3

這是我如何在我的應用程序上使用選項卡。我正在使用MvvmCross 4.4.0。

AppDelegate.cs:

[Register("AppDelegate")] 
public partial class AppDelegate : MvxApplicationDelegate 
{ 
    private UIWindow window; 

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
    { 
     this.window = new UIWindow(UIScreen.MainScreen.Bounds); 

     Setup setup = new Setup(this, this.window); 
     setup.Initialize(); 

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

     this.window.MakeKeyAndVisible(); 

     return true; 
    } 
} 

Setup.cs:

public class Setup : MvxIosSetup 
{ 
    public Setup(MvxApplicationDelegate applicationDelegate, UIWindow window) : base(applicationDelegate, window) 
    { 
    } 

    protected override IMvxApplication CreateApp() 
    { 
     return new Core.App(); 
    } 
} 

HomeView.cs:

public class HomeView : MvxTabBarViewController<HomeViewModel> 
{ 
    private bool _constructed; 

    public HomeView() 
    { 
     _constructed = true; 

     // need this additional call to ViewDidLoad because UIkit creates the view before the C# hierarchy has been constructed 
     ViewDidLoad(); 
    } 

    public override void ViewDidLoad() 
    { 
     if (!_constructed) 
      return; 

     base.ViewDidLoad(); 

     var viewControllers = new UIViewController[] 
     { 
      CreateTabFor(0, "First", "FirstImage", typeof(FirstViewModel)), 
      CreateTabFor(1, "Second", "SecondImage", typeof(SecondViewModel)), 
      CreateTabFor(2, "Third", "ThirdImage", typeof(ThirdViewModel)) 
     }; 

     ViewControllers = viewControllers; 
     CustomizableViewControllers = new UIViewController[] { }; 

     //Sometimes I need to start with a specific tab selected 
     SelectedViewController = ViewControllers[ViewModel.CurrentPage]; 
    } 

    private UIViewController CreateTabFor(int index, string title, string imageName, Type viewModelType) 
    { 
     var controller = new UINavigationController(); 
     var request = new MvxViewModelRequest(viewModelType, null, null, null); 
     var viewModel = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, null); 
     var screen = this.CreateViewControllerFor(viewModel) as UIViewController; 
     screen.Title = title; 
     screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle(imageName), index); 
     controller.PushViewController(screen, true); 
     return controller; 
    } 
} 

FirstView.cs,SecondView.cs,ThirdView.cs和所有其他選項卡式視圖:

public class FirstView : MvxViewController<FirstViewModel> 
{ 
    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     //Your view content... 
    } 
} 

Android和iOS的Github存儲庫示例:https://github.com/rrispoli/SampleTabs

+1

Rafael - 這非常有幫助。任何機會,你可以發佈一個鏈接到GitHub中的示例,它只顯示使用這種方法在ios和android中工作的選項卡菜單?我很樂意捐贈給你最喜歡的慈善機構! :) –

+0

是的,我將在接下來的幾天內創建一個示例項目,我將在這裏離開GitHub鏈接。 :) –

+0

嗨拉斐爾,如果你有機會發布該示例項目,將不勝感激:) –

相關問題