XAML中样式的奇怪行为

时间:2022-06-06 19:32:42

I can't understand why happens this. I have a simple application in WPF. This application have a window, and in the App.xaml have defined one style, that changes the style of all the buttons:

我无法理解为什么会这样。我在WPF中有一个简单的应用程序。这个应用程序有一个窗口,并且在App.xaml中定义了一种样式,它改变了所有按钮的样式:

<Application x:Class="PruebasDesk.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Window1.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Height" Value="23"></Setter>
            <Setter Property="Width" Value="75"></Setter>
            <Setter Property="Background" Value="DarkCyan"></Setter>
        </Style>
    </Application.Resources>
</Application>

This works fine, all the buttons get the style. Now, here is the problem. If instead of using the StartupUri attribute to start the application, I start it with using the OnStartup method:

这很好用,所有的按钮都得到了风格。现在,这是问题所在。如果不是使用StartupUri属性来启动应用程序,而是使用OnStartup方法启动它:

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Window1 win1 = new Window1();
            win1.Show();
        }
    }

The buttons of the application don't get applied the button style defined at App.xaml. But... if I add another style to the App.xaml, like this:

应用程序的按钮不会应用App.xaml中定义的按钮样式。但是......如果我向App.xaml添加另一种样式,如下所示:

<Application x:Class="PruebasDesk.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             >
    <Application.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Height" Value="23"></Setter>
            <Setter Property="Width" Value="75"></Setter>
            <Setter Property="Background" Value="DarkCyan"></Setter>
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Height" Value="23"></Setter>
            <Setter Property="Width" Value="180"></Setter>
            <Setter Property="Background" Value="Azure"></Setter>
        </Style>
    </Application.Resources>
</Application>

Then the buttons get the style applied!!! This seems really weird to me. Does anyone know if I am missing something?

然后按钮获得应用的样式!这对我来说似乎很奇怪。有谁知道我错过了什么吗?

1 个解决方案

#1


0  

I can't tell you with certainty why that behavior occurs but I can tell you that best practices would have steered you away from using App.xaml in that fashion. What I believe to be a better practice is to only merge in your resource dictionaries in app.xaml. Storing the actual styles is a good way to create an unmanageable project.

我不能肯定地告诉你为什么会出现这种行为,但我可以告诉你,最好的做法会让你无法以这种方式使用App.xaml。我认为更好的做法是仅在app.xaml中合并您的资源字典。存储实际样式是创建无法管理的项目的好方法。

Create a new ResourceDictionary file and add those styles. Merge the dictionaries in as you add more of them.

创建一个新的ResourceDictionary文件并添加这些样式。在添加更多字典时合并字典。

  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Common.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

Alternatively, you could hook into the event Startup (app.xaml: Startup="App_Startup") and not override OnStartup. This will work with the App.xaml resource setup you have defined. It is likely a timing issue.

或者,您可以挂钩事件Startup(app.xaml:Startup =“App_Startup”)而不是覆盖OnStartup。这将与您定义的App.xaml资源设置一起使用。这可能是一个时间问题。

#1


0  

I can't tell you with certainty why that behavior occurs but I can tell you that best practices would have steered you away from using App.xaml in that fashion. What I believe to be a better practice is to only merge in your resource dictionaries in app.xaml. Storing the actual styles is a good way to create an unmanageable project.

我不能肯定地告诉你为什么会出现这种行为,但我可以告诉你,最好的做法会让你无法以这种方式使用App.xaml。我认为更好的做法是仅在app.xaml中合并您的资源字典。存储实际样式是创建无法管理的项目的好方法。

Create a new ResourceDictionary file and add those styles. Merge the dictionaries in as you add more of them.

创建一个新的ResourceDictionary文件并添加这些样式。在添加更多字典时合并字典。

  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Common.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

Alternatively, you could hook into the event Startup (app.xaml: Startup="App_Startup") and not override OnStartup. This will work with the App.xaml resource setup you have defined. It is likely a timing issue.

或者,您可以挂钩事件Startup(app.xaml:Startup =“App_Startup”)而不是覆盖OnStartup。这将与您定义的App.xaml资源设置一起使用。这可能是一个时间问题。