MVVMCross Xamarin和故事板
问题描述:
我试着用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一起工作? 我应该如何命名这些零件?
答
MvvmCross的默认视图查找器将查找名为MainView的视图。该视图应该来自MvxViewController或另一种IMvxTouchView类型。如果你不想命名你的视图控制器“MainView”,那么你需要创建一个自定义视图解析器。
我的建议:只需将您的RootViewController重命名为MainView。
我得到一个MissingMethodException:找不到类型MainView的默认构造函数。与故事板一起使用mvvmcross是否不常见?我无法找到最新版本的mvvmcross实际工作的示例。 – 2015-03-09 21:20:48