【问题标题】:DynamicResource Cannot be resolved动态资源无法解析
【发布时间】:2012-03-17 19:14:58
【问题描述】:

我一直在努力学习资源和样式,我想创建一个无镶边窗口。

我有一个示例可以通过以下简单的 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 设计器声明它无法解析资源。

我一直在查看的示例以这种方式进行操作,我无法发现我所看到的所做的与我正在尝试做的之间的区别。

我认为 Genric.xaml 是一个“特殊”资源字典,应该可以通过我的 Window 控件发现 - 我猜这个假设是我的错误。

我需要做什么才能完成这项工作? (现在我知道我可以直接在 Window xaml 中设置这些属性,并且我已经这样做并获得了我想要的效果。但是我真的很想使用 Generic.xaml 资源字典的方式来了解我在此处介绍的方式)

最好的问候

约翰。

【问题讨论】:

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

标签: wpf


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我想我会添加以下示例,以防它帮助其他人。要将资源字典添加到 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>
    

    其中“资源”可以是项目中包含资源字典文件 (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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多