【问题标题】:'{DependencyProperty.UnsetValue}' is not a valid value for property 'FocusVisualStyle'“{DependencyProperty.UnsetValue}”不是属性“FocusVisualStyle”的有效值
【发布时间】:2011-07-02 15:14:55
【问题描述】:

我遇到了一个奇怪的错误,我正在尝试调试但没有成功。

我有子类 hwndhost 显示一些内容,我在该类中有以下函数设置为全屏:

    private void SetFullScreen(bool enable)
    {
        if (enable)
        {
            fs = new Window();
            fs.ResizeMode = ResizeMode.NoResize;
            fs.WindowState = System.Windows.WindowState.Maximized;
            fs.WindowStyle = System.Windows.WindowStyle.None;
            fs.Topmost = true;
            fs.PreviewKeyDown += delegate(object sender, KeyEventArgs e) { 
                if (e.Key==Key.Escape)
                    FullScreen = false;
            };
            fs.Show();
        }
        else
        {
            fs.Close();
            fs = null;
        }
    }

这在我的原型 WPF 应用程序中运行良好,但是当我在主应用程序中使用此代码时,在关闭窗口(转义键)和fs.close() 调用时出现此错误:

'{DependencyProperty.UnsetValue}' is not a valid value for property 'FocusVisualStyle'.

奇怪的是它发生在窗口关闭后大约 1500 毫秒。我尝试将 fs 上的 FocusVisualStyle 设置为 null,但它看起来像别的东西。直觉是它试图在我的应用程序中关注另一个没有此属性的元素,但我真的不知道!

谢谢!

编辑。问题是我的全屏按钮上 FocusVisualStyle 的自定义设置。我设置为 {x:Null},问题就消失了。

【问题讨论】:

    标签: wpf dependency-properties hwndhost


    【解决方案1】:

    我的猜测是,当您关闭上述窗口时获得焦点的控件具有您设置的自定义样式,其中不包含任何 FocusVisualStyle。

    所以为了进一步帮助你,你应该多解释一下:当你关闭这个窗口时会发生(或应该发生)什么?

    什么控件类型应该获得焦点?

    【讨论】:

    • 一个切换按钮会启动全屏命令,所以我猜这将重点放在返回上。但是,稍后键盘命令(例如 F12)可能会启动它,因此它可以是具有当前焦点的任何元素。该切换按钮具有自定义样式,我尝试将该样式上的 FocusVisualStyle 设置为 {x:Null} ,但没有成功。
    • 我撒谎,它被设置在两个地方,删除第二个解决了这个问题!
    • 致读者:我得到了错误,因为在某个地方,我没有合并定义我的实际焦点样式的 ResourceDictionary。 XAML 设计器没有抱怨 StaticResource 引用。在运行时以特定方式更改键盘焦点之前,我没有收到任何错误。
    【解决方案2】:

    当 Style 指向 不存在StaticResource 时,可能会发生这种情况。

    此 xaml 失败:

    <Grid.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Height" Value="{StaticResource StandardControlHeight}"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>
    </Grid.Resources>
    

    错误是:

    System.InvalidOperationException: ''{DependencyProperty.UnsetValue}' 不是属性“高度”的有效值。'

    当我添加缺少的StaticResource 时,问题就消失了。

    【讨论】:

      【解决方案3】:

      如果您通过谷歌搜索问题标题:导致此异常的另一种方法是使用Trigger,但忘记设置Value

      例子:

      <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled">
          <Setter Property="Background" Value="Gray" />
        </Trigger>
      </ControlTemplate.Triggers>
      

      这会导致内部异常读取的 XamlParseException:

      '{DependencyProperty.UnsetValue}' 不是属性的有效值 'IsEnabled'。

      更正:

      <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Background" Value="Gray" />
        </Trigger>
      </ControlTemplate.Triggers>
      

      【讨论】:

        【解决方案4】:

        导致上述异常的另一种方法是在使用后声明 StaticResource,例如在样式声明中。

        错误

        <Style TargetType="Label">
            <Setter Property="Foreground" Value="{StaticResource BlueAccent}"/>
        </Style>
        
        <SolidColorBrush x:Key="BlueAccent" Color="#22afed"/>
        

        正确

        <SolidColorBrush x:Key="BlueAccent" Color="#22afed"/>
        
        <Style TargetType="Label">
            <Setter Property="Foreground" Value="{StaticResource BlueAccent}"/>
        </Style>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多