Bug 43694

Summary: StaticResource does not work for x:String and OnPlatform
Product: Forms Reporter: Karol S <karol.stosik>
Component: FormsAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: brian, david, ehart, jas, karol.stosik, stephane.delcroix, v-nekha
Priority: ---    
Version: 2.3.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: ac Is this bug a regression?: ---
Last known good build:
Attachments: Repro project

Description Karol S 2016-08-24 08:24:13 UTC
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:resources="clr-namespace:Sample.Resources;assembly=Powel.Sample"
             x:Class="Sample.App">
  <Application.Resources>
    <ResourceDictionary>
      <x:String x:Key="Light">
        <x:String></x:String>
        <OnPlatform x:TypeArguments="x:String">
          <OnPlatform.iOS>OpenSans-Light</OnPlatform.iOS>
          <OnPlatform.Android>OpenSans-Light</OnPlatform.Android>
          <OnPlatform.WinPhone>/Assets/Fonts/OpenSans-Light.ttf#Open Sans</OnPlatform.WinPhone>
        </OnPlatform>
      </x:String>
...

This code results in error: Sequence contains no elements

Happens both in App.xaml.cs and page xamls.

Works for double resource:
      <x:Double x:Key="FontSize">
        <OnPlatform x:TypeArguments="x:Double">
          <OnPlatform.iOS>12</OnPlatform.iOS>
          <OnPlatform.Android>12</OnPlatform.Android>
          <OnPlatform.WinPhone>16</OnPlatform.WinPhone>
        </OnPlatform>

And the code works also well if nested inside a style:
       <Style x:Key="BaseLabel" TargetType="Label" BasedOn="Label">
        <Setter Property="FontFamily" >
          <x:String x:Key="Light">
            <x:String></x:String>
            <OnPlatform x:TypeArguments="x:String">
              <OnPlatform.iOS>OpenSans-Light</OnPlatform.iOS>
              <OnPlatform.Android>OpenSans-Light</OnPlatform.Android>
              <OnPlatform.WinPhone>/Assets/Fonts/OpenSans-Light.ttf#Open Sans</OnPlatform.WinPhone>
            </OnPlatform>
          </x:String>
        </Setter>
      </Style>

So the problem is only with string static resource.
Comment 1 Karol S 2016-08-24 08:27:48 UTC
I incorrectly pasted the last code sample, but after removing x:String parts it compiles
Comment 2 E.Z. Hart [MSFT] 2017-01-09 19:55:43 UTC
Created attachment 19139 [details]
Repro project
Comment 3 Stephane Delcroix 2017-01-25 09:49:24 UTC
@ez: this repro can't be used... and it's 43694, not 43964 :P
Comment 4 Stephane Delcroix 2017-01-25 11:56:52 UTC
this is too far of a stretch... <x:String> markup (and other markup for types primitives is meant to create a string from a string literal, and not from another element that might be a string.

the example you show using x:Double doesn't work. It only fails silently...

you can fix your code by declaring it like this:

  <ResourceDictionary>
    <OnPlatform x:Key="Light" x:TypeArguments="x:String">
      <OnPlatform.iOS>OpenSans-Light</OnPlatform.iOS>
      <OnPlatform.Android>OpenSans-Light</OnPlatform.Android>
      <OnPlatform.WinPhone>/Assets/Fonts/OpenSans-Light.ttf#Open Sans</OnPlatform.WinPhone>
    </OnPlatform>
  </ResourceDictionary

I'v changed the Xaml parser and compiler so it throws a meaningful exception instead of an InvalidOperationException. Note that this will throw for your <x:Double> example now: https://github.com/xamarin/Xamarin.Forms/pull/714
Comment 5 Stephane Delcroix 2017-02-02 10:22:25 UTC
this will be fixed in 2.3.5-pre1
Comment 6 faceoffers28 2017-06-15 15:07:46 UTC
I'm in the process of making the OnPlatform changes to my XAML as part of the 2.3.4 upgrade. Are you saying that making the change to code like this will not cause an error because it is wrapped in a Style? I'm running 2.3.4.231.

<Style x:Key="ActivityIndicatorStyle" TargetType="ActivityIndicator">
        <Setter Property="Color">
        	<OnPlatform x:TypeArguments="x:String"
              iOS="Blue"
              Android="White"
              WinPhone="Blue" />
        </Setter>
</Style>

Seems like x:TypeArguments="x:Double" are OK based on Karol's comment.

What about x:TypeArguments="GridLength"?

Thanks!
Comment 7 Neha Kharbade 2017-07-05 06:17:14 UTC
hi,

As attached repo sample is of different bug. 

Could you please provide me test steps to verify this bug. Your help would be appreciated.