检查以下情形(其他人可能也适用),你可以创建项目只需复印件粘贴在这里的代码在正确的文件]:Visual Studio WPF设计器中的错误?
一个 - 建立一个具有基本的东西(Resources.xaml)ResourceDictionary中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="Red" x:Key="Test" />
<Style TargetType="{x:Type GroupBox}" x:Key="Test2" >
<Setter Property="Background" Value="Blue" />
</Style>
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="Green" />
</Style>
</ResourceDictionary>
b - 创建用户控制基站在别人将继承含有基本资源(UserControlBase.cs):
using System.Windows.Controls;
using System;
using System.Windows;
namespace ResourceTest
{
public class UserControlBase : UserControl
{
public UserControlBase()
{
this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("ResourceTest;component/Resources.xaml", UriKind.RelativeOrAbsolute) });
}
}
}
ç - 创建一个用户控件从基部继承(UserControl1.xaml):
<ResourceTest:UserControlBase x:Class="ResourceTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ResourceTest="clr-namespace:ResourceTest"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300" >
<Grid>
<GroupBox BorderBrush="{StaticResource Test}" Margin="3" Header="Test" Style="{DynamicResource Test2}" >
<TextBlock Text="TESTTEST" />
</GroupBox>
</Grid>
</ResourceTest:UserControlBase>
结果:StaticResources得不到解决(并没有加载测试BorderBrush)。 DynamicResources已解决(背景为蓝色),但设计人员说它无论如何都找不到资源(第一次工作正常,但是当您打开/关闭设计器时,资源无法解析)。非命名资源,如TextBlock样式工作正常。
这是一个设计师错误还是我做错了什么?在资源永不改变的场景中,必须声明资源为动态的吗?
在此先感谢。
可以将资源声明为动态,只是效率不高。如果仅在此处使用此ResourceDictionary,是否尝试将它添加到基类的XAML前端中的'UserControl.Resources'中,以查看其行为是否有所不同? –
@WillEddins:在我真正的应用程序中,资源被设置在一个基本的UserControl上,因此每个控件都有必要的资源,以便在WPF设计器上正确地看到它(否则这些样式仅应用于运行时,并且我们无法进行适当的UI设计,除非启动每次更改的应用程序:S)。 –