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

  • Question

  • User180523 posted

    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 "NokiaMapViewin order to callInitialize(), create AndroidListeners, subscribe to callbacks etc. SoNokiaMapView` 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. ```xml


    @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/

    Monday, June 26, 2017 1:14 PM

All replies

  • User21936 posted

    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.

    Thursday, July 6, 2017 12:41 AM
  • User180523 posted

    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.

    Thursday, July 6, 2017 12:08 PM
  • User180523 posted


    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.

    Friday, July 7, 2017 8:20 PM