Bug 45283 - Style isn't applied to control in custom renderer
Summary: Style isn't applied to control in custom renderer
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 2.3.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-10-11 17:10 UTC by blitzillu
Modified: 2016-11-23 14:36 UTC (History)
6 users (show)

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

repro project (131.36 KB, application/zip)
2016-10-12 21:36 UTC, Jimmy [MSFT]

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 blitzillu 2016-10-11 17:10:15 UTC
=== Overview ===
I want to style the controls in an UWP project. Therefore I use a custom renderer. After retrieving and setting the style, the style is not applied to the control in the latest XF versions

=== Steps to Reproduce  ===
	1) Create a Xamarin.Forms PCL project.
	2) Add an Application.Resources ResourceDictionary with one style:

              <SolidColorBrush x:Key="BackgroundColor" Color="Yellow" />

              <Style TargetType="Button" x:Name="myNewButtonStyle">
                <Setter Property="Background" Value="{StaticResource BackgroundColor}" />
	3) Create a page with a custom button
        4) Implement the custom button renderer and set the style
=== Expected Behavior ===
Background color should be applied to the control (in the example it should be yellow).

=== Actual Behavior ===
XF style is applied
XF style isn't applied
XF style isn't applied
XF style isn't applied 

=== Build Information ===
Microsoft Visual Studio Professional 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01038

Installed Version: Professional

Visual Basic 2015   00325-60003-97886-AA825
Microsoft Visual Basic 2015

Visual C# 2015   00325-60003-97886-AA825
Microsoft Visual C# 2015

Visual C++ 2015   00325-60003-97886-AA825
Microsoft Visual C++ 2015

Windows Phone SDK 8.0 - DEU   00325-60003-97886-AA825
Windows Phone SDK 8.0 - DEU

Application Insights Tools for Visual Studio Package   7.8.00927.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2015.1 (Beta8)   14.1.11107.0
ASP.NET and Web Tools 2015.1 (Beta8)

ASP.NET Web Frameworks and Tools 2012.2   4.1.41102.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.40314.0
For additional information, visit http://www.asp.net/

Command Bus, Event Stream and Async Manager   Merq
Provides ICommandBus, IEventStream and IAsyncManager MEF services for loosely coupled Visual Studio extension components communication and integration.

Common Azure Tools   1.8
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.4.4
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.60519.0
Microsoft SQL Server Data Tools

TypeScript tools for Visual Studio

UWPTileGeneratorCommand Extension   1.0
UWPTileGeneratorCommand Visual Studio Extension Detailed Info

Visual Studio Tools for Universal Windows Apps   14.0.25527.01
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin (3b7df6f)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Inspector Support (45b35bb)
Adds support for inspecting Xamarin and WPF apps.

Xamarin.Android (96c7ba6)
Visual Studio extension to enable development for Xamarin.Android.

Xamarin.iOS (ad1cd42)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 2 Jimmy [MSFT] 2016-10-12 21:36:35 UTC
Created attachment 18012 [details]
repro project

Marking CONFIRMED since I can reproduce the issue with the repro project I've attached.

### Steps to Reproduce
1. Run the attached UWP or Windows project
Comment 3 Samantha Houts [MSFT] 2016-10-20 00:55:21 UTC
The repro project has the styles defined in the device projects, which I confirm does not work in 2.3.2. However, it does not appear to have worked in 2.2.0, either. I downgraded the project and saw the same behavior.

In most cases, Xamarin.Forms controls will have core styles defined that will override styles defined in the device projects. We recommend setting styles using the Xamarin.Forms Resource Dictionaries whenever possible, as documented here: https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/application/

Also, please note that at this time, implicit styles are not inherited by derived classes, so your TargetType should be "MyButton", not "Button", in your Xamarin.Forms styles.

Please try this method to see if it will work for your purposes. Also, I am concerned that I was unable to reproduce a working state in version 2.2.0 with the provided reproduction project, so if you are certain that this was working before, please do not hesitate to reopen this report with more details that will help us investigate this issue.

Warm regards,
Xamarin Forms Team
Comment 4 blitzillu 2016-11-02 17:28:03 UTC
Hi Samantha,

you are correct. Now I tried to downgrade to 2.2.0 and it also doesn't work. Don't know why I had the case at that time.

Nevertheless, I want to style the controls in the UWP app. Using only the Xamarin.Forms Resource Dictionaries is not always possible (not all options through all platforms are supported). E.g. how do you set the border color of an Entry in UWP? I found a way to do this, but it uses the UWP Resource Dictionary in App.xaml (it is also possible to set one via code). And now it is getting complicated. If you set the SystemControlHighlightAccentBrush, you set it for all controls, which uses the brush. This is not desired. Additionally, there are hover and selected states, which you can't set with Xamarin.Forms (or I haven't found a way to do this). And this is only one example. There are much more controls/options and I want to style/modify.

Imho the solution to this problem is to use a custom renderer and set the complete style of a specific control via the device project (as demonstrated in the attached project). And this doesn't work. Do you have a solution for this?

PS: E.g. the complete style definiton of a TextBox can be found under "Default style" here: https://msdn.microsoft.com/en-us/library/windows/apps/mt299154.aspx
Comment 5 Jimmy [MSFT] 2016-11-10 17:45:47 UTC
I think the issue demonstrated by the repro project I attached is actually caused by bug 42100. That bug caused the background color set on a button via a UWP style to be ignored. I re-ran the repro project with the latest Forms master branch and confirmed the style does get applied to the button.

I also tested applying a UWP style to an Entry and ProgressBar control and it worked with the master branch and latest pre-release versions. If you are seeing issues with certain controls ignoring the style set in a custom renderer, please attach a repro project so we can look into this.
Comment 6 blitzillu 2016-11-23 14:36:50 UTC
Works now for me in XF