Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Xamarin.Form Controls and Native Controls On the same page

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?

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

http://xamarin.com/forms

like image 947
Gulshan Avatar asked Sep 05 '14 11:09

Gulshan


1 Answers

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

The image snippet, from what I understand:

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

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."

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.
 }

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.

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();
     ... 
     ...

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.

For instance in WindowsPhone you would do this via:-

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

on the platform specific stub-page.

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

Update 1:-

==========

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

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.

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.

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

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.

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

like image 192
Pete Avatar answered Sep 28 '22 02:09

Pete