我将 MvvmCross 用于我的应用程序并从 iPhone 开始。为了实现使用 RequestNavigate 和 Close 在视图之间导航的复杂性,我从 MvxBaseTouchViewPresenter 派生了一个新的演示者。到目前为止,一切都很好。
我现在遇到的问题是可以关闭或显示视图,而之前可见的视图仍在转换中,无论是打开还是关闭。我能想到的解决这个问题的唯一方法是使用视图控制器的 ViewDidAppear 和 ViewDidDisappear 事件处理程序来调用执行下一个视图显示的操作,从而将显示推迟到前一个视图完成其转换之后。
这意味着我必须添加:
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
DoPostTransitionAction();
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
DoPostTransitionAction();
}
private void DoPostTransitionAction()
{
if( _postTransitionAction != null)_postTransitionAction();
}
private Action _postTransitionAction;
public void ShowActionAfterTransition( Action postTransitionAction)
{
if( this.IsBeingDismissed || this.IsBeingPresented)
{
_postTransitionAction = postTransitionAction;
}
else
{
_postTransitionAction = null;
postTransitionAction();
}
}
查看控制器,引入一个 IEventViewController 接口,让我添加代码并将演示者的 Show 方法更改为:
public override void Show(MvxShowViewModelRequest request)
{
// find the current top view as it will disappear as a result of
// showing the requested view
// note: this will be null for the first show request
var topViewController = TopViewController;
// if the request is to roll back an existing stack of views
// then rollback to the bottom of the stack
if (request.ClearTop && topViewController != null)
{
RollBackStack(topViewController);
}
// the top view is about to be 'covered' by the view requested
// if the top view is transitioning (appearing or disappearing)
// then wait for it to finish before showing the view requested
var disappearingViewController = topViewController as IEventViewController;
// if the top view is disappearing and it's of the 'event' type
// delay calling Show until after the view has disappeared
if( disappearingViewController != null)
{
// the top view has been requested to close and will fire the
// 'transition complete' event when it has
// register the action to perform when the top view has disappeared
disappearingViewController.ShowActionAfterTransition (() =>
{
Show (CreateView(request), true);
});
}
else
{
// show the view now as the top view is either not closing
// or, if it is, is not going to say when it does
// create a view controller of the type requested
Show(CreateView(request), true);
}
}
我真正想要的是基类中的那些事件处理程序。我想知道如果我不让自己偏离主要发展道路,有什么机会?偏班可以吗?