如何使用Xamarin.Form控制和本地控制在同一页面上 [英] How to use Xamarin.Form Controls and Native Controls On the same page

查看:177
本文介绍了如何使用Xamarin.Form控制和本地控制在同一页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Xamarin.Form控制在我的原生Android的页面控制。在我的Andr​​oid原生页面有没有什么办法来加载Xamarin.Form页在我的Andr​​oid原生片段或LinearLayout中?

I want to use Xamarin.Form Control inside my Native Android Page Controls. In my Android native page is there any way to load a Xamarin.Form Page inside my Native Android Fragment or LinearLayout?

根据 Xamarin 的这是可能的。但我无法找到如何实现这一目标。

According to Xamarin it's possible. But I cannot find how to achieve this.

推荐答案

有几件事情正在讨论,所以裸跟我说: -

There's a few things being discussed, so bare with me:-

图片片段,从我的理解: -

The image snippet, from what I understand:-

 "Not only are Xamarin.Forms pages mixable with custom screens" ...

这是像什么@Stephane被提允许应用程序使用本机屏幕上的以及作为 Xamarin.Forms 屏幕。每个平台存根页的可以在应用程序中的任何时候提出一个共享的 Xamarin.Forms 页。您不必使用 Xamarin.Forms 导航堆栈

This is like what @Stephane is mentioning allowing an application to use native screens as well as Xamarin.Forms screens. Each platform stub-page can introduce a shared Xamarin.Forms page at any point in the application. You don't have to use the Xamarin.Forms Navigation Stack.

 "but you can embed custom views built directly against Xamarin.IOS and Xamarin.Android into Xamarin.Forms pages."

这是什么@Sten被提的,在那里,如果你创建的特定平台 ContentRenderers 您可以使任何一种的本机控制到现有 Xamarin.Forms 页,通过传递这个共同查看以每个平台的渲染器。

This is what @Sten is mentioning, where if you create platform specific ContentRenderers you can render any kind of native controls into an existing Xamarin.Forms page, by passing this common View to each platform renderer.

所以,举例来说,如果你有一个查看: -

So, for instance if you had a View:-

 public class MyView : ContentView
 {
     // Create some bindable properties here that can be used in your platform specific renderer,
     // i.e. DisplayText etc.
 }

您可以使用上面的 Xamarin.Forms 页定义,然后创建的特定于平台的渲染器的,将被调用的呈现内容

You can use the above in a Xamarin.Forms page definition and then create platform specific renderers that would be called to render the content.

这是在这些的平台的具体内容渲染器类的,你可以使用的本机控制的每一个平台上。

It is from within these platform specific content renderer classes that you can use native controls on each platform.

例如在 ContentRenderer ,该事件的 OnElementChanged 您可以不喜欢下面的的WindowsPhone : -

For instance in the ContentRenderer, for the event OnElementChanged you could do something like the following for WindowsPhone:-

 protected override void OnElementChanged(Xamarin.Forms.Platform.WinPhone.ElementChangedEventArgs<MyView> e)
 {
     base.onElementChanged(e);
     ...
     System.Windows.Controls.TextBox objPlatformSpecificTextBox = new System.Windows.Controls.TextBox();
     ... 
     ...

不过,@Gulshan真的希望,我相信是有一个本地页中心舞台 Xamarin.Forms 内容所呈现的到一个特定的点中的已经存在的本地页的。

However, what @Gulshan is really wanting I believe is to have a native page that is center-stage with the Xamarin.Forms content being rendered into a specific point in an already existing native page.

它有点颠倒,思维,然而,这仍然是可能的: -

Its kinda-reversed, in thinking, however this is still possible:-

要做到这一点,你需要刺激页面代入暂新页

To achieve this you need to stimulate page-generation into a temporary new page.

例如,在的WindowsPhone 您会通过这样做: -

For instance in WindowsPhone you would do this via:-

        PhoneApplicationPage objInnerPage_Temp = new PhoneApplicationPage();
        UIElement objInnerContent = App.GetMainPage().ConvertPageToUIElement(objInnerPage_Temp);

特定于平台的存根页

可以和注射所呈现的内容插入到机容器这是适当的平台上,即电网

You can then inject the rendered contents into a native container that is appropriate on the platform, i.e. a Grid.

更新1: -

==========

虽然它很容易做到这一点的的WindowsPhone 它似乎并不认为这在尝试类似的东西时,可以机器人不幸。

Although its very easy to do this on WindowsPhone it doesn't seem that this is possible when attempting something similar in Android unfortunately.

安卓页创作是非常锁定,即你的不能获得的私人字段帆布和/或布局,它在内部使用的 AndroidActivity

The Android page creation is very locked down, i.e. you can't gain access to the private fields for canvas and/or layout that it internally uses in AndroidActivity.

如果这是可能的,那么你可以重新使用,并采取呈现的内容和注射类似的的WindowsPhone 的平台解决方案。

If this was possible then you could re-use this and take the contents rendered and inject similar to the solution for the WindowsPhone platform.

此外,平台类被标记为内部同时,preventing另一种方式来产生渲染的内容。

Furthermore, the Platform class is flagged as internal also, preventing another way to generate the rendered contents.

有希望,我从一个片段的实施产生的活动进去,这可以很好的工作了接近这一思想 - 的然而 - 在这里的问题是,在 Xamarin.Forms 期待在动作条可用,其中它不是,他们不检查一个如果为null 的情况下,允许继续执行,如果它不适用,因此它抛出一个异常preventing网页内容生成。

There was hope I was thinking from approaching this from a Fragment implementation of generating an Activity into it, and this could well work - However - the issue here is that the implementation in Xamarin.Forms is expecting the ActionBar to be available of which it is not, and they aren't checking for a if null scenario to allow execution to continue if it is not available so it throws an exception preventing page content generation.

它并不因此看上去可能?除非他们打开了布局/帆布领域的 AndroidActivity 或解决的动作条的预期问题。

It doesn't therefore look possible?, unless they open up the layout / canvas fields in AndroidActivity or address the ActionBar expectation issue.

这篇关于如何使用Xamarin.Form控制和本地控制在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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