Bug 55343 - New OnPlatform doesn't return KeyValuePair values - XAML resource in app.xaml crash app
Summary: New OnPlatform doesn't return KeyValuePair values - XAML resource in app.xaml...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-04-18 18:27 UTC by Clint
Modified: 2017-06-20 18:55 UTC (History)
5 users (show)

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

Code and breakpoints showing before and after return values. (399.80 KB, image/png)
2017-04-18 18:27 UTC, Clint
Use of xaml resource in app.xaml - old and new syntax (172.68 KB, image/png)
2017-04-18 18:28 UTC, Clint

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:

Description Clint 2017-04-18 18:27:25 UTC
Created attachment 21602 [details]
Code and breakpoints showing before and after return values.

As seen on Xamarin Forums for Forms version 2.3.4

##KeyValuePair returns null under new `OnPlatform` syntax##

As of XF 2.3.4 the OnPlatform syntax has changed.

This resource lives in my App.xaml to provide platform-specific paths via a FileHelper DependencyService.
            <OnPlatform x:Key="LocalStoragePath"
                        iOS="" />
            <OnPlatform x:Key="LocalStoragePath"
                <On Platform="Android"
                    Value="/sdcard" />
                <On Platform="WinPhone"
                    Value="D:\" />
                <On Platform="iOS"
                    Value="" />

DependencyService C# (droid project)
        public override string GetPublicStorage()
            KeyValuePair<string, object> kvp = Application.Current.Resources.FirstOrDefault(
                r => r.Key == "LocalStoragePath");
            OnPlatform<string> test = kvp.Value as OnPlatform<string>;
            return test.Android;
When using the new syntax the return is null.
When using the old syntax the return is "/sdcard" as defined in the xaml resource.

![](https://us.v-cdn.net/5019960/uploads/editor/xj/4mwluwbpjh4m.png "")

Using the new syntax also seems to break all my style definitions and resources for things like font definitions and sizes.

Old works:
            <OnPlatform x:Key="HeaderFont"
                        iOS="Large" />
            <OnPlatform x:Key="HeaderFontSize"
                        iOS="64.0" />
New throws exception when the view tries to display:
            <OnPlatform x:Key="HeaderFont"
                <On Platform="Android"
                    Value="Large" />
                <On Platform="WinPhone"
                    Value="Large" />
                <On Platform="iOS"
                    Value="Large" />

            <OnPlatform x:Key="HeaderFontSize"
                <On Platform="Android"
                    Value="60.0" />
                <On Platform="WinPhone"
                    Value="60.0" />
                <On Platform="iOS"
                    Value="64.0" />

Used in `DataTemplate`.  Same template with old syntax works.  Changing only the XAML for the `OnPlatform` crashes when the view is displayed.
                            <Grid HeightRequest="80"
                                  BackgroundColor="{StaticResource BlueTextColor}"
                                  IsVisible="{Binding IsEnabled}">

<!-- Snip for brevity -->

                                <Label HorizontalOptions="CenterAndExpand"
                                       Text="{Binding FriendlyName,
                                       TextColor="{StaticResource TagForegroundColor}"
                                       Style="{StaticResource LabelStyleMed}" />


Again, the only difference here is which group of XAML I block-comment.
![](https://us.v-cdn.net/5019960/uploads/editor/zt/aryc8hjw7nc2.png "")
Comment 1 Clint 2017-04-18 18:28:12 UTC
Created attachment 21603 [details]
Use of xaml resource in app.xaml - old and new syntax
Comment 2 Stephane Delcroix 2017-04-19 07:48:16 UTC
1/ it's null, as you no longer assign the deprecated `Android` property. You shouldn't try to access the properties for other platforms than the current one you're executing on, and for that, just do `return (string) test;`

2/ I'm looking at this

3/ I don't understand what you mean, or the relation with OnPlatform. Please open a new issue for that
Comment 3 Stephane Delcroix 2017-04-19 08:03:28 UTC
I tried 2/, with and without XamlC enabled. and both resources are correctly parsed and applied with `{StaticResource}`

Please attach a small reproduction project triggering your issue.

Comment 4 Clint 2017-04-19 17:28:17 UTC
Thanks for researching this Stephane.

I'll build a new simple project for you to demo the second issue.
But the simple explanation is... If you define a resource in app.xaml using the new syntax:

            <OnPlatform x:Key="HeaderFont"
                <On Platform="Android"
                    Value="Large" />
                <On Platform="WinPhone"
                    Value="Large" />
                <On Platform="iOS"
                    Value="Large" />

Then us it inside a DataTemple, the app crashes.  Same page, same datatemplate, when the resource is defined using the old syntax works fine.

But like I said, I'll make new project for this - both to confirm it to myself - and to give you something to work with.
Comment 5 Rui Marinho 2017-06-20 18:55:59 UTC
Unfortunately, we’re unable to reproduce this report. If this issue is still occurring for you, please reopen this issue and attach a reproduction to the bug by starting with a clean Xamarin.Forms project adding just the code necessary to demonstrate the issue. 
Here are some reproduction best practices: https://gist.github.com/jassmith/92405c300e54a01dcc6d