在“System.Windows.StaticResourceExtension”上提供值时引发了异常

因应需要,写了一个转换器,然后窗体上引用,结果就出来这个错。编译的时候没事,运行阶段就异常。
难道是转换器写错啦?断点调试,发现根本还没运行到转换器代码。仔细一看,系统报的是XAML解释的错,就纯粹是页面这里的问题。
页面代码是这样的:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cvt="clr-namespace:Converters"
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}"> <Window.Resources>
<cvt:GenericTypeConverter x:Key="cvtBool"/>
</Window.Resources> <Grid>
<Button x:Name="btnShowUser" Content="秀一下" Height="50" Width="100" Click="btnShow_Click" />
</Grid> </Window>
寻寻觅觅,冷冷清清凄凄惨惨戚戚,百撕不得骑姐。
后来将 
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}">

这一句从<Window> 移到 <Button>里,结果一切正常。

这说明,之前的错误是因为资源声明在后,而我的调用在前,因此报错了。
怎么办?难道<Window>就不能使用了吗?
解决方法是:将资源声明放到一个独立的XAML文件里,比如起名叫 StaticResources.xaml,然后,在App.xaml里包含这个StaticResources.xaml,最后在窗体就可以正常使用这些资源了。

StaticResources.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cvt="clr-namespace:Converters"
> <cnvtrs:GenericTypeConverter x:Key="cvtBool" /> </ResourceDictionary>

app.xaml

<Application
x:Class="My.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="TestWindow.xaml"> <Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
<ResourceDictionary Source="StaticResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

调用窗体

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}"> <Grid>
<Button x:Name="btnShowUser" Content="秀一下" Height="50" Width="100" Click="btnShow_Click" />
</Grid> </Window>

WPF学习笔记——在“System.Windows.StaticResourceExtension”上提供值时引发了异常的更多相关文章

  1. WPF关于“在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。”问题解决办法

    在WPF中添加样式,在MainWindow.xaml使用自定义按钮FButton时报错,报错信息如下: "System.Windows.Markup.XamlParseException&q ...

  2. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  3. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  4. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  5. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  6. kvm虚拟化学习笔记(三)之windows kvm虚拟机安装

    KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...

  7. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  8. WPF-学习笔记 动态修改控件Margin的值

    原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...

  9. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

随机推荐

  1. fcc html5 css 练习3

    行内样式看起来是这样的 <h1 style="color: green"> .pink-text { color: pink !important; }         ...

  2. GNSS数据下载网站

    Bernese 数据表文件下载 rinex文件下载 ftp://nfs.kasi.re.kr DCB.ION文件ftp://ftp.unibe.ch/AIUB/CODE/ 下载5.0更新文件 ftp: ...

  3. (转) 分布式文件存储FastDFS(一)初识FastDFS

    http://blog.csdn.net/xingjiarong/article/details/50559849 一.FastDFS简介 FastDFS是一款开源的.分布式文件系统(Distribu ...

  4. Git学习总结四(删除)

    一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了: $ rm test.txt 这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻 ...

  5. img、a标签的使用

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  6. 详解proxy_pass、upstream与resolver

    详解proxy_pass.upstream与resolver boldcautious 关注 2018.06.04 10:48 字数 1204 阅读 1434评论 0喜欢 2 应用场景 这里列举几个应 ...

  7. pandas.DataFrame.rank

        原文:https://www.cnblogs.com/sunbigdata/p/7874581.html pandas.DataFrame.rank DataFrame.rank(axis=0 ...

  8. wing ide破解

    LicenseID='CN123-12345-12345-67891' # RequestCode='RL634-8363J-X7E8K-95XD3' RequestCode = 'RW61C-NN6 ...

  9. Free中的buffer和cache理解

    吐血推荐文章: Linux内存中的Cache真的能被回收么? free中的buffer和cache: redhat对free输出的解读 两者都是RAM中的数据.简单来说,buffer是即将要被写入磁盘 ...

  10. hstack()与vstack()函数

    ref: https://blog.csdn.net/csdn15698845876/article/details/73380803 1. hstack()函数 a,b只有一个维度:对第一个维度拼接 ...