XF native embedding - What is the syntax to tell an Android Fragment to MATCH_PARENT?

ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

Xamarin.Forms PCL project.
ContentPage has an Android Fragment on it using Native Embedding.
I think the fragment is technically working, in that the native engine initialization callback responds with ERROR.NONE, and I can use the API calls that get initialized when the fragment initializes.
The problem is that the fragment has a size of 0,0.

I can set the container size. I cannot for the life of me find any documentation in any of the Xamarin write-ups on how to set the LayoutParameters of the fragment to MATCH_PARENT so it will expand to fit its container.
There's some limited examples of sending x:Arguments, but I'm just guessing in the dark about DataType and parameter casing. The permutations are almost endless.

Per examples: The native MapFragment has been subclassed to "NokiaMapView in order to call Initialize(), create Android Listeners, subscribe to callbacks etc. So NokiaMapView` in the example below is a fragment derived from the native fragment; as shown then the available tutorials.

This is just a small sampling of my attempts. I have a huge amount of commented markup with various combinations and permutations of attempts that all fail at runtime with errors same or similar to the commented error here.

<views:View x:Name="mapWrap"  x:Arguments="{x:Static androidForms:Forms.Context}">
  <androidLocal:NokiaMapView x:Arguments="{x:Static androidForms:Forms.Context}">
    <!--<androidLocal:NokiaMapView.Layoutparameters>
      <x:Arguments>
        <androidForms:Layoutparameters>MATCH_PARENT</androidForms:Layoutparameters>
      </x:Arguments>
    </androidLocal:NokiaMapView.Layoutparameters>-->
  </androidLocal:NokiaMapView>
</views:View>

<!--  06-23 15:04:21.095 I/MonoDroid(23792): Xamarin.Forms.Xaml.XamlParseException: Position 273:25. 
      Cannot assign property "LayoutParameters": Property does not exists, or is not assignable, or mismatching type between value and property  -->

@DavidOrtinau @JamesMontemagno @PierceBoggan
You've each written a tutorial or article on behalf of Xamarin for this new ability of embedding native elements in a PCL ContentPage - Do any of you have a technique, example or input on how to set this vital arguement?

~~~~

*Native Embedding references:
https://developer.xamarin.com/guides/xamarin-forms/user-interface/native-views/
https://blog.xamarin.com/adding-bindable-native-views-directly-to-xaml/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/add-platform-controls/
https://blog.xamarin.com/unleashed-embedding-xamarin-forms-in-xamarin-native/

Tagged:

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    I believe you need to use a Shared project type to use the embedded native control feature. See:
    https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/add-platform-controls/#Overview

    However, note that this functionality requires the use of #if defines in Xamarin.Forms Shared Project solutions, and isn't available from Xamarin.Forms Portable Class Library (PCL) solutions.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Actually you can add native controls to your agnostic views in PCL. There are blogs and tutorials on it. When the capability was first introduced it was for shared projects only. But that expanded. There are some things you can do easier in shared than PCL, but its not exclusive.

    The tutorial you mentioned is adding all the controls through C# code behind, not XAML. Doing it that was does require the use of #if platform declarations which do only exist in shared projects. That is not the approach taken here.

    If you check out the second link I listed in the original post you'll see an example of doing embedding native UI elements in a PCL project. We do it just fine with simpler controls like native checkboxes and bind them back to ViewModels without issue.
    https://blog.xamarin.com/adding-bindable-native-views-directly-to-xaml/

    @PierceBoggan
    native view declaration works in Portable Class Libraries (PCLs) as well. Native data binding will also work in PCLs, though you will need to ensure you have the XAML compiler (XAMLC) turned off for pages that use native view declaration.

    The native android MapFragment is actually applied to the page in the PCL project. It works in so far as it sends and receives data, search requests etc. I can see the returned data at debug. However the child controls nested within the fragment don't receive their size parameters, and thus the Map inside the MapFragment has a size of zero by zero. You can guess what happens when you try to tell the fragment to add objects on a map of zero by zero size - Yep, it crashes because it can't work out size and placement of the pins, markers and so on.

    The crux of the problem I'm facing is there is no example of setting the the Layout_Height and Layout_Width to MATCH_PARENT in XAML - which is one of the most basic Android settings. Its stunningly absent in the documentation and blogs posts on this feature.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JGoldberger

    As a test I build a Shared test solution - same result. Both PCL and Shared let you place native Android Widgets without a problem. Probably because you don't have to set the Layout_Height and Layout_Width at the time of construction. We can see a native CheckBox and native RatingBar in both the Shared and PCL versions of the test project.

    The big empty green box is the Fragment.

    But an Android Fragment does need this and it is very common.

    I believe, that without being able to set this at the time of creation the value isn't passed down to the nested children of the Fragment. Nobody monitors this parameter for changes after construction so doing it in code behind after the fact does no good. The normal Android expectation is that it is set during construction, then passed down. Without being able to do that all we get is a big empty box whose children have a size of 0 by 0.

    Any chance of reaching out to @DavidOrtinau @JamesMontemagno @PierceBoggan since they've each written tutorials on doing this? If there is a syntax for passing these parameters in the XAML I would think one of them would be aware of it.

Sign In or Register to comment.