This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 43694 - StaticResource does not work for x:String and OnPlatform
Summary: StaticResource does not work for x:String and OnPlatform
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-24 08:24 UTC by Karol S
Modified: 2017-02-02 10:22 UTC (History)
5 users (show)

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


Attachments
Repro project (280.58 KB, application/x-zip-compressed)
2017-01-09 19:55 UTC, E.Z. Hart
Details

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

Note You need to log in before you can comment on or make changes to this bug.