Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application styles are only applied if there is more than one style when StartupUri is not used #4457

Open
jnm2 opened this issue Apr 25, 2021 · 4 comments · May be fixed by #8853
Open

Application styles are only applied if there is more than one style when StartupUri is not used #4457

jnm2 opened this issue Apr 25, 2021 · 4 comments · May be fixed by #8853
Labels
Milestone

Comments

@jnm2
Copy link

jnm2 commented Apr 25, 2021

  • .NET Core Version: 5.0.202
  • Windows version: Windows 10 20H2
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

The application style has no effect, but it should have an effect. The style starts taking effect as soon as a second unrelated style is added before or after it.

This causes a long search for ways to fix the problem and is not beginner-friendly.

image

Full repro created from VS template

Download: Repro.zip

<Application x:Class="WpfRepro.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <!-- Uncomment to see the style above start taking effect -->
        <!-- <Style x:Key="Workaround" /> -->
    </Application.Resources>
</Application>
<Window x:Class="WpfRepro.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfRepro"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <TextBox />
</Window>
using System.Windows;

namespace WpfRepro
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            new MainWindow(startupUriIsNotAnOption: 42).Show();
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>
@daisyTian0517
Copy link

You need to add a ResourceDictionary to contain your Style and make it to the Application.Current.Resources.Source in App.xaml.cs, eg, You can put your style into a ResourceDictionary named a Dictionary1 , and add below code to your App.xaml.cs

 public App()
        {
            Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/ApplicationStyles;Component/Dictionary1.xaml",UriKind.Relative)) as ResourceDictionary);
        }

@jnm2
Copy link
Author

jnm2 commented Apr 26, 2021

Why is this needed if it's not needed when StartupUri is used and it's not needed if there is more than one style?

@ryalanms ryalanms added this to the Future milestone Apr 26, 2021
@ryalanms ryalanms added the Bug Product bug (most likely) label Apr 26, 2021
@ThomasGoulet73
Copy link
Contributor

This bug looks really weird. It's from the code generated by PresentationBuildTasks.dll.

See the difference between the generated code in App.g.i.cs:

Without <Style x:Key="Workaround" />
#pragma checksum "..\..\..\App.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "5B5BE5C8C8262B68A118BFB34A8694FD962B7EA1"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;


namespace WpfRepro {
    
    
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {
        
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "5.0.5.0")]
        public static void Main() {
            WpfRepro.App app = new WpfRepro.App();
            app.Run();
        }
    }
}
With <Style x:Key="Workaround" />
#pragma checksum "..\..\..\App.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "D8DA150CB61153CE667AE1E7D93E9C88B025B8F8"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;


namespace WpfRepro {
    
    
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {
        
        private bool _contentLoaded;
        
        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "5.0.5.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/WpfRepro;V1.0.0.0;component/app.xaml", System.UriKind.Relative);
            
            #line 1 "..\..\..\App.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);
            
            #line default
            #line hidden
        }
        
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "5.0.5.0")]
        public static void Main() {
            WpfRepro.App app = new WpfRepro.App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

It does not seem to generate the InitializeComponent when there is only one item in <Resources>.

My guess would be that there is a wrong condition here that prevents the generation of InitializeComponent:
https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/MarkupCompiler/MarkupCompiler.cs#L3083

@ThomasGoulet73
Copy link
Contributor

This bug should be fixed by #8853. For now, you can workaround this issue by adding a dummy item in <Application.Resources>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants