Bug 31182 - StaticResource forward references in ResourceDictionaries
Summary: StaticResource forward references in ResourceDictionaries
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.4
Hardware: PC Mac OS
: Low minor
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2015-06-17 03:20 UTC by Stephane Delcroix
Modified: 2016-04-10 01:31 UTC (History)
4 users (show)

Tags: ac all xaml resources
Is this bug a regression?: ---
Last known good build:

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 INVALID

Description Stephane Delcroix 2015-06-17 03:20:08 UTC
It works perfectly in Xamarin Forms 1.4.3-pre2, but fail in 1.4.3-pre3
I figure out the root cause is that the BasedOn style must be in order.

For example:
if plainButtonStyle places before colorButtonStyle, then crashes in 1.4.3-pre3, but works in 1.4.3-pre2.
06-17 12:31:04.975 I/MonoDroid(30533): UNHANDLED EXCEPTION:
06-17 12:31:04.975 I/MonoDroid(30533): Xamarin.Forms.Xaml.XamlParseException: Position 23:59. StaticResource not found for key colorButtonStyle

<Application.Resources>
    <ResourceDictionary> 
      <Style x:Key="plainButtonStyle" TargetType="Button" BasedOn="{StaticResource colorButtonStyle}">
        <Setter Property="BorderWidth" Value="0" />
        <Setter Property="BackgroundColor" Value="Transparent" />
      </Style>
      <Style x:Key="colorButtonStyle" TargetType="Button">
        <Setter Property="TextColor" Value="White" />
        <Style.Triggers>
          <Trigger Property="IsEnabled" Value="False" TargetType="Button">
            <Setter Property="TextColor" Value="Gray" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </ResourceDictionary>
  </Application.Resources>
if plainButtonStyle places after colorButtonStyle, then both works.

<Application.Resources>
    <ResourceDictionary> 
      <Style x:Key="colorButtonStyle" TargetType="Button">
        <Setter Property="TextColor" Value="White" />
        <Style.Triggers>
          <Trigger Property="IsEnabled" Value="False" TargetType="Button">
            <Setter Property="TextColor" Value="Gray" />
          </Trigger>
        </Style.Triggers>
      </Style>
      <Style x:Key="plainButtonStyle" TargetType="Button" BasedOn="{StaticResource colorButtonStyle}">
        <Setter Property="BorderWidth" Value="0" />
        <Setter Property="BackgroundColor" Value="Transparent" />
      </Style>
    </ResourceDictionary>
  </Application.Resources>
Comment 1 Stephane Delcroix 2015-06-17 03:22:45 UTC
I'm filing this because it's a regression from 1.4.3-pre2 to 1.4.3-pre3, but the spec for {StaticResource} says:

> Important  
> A StaticResource must not attempt to make a forward reference to a resource that is defined
> lexically further within the XAML file. Attempting to do so is not supported. Even if the forward
> reference doesn't fail, trying to make one carries a performance penalty. For best results, adjust the
> composition of your resource dictionaries so that forward references are avoided.

(https://msdn.microsoft.com/en-us/library/hh758287.aspx)

so this is more like an enhancement request