如何显示来自Xamarin Forms的iOS添加联系人屏幕? [英] How to show iOS add contact screen from Xamarin Forms?

查看:126
本文介绍了如何显示来自Xamarin Forms的iOS添加联系人屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xamarin Forms显示iOS添加联系人屏幕.从我可以看出,Xamarin Forms不支持此功能,但Xamarin iOS支持.不幸的是,我不能让他们一起工作.我的意思是在一起"是我需要从Xamarin表单页面访问NavigationController.

I'm trying to show the iOS add contact screen using Xamarin Forms. From what I can see Xamarin Forms does not support this out of the box but Xamarin iOS does. Unfortunately I can't get them to work together. What I mean by "together" is that I need get access to NavigationController from Xamarin Forms Page.

可以做到吗?

我有一个示例解决方案,可在此处演示该问题: https://github.com/pawelpabich/XamarinFormsContacts.我还将最重要的代码放在下面.

I have a sample solution that demonstrates the problem here: https://github.com/pawelpabich/XamarinFormsContacts. I also put the most important code below.

public void ShowContact(NavigationPage page)
{
    var newPersonController = new ABNewPersonViewController();
    var person = new ABPerson();
    person.FirstName = "John";
    person.LastName = "Doe";

    newPersonController.DisplayedPerson = person;
    var controller = page.CreateViewController();

    //!!!!---> controller.NavigationController is null !!!!!<----
    controller.NavigationController.PushViewController(newPersonController, true);
}

我更新了该存储库,它现在包含有效的代码.

推荐答案

使用Xamarin.Forms时(使用NavigationPage时)有一个UINavigationController,但是您必须搜索它.这是我掌握它的唯一方法.那些其他方法,CreateViewController和RendererFactory实际上创建了一个新的ViewController,这不是您想要的.

There is a UINavigationController when using Xamarin.Forms (when using a NavigationPage), but you have to search for it. This was the only way I could get a hold of it. Those other methods, CreateViewController and RendererFactory actually create a new ViewController which isn't what you wanted.

public void ShowContact(NavigationPage page)
{
    var newPersonController = new ABNewPersonViewController();
    var person = new ABPerson();
    person.FirstName = "John";
    person.LastName = "Doe";
    newPersonController.Title = "This is a test";

    newPersonController.DisplayedPerson = person;

    UINavigationController nav = null;
    foreach (var vc in 
      UIApplication.SharedApplication.Windows[0].RootViewController.ChildViewControllers) 
    {
        if (vc is UINavigationController)
            nav = (UINavigationController)vc;
    }

    nav.PresentModalViewController(new UINavigationController (newPersonController), true);
}

我还尝试创建一个PersonPage和PersonPageRenderer,因为这将是最干净的解决方案,但我无法使其正常运行.如果您花了一些时间,这可能会起作用.

I also attempted to Create a PersonPage and PersonPageRenderer, as that would be the cleanest solution, but I couldn't get it working. This could work if you spent some time.

[assembly: ExportRenderer(typeof(PersonPage), typeof(PersonPageRenderer))] 

public class PersonPageRenderer : ABNewPersonViewController, IVisualElementRenderer, IDisposable, IRegisterable 

这篇关于如何显示来自Xamarin Forms的iOS添加联系人屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆