Wpf 无法解析DynamicSource

Wpf 无法解析DynamicSource,wpf,Wpf,我一直在努力学习资源和风格,我想创建一个无铬窗口 我有一个例子,它通过以下简单的xaml摘录实现了我想要的 我在Themes/Generic.xaml中有一个资源集 <Style x:Key="BorderlessWindowStyle" TargetType="{x:Type Window}"> <Setter Property="AllowsTransparency" Value="true" /> <Setter Property="Windo

我一直在努力学习资源和风格,我想创建一个无铬窗口

我有一个例子,它通过以下简单的xaml摘录实现了我想要的

我在Themes/Generic.xaml中有一个资源集

<Style x:Key="BorderlessWindowStyle" TargetType="{x:Type Window}">
    <Setter Property="AllowsTransparency" Value="true" />
    <Setter Property="WindowStyle" Value="None" />
    <Setter Property="ResizeMode" Value="CanResizeWithGrip" />
    <Setter Property="Background" Value="Transparent" />
</Style>

我有一个主窗口:

<Window x:Class="Project1.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Project1"
    x:Name="Window"
    Title="Shell" Height="576" Width="1024" Style="{DynamicResource BorderlessWindowStyle}">
<Grid></Grid>

但是该样式未被应用,并且VS designer声明它无法解析资源

我所看到的例子就是这样做的,我无法发现我所看到的和我正在尝试做的之间的区别

我认为Genric.xaml是一个“特殊”的资源字典,应该可以被我的窗口控件发现——我猜这个假设是我的错误

我需要做些什么才能让它工作?(现在我知道我可以直接在窗口xaml中设置这些属性,我已经这样做了,并获得了我想要的效果。但是我真的想使用Generic.xaml资源字典的方式来理解satnd,正如我在这里介绍的那样)

致意


John。

Themes/generic.xaml文件自动用于查找自定义控件的默认样式。在您的情况下,您有一个具有自定义样式的普通窗口。您不能在
Window.Resources
部分中定义此样式,因为该样式应在更高级别上定义。唯一较高级别的窗口是App.xaml,因为该窗口实际上是它的子窗口。这就是为什么您的问题的解决方案是将样式放入
App.Resources
部分。

Themes/generic.xaml文件将自动用于查找自定义控件的默认样式。在您的情况下,您有一个具有自定义样式的普通窗口。您不能在
Window.Resources
部分中定义此样式,因为该样式应在更高级别上定义。唯一较高级别的窗口是App.xaml,因为该窗口实际上是它的子窗口。这就是为什么您的问题的解决方案是将样式放入
App.Resources
部分。

我想添加以下示例,以防它帮助其他人。要将资源字典添加到app.xaml文件中,可以将以下xaml代码添加到app.xaml文件中

<Application x:Class="ProjectX.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:ProjectX"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Use the Black skin by default -->
            <ResourceDictionary Source="Resources\ResourceFile.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

其中“Resources”可以是项目中包含资源字典文件(ResourceFile.xaml)的文件夹

您可以将代码添加到资源字典中,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ProjectX.Resources">

<!-- The Background Brush is used as the background for the Main Window -->
<SolidColorBrush x:Key="MainBackgroundBrush" Color="#FF202020" />

</ResourceDictionary>

最后,动态绑定到资源字典,执行以下操作:

<Window
  x:Class="ProjectX.MainWindow"
  Title="Family.Show" Height="728" Width="960"
  Background="{DynamicResource MainBackgroundBrush}"
  ResizeMode="CanResizeWithGrip">
</Window>

我想我会添加以下示例,以防它帮助其他人。要将资源字典添加到app.xaml文件中,可以将以下xaml代码添加到app.xaml文件中

<Application x:Class="ProjectX.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:ProjectX"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Use the Black skin by default -->
            <ResourceDictionary Source="Resources\ResourceFile.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

其中“Resources”可以是项目中包含资源字典文件(ResourceFile.xaml)的文件夹

您可以将代码添加到资源字典中,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ProjectX.Resources">

<!-- The Background Brush is used as the background for the Main Window -->
<SolidColorBrush x:Key="MainBackgroundBrush" Color="#FF202020" />

</ResourceDictionary>

最后,动态绑定到资源字典,执行以下操作:

<Window
  x:Class="ProjectX.MainWindow"
  Title="Family.Show" Height="728" Width="960"
  Background="{DynamicResource MainBackgroundBrush}"
  ResizeMode="CanResizeWithGrip">
</Window>


您是否尝试运行您的程序;这样行吗?可能是VS designer在这方面受到限制,因为DynamicResources是在运行时而不是在编译时进行计算的。是的,我确实运行了应用程序,但没有应用样式。请尝试将Generic.xaml更改为Generic.xaml。不幸的是,这也不起作用。为了最初创建Generic.xaml,我创建了一个自定义控件,以便Visual studio为我添加它,然后删除了不需要的自定义控件。我这样做是为了消除任何人为错误的可能性。我把它添加到App.xaml中,现在它可以工作了。我认为对于Generic.xaml,这不是必需的。我有点困惑。你是否试过运行你的程序;这样行吗?可能是VS designer在这方面受到限制,因为DynamicResources是在运行时而不是在编译时进行计算的。是的,我确实运行了应用程序,但没有应用样式。请尝试将Generic.xaml更改为Generic.xaml。不幸的是,这也不起作用。为了最初创建Generic.xaml,我创建了一个自定义控件,以便Visual studio为我添加它,然后删除了不需要的自定义控件。我这样做是为了消除任何人为错误的可能性。我把它添加到App.xaml中,现在它可以工作了。我认为对于Generic.xaml,这不是必需的。我有点困惑。