从当前视图而不是RootView导航到ViewModel

时间:2018-09-10 21:37:17

标签: ios xamarin mvvmcross

是否可以从当前视图而不是根视图导航到ViewModel?

我创建了一个视图模型,该视图模型是从根视图中模态呈现的,并希望在该视图中呈现一个视图模型。

例如,从我的模式视图调用_navigationService.Navigate<MyViewModel>();时,根视图导航到MyViewModel而不是当前的模式视图。这使我的模式屏幕上留有空白页,直到您将其关闭并看到下面已经进行了导航。

我是否创建自定义导航,或者有更好的方法呢?

编辑

以下是一些代码:

我的视图模型

 public class ViewModel1 : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

    public ViewModel1(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    public IMvxCommand NavigateCommand => new MvxCommand(ChangeView);
    void ChangeView()
    {
        _navigationService.Navigate<ViewModel2>();
    }
}

我的观点

 [MvxModalPresentation(WrapInNavigationController = true, ModalPresentationStyle = UIModalPresentationStyle.Custom, Animated = false)]
public partial class MyView : MvxViewController
{
    ViewModel1 VM;

    public MyView() : base("MyView", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        NavigationController.NavigationBar.Hidden = true;

        var set = this.CreateBindingSet<MyView, ViewModel1 >();
        set.Bind(NextButton).To(vm => vm.NavigateCommand);
        set.Apply();
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }
}

我想让导航命令在呈现的模式中呈现ViewModel2。

1 个答案:

答案 0 :(得分:0)

对于这种自定义,您可能需要编写一个自定义演示者,然后您就可以在其中摆弄以实现使用本机可以实现的一切。

我正在删除一些提示。

在AppDelegate中,完成启动。

 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {   

    Window = new UIWindow(UIScreen.MainScreen.Bounds);
            var presenter = new DelegatingPresenter(this, Window);
            var setup = new Setup(this, presenter);
            setup.Initialize();

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

DelegatingPresenter看起来像:

public class DelegatingPresenter : MvxIosViewPresenter {


        private UIWindow currentWindow;

        public DelegatingPresenter(AppDelegate appDelegate, UIWindow window)
            : base(appDelegate, window) {


            currentWindow = window;
            --
            ---

        }
        protected override void SetWindowRootViewController(UIViewController controller, MvvmCross.iOS.Views.Presenters.Attributes.MvxRootPresentationAttribute attribute = null)
        {
            base.SetWindowRootViewController(controller, attribute);
        }

        public override void Show(MvxViewModelRequest request)
        {
            var viewCreator = Mvx.Resolve<IMvxIosViewCreator>();

            var controller = (UIViewController)viewCreator.CreateView(request);
            Show((UIViewController)controller);
        }
        //public override void Show(IMvxIosView view) {

        //}

        public override void ChangePresentation(MvxPresentationHint hint) {

            ---
            ----
        }

        public override void Close(IMvxViewModel toClose) {

            ---
            ----
        }

        public void Show(UIViewController) {

         //if (viewController is IFlyoutNavigationControllerCapable) {
                //flyoutPresenter.Show(viewController, flyout);
            //}
            //else {
                //currentWindow.RootViewController = viewController;
                //navigationPresenter.Show(viewController, flyout);
            //}
    //Do your thing here.

        }

        public bool Close(FlyoutNavigationController flyout) {

---
----
        }
    }

在这里,您感兴趣的是加载新ViewModel时将调用的“ show”方法重写。 在这里,您具有要显示的rootviewController和新控制器,所以我想您可以从这里获取它。