【问题标题】:VsBrush doesn't work in WPF ResourceDictionaryVsBrush 在 WPF ResourceDictionary 中不起作用
【发布时间】:2015-02-09 22:57:41
【问题描述】:

tl;博士

问题描述

首先,我是 WPF 的新手,我现在几乎不知道自己在做什么......

我正在开发一个 Visual Studio 2013 扩展,我在其中创建了一个自定义工具窗口。由于 WPF 控件不从父级(即 Visual Studio 主窗口)继承主题,因此您必须手动进行。

我读到我应该使用 Microsoft.VisualStudio.Shell.VsBrushesMicrosoft.VisualStudio.PlatformUI.EnvironmentColors 类,因为它们定义了 Visual Studio shell 主题特定的常量。 (See example here.) 只要我在Style 标签中使用它,这一切都很好。

不过,Microsoft 的 Menu and MenuItem ControlTemplate Example 解释了如何使用 <ContentTemplate> 制作正确的 MenyItem 模板。 问题是,VsBrush 和 EnvironmentColors 在<ContentTemplate> 中都不起作用。当我为<GradintStop> 设置颜色时出现一般异常抱怨(没有详细信息,问题是什么),或者 UI 只是挂起,甚至没有加载。在后一种情况下,当我破坏应用程序时,我总是在MS.Win32.UnsafeNativeMethods.GetMessageW() 函数中运行。

问题

  1. 谁能解释我做错了什么以及为什么我不能在我的<ContentTemplate> 中使用VsBrushesEnvironmentColors 类?
  2. 如何使用建议的 <ContentTemplate> 格式正确设置 Visual Studio 扩展的样式?

解决问题的尝试

  1. 我检查了整个包的构造函数,可以获取VsBrushes类的颜色常量。只有在构造函数之后 UI 才会挂起,因此必须在处理 XAML 时初始化 VsBrushes 值。
  2. 如上所述,不使用<ControlTemplate>,可以使用常量。
  3. 调查了异常:当解析器试图解释 <GradientStop> 标记的 Color 属性时抛出异常。没有解释到底是什么失败了。 (一段时间后——代码稍作更改——异常停止,UI 开始挂起。)
  4. 如果我将LinearGradientBrush 更改为SolidColorBrush,问题仍然存在(当然这次的异常略有不同):'Set property 'System.Windows.Media.SolidColorBrush.Color' 抛出异常。李>
  5. 问题不是<MenuItem> 特定的。可以使用<Button> 复制它,我猜想使用任意 WPF 控件。

来源

这是我用来定义 MenuItems 样式的代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vsShell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.12.0"
                    xmlns:vsUI="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.12.0">
    <Color x:Key="MyColor">#FFFFFF00</Color>
    <Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border BorderThickness="1">
                        <Border.BorderBrush>
                            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStopCollection>
                                        <GradientStop Color="{DynamicResource MyColor}" Offset="0.0" />
                                        <GradientStop Color="{DynamicResource {x:Static SystemColors.ActiveBorderColorKey}}" Offset="0.1" />
                                        <GradientStop Color="#FF00FF00" Offset="0.5" />
                                        <!-- The following 3 do not work -->
                                        <GradientStop Color="{DynamicResource {x:Static vsUI:EnvironmentColors.AccentBorderBrushKey}}" Offset="0.8" />
                                        <GradientStop Color="{DynamicResource {x:Static vsShell:VsBrushes.AccentBorderKey}}" Offset="0.8" />
                                        <GradientStop Color="{DynamicResource VsBrush.AccentBorder}" Offset="1.0" />
                                    </GradientStopCollection>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.BorderBrush>
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0,0.5" StartPoint="1,0.5">
                                <GradientStop Color="Blue" Offset="0" />
                                <GradientStop Color="Blue" Offset="1" />
                            </LinearGradientBrush>
                        </Border.Background>
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

调用栈

这是进程“挂起”的地方:

[Managed to Native Transition]  
>   WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetMessageW(ref System.Windows.Interop.MSG msg, System.Runtime.InteropServices.HandleRef hWnd, int uMsgFilterMin, int uMsgFilterMax) Line 566  C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.GetMessage(ref System.Windows.Interop.MSG msg, System.IntPtr hwnd, int minMessage, int maxMessage) Line 391 C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Line 979  C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Line 961  C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.Run() Line 1059 C#
Microsoft.VisualStudio.Shell.12.0.dll!Microsoft.Internal.VisualStudio.PlatformUI.BackgroundDispatcher.ThreadProc(object arg)    Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)    Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart(object obj)  Unknown

【问题讨论】:

    标签: wpf visual-studio xaml contenttemplate


    【解决方案1】:

    分辨率

    以下所有这 3 行都会导致编译错误,因为编译器需要 Color,但我给了它 Brush

    <GradientStop Color="{DynamicResource {x:Static vsUI:EnvironmentColors.AccentBorderBrushKey}}" Offset="0.8" />
    <GradientStop Color="{DynamicResource {x:Static vsShell:VsBrushes.AccentBorderKey}}" Offset="0.8" />
    <GradientStop Color="{DynamicResource VsBrush.AccentBorder}" Offset="1.0" />
    

    我应该改用&lt;GradientStop Color="{DynamicResource {x:Static vsUI:EnvironmentColors.AccentBorderColorKey}}" Offset="0.8" /&gt;(请注意,我使用 AccentBorderColorKey 而不是 AccentBorderBrushKey。)

    XAML 正在解析字符串并试图解释这导致一个简单的事实:XAML 是无类型的(一切都是字符串)。由于错误消息('Set property 'System.Windows.Media.LinearGradientBrush.Color' 引发了异常。)不是很健谈,它并不能真正帮助您了解您做错了什么。

    经验教训

    如果编译器没有为您检查类型(或没有告诉您这是导致问题的原因),请尝试手动检查类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2017-04-20
      • 2011-02-26
      相关资源
      最近更新 更多