'{DependencyProperty.UnsetValue}'不是属性“FocusVisualStyle”的有效值;
我有一个奇怪的错误,我正在尝试调试,但没有成功。
我已经对显示一些内容的 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}' 不是属性 'FocusVisualStyle' 的有效值。
奇怪的是它发生在大约 1500 毫秒窗口关闭后。我尝试将 fs
上的 FocusVisualStyle 设置为 null,但它看起来像其他东西。直觉是它试图集中我的应用程序中没有此属性的另一个元素,但实际上我不知道!
谢谢!
编辑。问题是我的全屏按钮上 FocusVisualStyle 的自定义设置。我设置为 {x:Null} 然后问题就消失了。
I have a weird error I'm trying to debug with no luck.
I have subclassed hwndhost showing some content, I have the following function in that class to set to fullscreen:
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;
}
}
This worked fine in my prototype WPF app but when I use this code in my main app I get this error when closing the window (escape key) and on fs.close()
call:
'{DependencyProperty.UnsetValue}' is not a valid value for property 'FocusVisualStyle'.
The weird thing is it happens about 1500ms AFTER the window closes. I've tried setting FocusVisualStyle on fs
to null, but it looks like something else. Gut feeling is it's trying to focus another element in my app that doesn't have this property, but really I have no idea!
Thanks!
Edit. Problem was custom setting of FocusVisualStyle on my fullscreen button. I set to {x:Null} and the problem went away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当样式指向不存在的
StaticResource
时,就会发生这种情况。此 xaml 失败:
错误是:
当我添加缺少的
StaticResource
时,问题就消失了。This can happen when a Style is pointing to a
StaticResource
that does NOT exist.This xaml was failing:
The error was:
When I added the missing
StaticResource
, the problem went away.我的猜测是,当您关闭提到的窗口时获得焦点的控件具有您设置的自定义样式,该样式不包含任何 FocusVisualStyle。
因此,为了进一步帮助您,您应该多解释一下:关闭此窗口时会发生(或应该发生)什么?
哪种控件类型应该获得焦点?
my guess is that the control that gets the focus when you close the mentioned window has a custom style set by you that does not include any FocusVisualStyle.
so to help you further, you should explain a bit more: what happens (or should happen) when you close this window?
what control type is supposed to get the focus?
导致上述异常的另一种方法是在使用 StaticResource 之后声明它,例如在样式声明中。
错误
正确
Yet another way to cause mentioned exception is when you declare StaticResource after using it, for example in style declaration.
WRONG
CORRECT
如果您通过谷歌搜索问题标题来到这里:导致此异常的另一种方法是使用
触发器
,但忘记设置值
。示例:
这会导致 XamlParseException,其中内部异常如下:
更正:
In case you got here by Googling the question title: Another way to cause this exception is to use a
Trigger
, but forget to set theValue
.Example:
This causes a XamlParseException where the inner exception reads:
Correction:
另一种情况是声明了
StaticResource
但在提及时未看到。例如,在我的例子中:
在应用程序启动之前,在
App.xaml.cs
文件中的OnStartup
方法中发生。异常消息提到了
Background
属性,它告诉我们:在某些
或
WPF开始在层次结构中寻找xxx,如果
没有找到类似的东西,就会发生异常。
就我而言,我已将样式放在一个单独的项目中,就像这样:
我认为
ToggleButtonStyles.xaml
中的样式会因为声明的顺序而看到 xxx (ButtonStyles .xaml
在上面,会在ToggleButtonStyles.xaml
之前合并),结果是错误的!解决方案1.
将
StaticResource
替换为DynamicResource
这样,如果在启动时找不到xxx,WPF不会抛出异常,而是会使用之后就如预期的那样。
解决方案 2.
将所有颜色提取到单独的 ResrouceDictionary 中,并将其合并到 SharedResources.xaml 之前的 App.xaml 中
Another situation is when the
StaticResource
is declared but not seen when mentioned..For example, in my case:
Occurs at
OnStartup
method inApp.xaml.cs
file, before the app launch..The exception message mentioned the
Background
property, it tells that:At some
or
WPF started looking for xxx in the hierarchy, and if somthing like
was not found, the exception will occur.
In my case, I have put my styles in a separate project, It was like this:
I thought that the styles in
ToggleButtonStyles.xaml
will see xxx because of the order of the declaration (ButtonStyles.xaml
is above and will be merged beforeToggleButtonStyles.xaml
), it turns out to be wrong!Solution 1.
Replace
StaticResource
withDynamicResource
This way, WPF will not throw an exception if xxx is not found at launch time, but it will use it afterwards as expected.
Solution 2.
Extract all the colors to a separate ResrouceDictionary and merge it in App.xaml before SharedResources.xaml
此异常的另一个原因可能是将属性值设置为错误的类型。例如,请考虑以下 XAML 代码:
在此代码中,以 ColumnDefinition 开头的行将导致上述异常(由于 ColumnDefinition.Width 属性的类型不是 System.Double 值而是 System.Windows.GridLength 结构,因此抛出 ArgumentException: '200' 不是属性 'Width' 的有效值) . 要解决此问题,首先在 Grid.Resources 块中定义一个新资源
,并将错误行替换为以下行:
实际上,当我们将属性设置为错误类型时,Visual Studio 通过在属性下划线来警告我们并指出“资源‘DoubleWidth’具有不兼容的类型。”但无论如何都能编译。
Yet another reason of this exception can be setting a property value to a wrong type. For instance, consider the following XAML code:
In this code the line starting with ColumnDefinition will cause the aforementined exception (ArgumentException: '200' is not a valid value for property 'Width') to be thrown since the type of the ColumnDefinition.Width property is not a System.Double value but a System.Windows.GridLength struct. To fix the issue, first define a new resource in the
Grid.Resources
block asand replace the erroneous line with this line:
Actually when we set a property to a wrong type, Visual Studio warns us with underlining the property and saying "The resource 'DoubleWidth' has an incompatible type." but compiles anyway.
对于我来说,这段代码中发生了同样的错误:
我以这种方式编写了样式:
现在我收到如下错误:
“{DependencyProperty.UnsetValue}”不是属性“Visibility”的有效值,
因此我了解 Visibility 属性设置不正确。然后我做了一些排列和组合,找到了正确的方法,如下所示:
在 usercontrol xaml 中,我更改了代码,如下所示:
所以我理解的基本点是它显示错误的属性退出查找
For me the same error happened in this code :
I have written the style in this way :
Now I was getting error like :
'{DependencyProperty.UnsetValue}' is not a valid value for property 'Visibility'
So I understood the Visibility property is not set properly. Then I done some permutation and combination and found the right way as follows :
and in usercontrol xaml I have changed the code as follows :
So the basic point I understood is whichever the property it is showing error exits look