【问题标题】:display ViewController from Native forms从本机表单显示 ViewController
【发布时间】:2020-09-10 15:11:52
【问题描述】:

我需要从 Xamarin.forms 显示本机控制器 IOS 我已经尝试过了

 UIWindow window = UIApplication.SharedApplication.KeyWindow;
 UIViewController vc = window.RootViewController;

RGLDocReader.Shared.ShowScanner(vc, HandleRGLDocumentReaderCompletion);

这个 How to create navigation in a Xamarin.iOS app?

关注了这个 https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/ios-code-only?tabs=macos

本教程工作正常,但是我需要在指定的方法中使用视图控制器,一旦通过视图控制器,我就会得到 null

【问题讨论】:

标签: xamarin.forms


【解决方案1】:

实际上,导航到 Forms 中的本机控制器并不是一个好的设计。但如果你确实想实现它,你可以使用 DependencyService

在表单中

创建界面

public  interface IOpenNativeView
{
   void OpenNativeView();
}

在 iOS 项目中

using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenNativeView))]
namespace xxx.iOS
{
    public class OpenNativeView : IOpenNativeView
    {
        void IOpenNativeView.OpenNativeView()
        {
            var CurrentViewController = topViewControllerWithRootViewController(UIApplication.SharedApplication.Delegate.GetWindow().RootViewController);
            CurrentViewController.NavigationController.PushViewController(YourViewController,true);
        }

        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            if (rootViewController is UITabBarController)
            {
                UITabBarController tabBarController = (UITabBarController)rootViewController;
                return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
            }
            else if (rootViewController is UINavigationController)
            {
                UINavigationController navigationController = (UINavigationController)rootViewController;
                return topViewControllerWithRootViewController(navigationController.VisibleViewController);
            }
            else if (rootViewController.PresentedViewController != null)
            {
                UIViewController presentedViewController = rootViewController.PresentedViewController;
                return topViewControllerWithRootViewController(presentedViewController);
            }
            else
            {
                return rootViewController;
            }
        }
    }
}

现在在 ContentPage 中,您可以在想要打开 ViewController 时调用以下行

DependencyService.Get<IOpenNativeView>().OpenNativeView();

【讨论】:

  • 那么什么是好的设计呢?我需要将视图控制器传递给方法,这就是我必须这样做的原因
  • 为什么不通过原生的ContentPage?
  • 该方法只接受 UiViewController 它的 api 只为 IOS 完成
  • 在这种情况下你可以使用我的解决方案。
  • 我稍后看看
猜你喜欢
  • 2016-08-22
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
相关资源
最近更新 更多