我一直在努力学习资源和风格,我想创建一个无色的窗口。
我有一个例子,它通过以下简单的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是一个“特殊的”资源字典,应该可以被我的窗口控件发现--我猜这个假设就是我的错误。
我需要做些什么才能让它正常工作?(现在我明白了,我可以直接在窗口xaml中设置这些属性,我已经这样做了,并获得了我想要的效果。但我真的想要理解并使用我在这里介绍的Generic.xaml资源字典的方式)
诚挚的问候
约翰。
发布于 2012-03-18 05:29:38
Themes/generic.xaml文件自动用于查找自定义控件的默认样式。在您的示例中,您有一个具有自定义样式的普通窗口。您不能在Window.Resources节中定义此样式,因为应在更高级别定义此样式。唯一更高级别的窗口是App.xaml,因为该窗口实际上是它的子窗口。这就是为什么你的问题的解决方案是将样式放在App.Resources部分中。
发布于 2017-07-04 04:20:33
我想我会添加下面的例子,以防它对其他人有所帮助。要向app.xaml文件添加资源字典,可以向app.xaml文件添加以下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>https://stackoverflow.com/questions/9752929
复制相似问题