Bug 56314 - XAML OnPlatform with HeightRequest defaults non-set Platforms to 0 (invisible)
Summary: XAML OnPlatform with HeightRequest defaults non-set Platforms to 0 (invisible)
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-05-12 19:28 UTC by John R
Modified: 2017-06-05 14:15 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample project that shows the issue. (1008.56 KB, application/zip)
2017-05-12 19:28 UTC, John R
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description John R 2017-05-12 19:28:16 UTC
Created attachment 22142 [details]
Sample project that shows the issue.

I need to specify the HeightRequest of a SearchBar control in XAML file for the Android platform only due to this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=43975, and I need to leave the default values for the other platforms, however, at runtime and in the Forms Previewer, the SeachBar on iOS is not visible (with a heighrequest of 0) even though I didn't set the value of the OnPlatform for iOS.

Here's my sample SearchBar that shows the issue:

```
<SearchBar
                x:Name="searchBar"
                Placeholder="Search"
                Text="{Binding SearchText, Mode=TwoWay}"
                BackgroundColor="Fuchsia">
                <SearchBar.HeightRequest>
                        <OnPlatform x:TypeArguments="x:Double">
                            <On Platform="Android">40</On>
                        </OnPlatform>
                    </SearchBar.HeightRequest>
            </SearchBar>
```

Instead of getting the default value on iOS where the SearchBar is visible, the SearchBar is not visible. It is visible on Android. I would assume that if we don't set a value for a specific platform, it would use the default for that Platform, but it looks like it is not working as it should.

You can see the issue in the sample project using the forms previewer.

The onplatformbugPage.xaml Page shows the onplatform setting for android only which makes the search bar not show on iOS. (this is the one that I would like to work and have the default OS height for the search bar on iOS).

The WithIOSHeightRequest Page shows that both search bars show if I set an On Platform for iOS with an explicit height (I don't want to set it as I want the OS to use the default height).

The WithoutHeightRequest shows that the iOS searchbar shows fine at it's default height, but the Android search bar does not show because of this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=43975
Comment 1 Jimmy [MSFT] 2017-05-18 21:43:00 UTC
Hi John, thank you for filing this report!

What you described sounds like expected behavior because in 2.3.4 the new OnPlatform mechanism returns default(T) for non-specified platforms[1]. In this case, default(double) is 0 so the SearchBar does not appear on iOS.

To avoid this, you will need to set the HeightRequest on iOS (and any other platform) to '-1'. This is the default Forms value for controls which causes them to be the default size.

This will be easier to do in 2.3.5 as you will be able to set a single default value that is returned in these scenarios[2] instead of providing one for each platform.

However I also looked at the Google bug[3] linked to in bug 43975 and it says it should be fixed in v25 of the support libraries. If your Android project is using Forms 2.3.4 and is set to compile with Android 7.0 or higher, you can update the support library packages to the latest version where this issue should be fixed. This might be a better solution as you will not have to use OnPlatform as a workaround.

Hope this helps!

[1] https://forums.xamarin.com/discussion/93454/default-value-for-onplatform
[2] https://github.com/xamarin/Xamarin.Forms/pull/873
[3] https://issuetracker.google.com/issues/37117974
Comment 2 John R 2017-05-22 15:03:59 UTC
Thanks for the detailed feedback. Is the "-1" requirement documented anywhere? I'll try the v25 support libraries and see how that works. Either way, it looks like I have a way around my issue. :)