C#WPF:资源字典不会在运行时加载,但会在设计器中正确显示

问题描述

问题:资源字典不会在运行时加载,但会在设计器中正确显示。如果我将Dark.xaml中的颜色更改为绿色,则可以在设计器中注意到我的窗口背景(Layout.xaml)变为绿色。但是当我在Debug模式下编译项目时,背景只是透明的!

我在App.xaml中尝试过的内容

<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" x:Class="StackOverflow.App">
    <Application.Resources>
        <ResourceDictionary Source="#"/>
    </Application.Resources>
</Application>

并且:

<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" x:Class="StackOverflow.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="#"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在源值中,我尝试了以下三种可能性:

  1. 主题/Dark.xaml
  2. /StackOverflow;component/Theme/Dark.xaml
  3. pack:// application:,/ StackOverflow; component / Theme / Dark.xaml

App.xaml.cs:

Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

Window Window = Server.Status() is StatusCode.OK ? new Layout() : new Login();
Window.ShowDialog();

Current.Shutdown();

Dark.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="StackOverflow.Theme.Dark" xmlns:local="clr-namespace:StackOverflow.Theme">
    <SolidColorBrush x:Key="Layout.Background">Red</SolidColorBrush>
</ResourceDictionary>

Dark.xaml:文件属性

Dark.cs:

namespace StackOverflow.Theme {
    public partial class Dark : ResourceDictionary {
        public Dark(){
            InitializeComponent();
        }
    }
}

Dark.cs:文件属性

注意::这两个文件(Dark.xaml / Dark.cs)使用文件嵌套扩展名(Keith Wilson)进行嵌套。

Layout.xaml:

<Window
    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:StackOverflow.XAML"
    x:Class="StackOverflow.XAML.Layout"
    Background="{DynamicResource Layout.Background}"
    AllowsTransparency="True"/>

项目文件结构:

XAML
  Layout.xaml
Theme
  Dark.xaml
App.xaml
  

其他信息:

解决方案配置:调试

解决方案平台:x64

Microsoft Visual Studio Community 2019 v16.7.3

更新

由于某种原因,如果您发现我没有使用此属性是因为我必须先验证用户登录名,然后再授予他们访问主窗口的权限,则该问题再次出现在Application StartUpUri中。因此,在App.xaml.cs中的代码后面,我称为Login.ShowDialog()和Layout.ShowDialog()。将此属性设置为“布局窗口”后,我可以注意到该应用程序正在使用Dark.cs下定义的资源,因此,如果我更改此行:

<SolidColorBrush x:Key="Layout.Background">​​Lime</SolidColorBrush>

布局背景在运行时更改为 Lime ,但我想知道是否可以在所有项目中使用App.xaml中定义的相同资源Windows,在我的情况下,StartUpUri将为Login.xaml,如果用户信息正确,则在Login(登录)下,我可以阻止此窗口显示显示我的布局窗口。但是这里的问题是,名为“ Layout.Background”的资源是否可以在两个窗口中使用?

我想到App.xaml中定义的应用程序资源字典就像一个超级全局资源,可以随时使用。这是对的吗?因为这个结果似乎不支持它。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...