title author date CreateTime categories
win10 uwp 提示 Cannot find a Resource with the Name Key 找不到资源
lindexi
2019-11-28 08:51:54 +0800
2019-11-28 8:31:5 +0800
Win10 UWP

在写 UWP 界面如果没有写对资源的顺序,那么在加载到对应的界面会在提示上面信息

堆栈小伙伴问了一个问题,在他的程序启动提示下面代码

Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.
Cannot find a Resource with the Name/Key ItemTemplateSelector [Line: 66 Position: 19]'

这个问题其实是 UWP 的 XAML 界面提示做的不好的原因,比较难简单从提示信息里面找到对应的问题

其实上面提示说的是在 66 行没有找到资源名叫 ItemTemplateSelector 的资源,那么 UWP 的资源是如何寻找的?在 UWP 将会通过顺序查找资源,按照当前所在的范围一直往上找,直到找到第一个资源。那么什么是按照当前所在的范围一直往上找,在 UWP 的界面布局是一棵树,将会从控件本身资源开始找,然后找控件的容器是否存在资源,如果找不到,就找控件的容器的容器的资源

但是除了上面的规则,还有一个规则就是按照代码写的上下顺序找,也就是资源需要写到寻找资源的代码之前。小伙伴的代码有点多,我将代码放在github就不再这里贴细节

    <Grid>
<GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Margin="80, 40, 60, 40" BorderThickness="0"
ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="Margin" Value="0, 0, 0, 32"/>
</Style>
</GridView.ItemContainerStyle>
</GridView>
</Grid> <Page.Resources>
<local:ItemTemplateSelector x:Key="ItemTemplateSelector"
Template1="{StaticResource Template1}"
Template2="{StaticResource Template2}"> </local:ItemTemplateSelector> <DataTemplate x:Key="Template1" >
<local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
</DataTemplate>
<DataTemplate x:Key="Template2" >
<local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
</DataTemplate>
</Page.Resources>

这里 ItemTemplateSelector="{StaticResource ItemTemplateSelector}" 是第66行,也就是 ItemTemplateSelector 这个资源找不到,在上面代码可以看到在 Page.Resources 里面有定义,为什么会找不到。按照第二个规则需要在使用资源之前,也就是需要将页面定义放在最前

    <Page.Resources>
<DataTemplate x:Key="Template1" >
<local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
</DataTemplate>
<DataTemplate x:Key="Template2" >
<local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
</DataTemplate> <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
Template1="{StaticResource Template1}"
Template2="{StaticResource Template2}"> </local:ItemTemplateSelector>
</Page.Resources> <Grid>
<GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Margin="80, 40, 60, 40" BorderThickness="0"
ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="Margin" Value="0, 0, 0, 32"/>
</Style>
</GridView.ItemContainerStyle>
</GridView>
</Grid>

修改的代码放在 github 欢迎小伙伴访问

如果看到在 UWP 提示下面代码,那么应该就是找不到资源,找不到资源可能的原因是资源名写错了,或者资源定义在使用后或者从这个控件往上找不到这个资源

无法找到与此错误代码关联的文本。

Cannot find a Resource with the Name/Key ItemTemplateSelector [Line: 16 Position: 11]

提示这个代码的堆栈如下

   在 Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
在 LawhallwheachalNakearjalle.MainPage.InitializeComponent()
在 LawhallwheachalNakearjalle.MainPage..ctor()

请看下面代码,在 Grid 用到了没有定义的 Foo 资源

    <Page.Resources>

    </Page.Resources>

    <Grid Background="{StaticResource Foo}">

    </Grid>

此时将提示下面代码

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Windows.UI.Xaml.Markup.XamlParseException: 无法找到与此错误代码关联的文本。

Cannot find a Resource with the Name/Key Foo [Line: 15 Position: 11]
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at LawhallwheachalNakearjalle.MainPage.InitializeComponent()
at LawhallwheachalNakearjalle.MainPage..ctor()

请看下面代码,虽然有定义资源,但是定义资源在使用的代码之后

    <Grid Background="{StaticResource Foo}">

    </Grid>

    <Page.Resources>
<SolidColorBrush x:Key="Foo">#565656</SolidColorBrush>
</Page.Resources>

建议将资源写在最前

请看下面代码,虽然有定义资源,但是定义资源在控件往上找不到的控件

    <Grid>
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="Foo">#565656</SolidColorBrush>
</Grid.Resources>
</Grid> <Grid Background="{StaticResource Foo}"> </Grid>
</Grid>

此时 Grid 的容器没有资源

2019-11-28-win10-uwp-提示-Cannot-find-a-Resource-with-the-Name-Key-找不到资源的更多相关文章

  1. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  2. Win10 UWP开发系列:实现Master/Detail布局

    在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...

  3. Win10/UWP开发—使用Cortana语音与App后台Service交互

    上篇文章中我们介绍了使用Cortana调用前台App,不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互,这篇我们讲讲如何使用Cortana调用App的后台任务,相比 ...

  4. 【Win10 UWP】QQ SDK(二):SDK的回调处理

    上一讲,我们介绍了QQ SDK的使用方法,请看<[Win10 UWP]QQ SDK(一):SDK基本使用方法> 一. 回调的基本形式 从前面的介绍中我们知道,我们的应用和QQ客户端之间需要 ...

  5. win10 uwp DataContext

    本文告诉大家DataContext的多种绑法. 适合于WPF的绑定和UWP的绑定. 我告诉大家很多个方法,所有的方法都有自己的优点和缺点,可以依靠自己喜欢的用法使用.当然,可以在新手面前秀下,一个页面 ...

  6. win10 uwp 车表盘 径向规

    车表盘就是有刻度的圆盘加上针,这个控件可以直观让用户知道当前的速度或其他 看名字不知道是什么,我就放一张图 使用很简单,在Nuget,Radial Gauge 要使用大神做的,简单,在使用我们需要在N ...

  7. win10 uwp 如何打包Nuget给其他人

    原文:win10 uwp 如何打包Nuget给其他人 本文告诉大家,如果自己有做一些好用的库,如何使用 Nuget 打包之后上传,分享给大家. 首先需要知道一些 Nuget 打包需要知道的,请看 wi ...

  8. win10 uwp 使用 Microsoft.Graph 发送邮件

    在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...

  9. win10 uwp 商业游戏 1.1.5

    本文是在win10 uwp 商业游戏 基础上继续开发,添加一些无聊的游戏 因为在发布几个月,下载量很少,小伙伴说游戏就玩不到几分钟就不想玩,于是我就想加入其他游戏 下面我来告诉大家如何在游戏中添加多个 ...

  10. Win10 UWP开发实现Bing翻译

    微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...

随机推荐

  1. Windows操作系统Apache服务器下配置PHP

    在Apache web服务器上发布PHP项目之前,需要进行相应的配置,服务器才能解析php文本,正常显示php动态页面内容.在进行php配置之前默认已经在Windows系统下安装好了Apache服务器 ...

  2. 【Python】学习笔记五:缩进与选择

    Python最具特色的用缩进来标明成块的代码 缩进 i = 4 j = 2 if i > j: i = i+1 print(i) 这是一个简单的判断,Python的if使用很简单,没有括号等繁琐 ...

  3. 后盾网lavarel视频项目---页面post方式提交之后动态弹出错误信息

    后盾网lavarel视频项目---页面post方式提交之后动态弹出错误信息 一.总结 一句话总结: 1.思路和我想的一样,有错误的时候弹出提示错误消息的模态框就好,没有错误的时候不管它 2.把模态框的 ...

  4. JNI写本地日志文件

    调试JNI库 我喜欢反编译APK 然后替换.so文件 然后再编译成APK 其中写日志的话 用fopen("/sdcard/lei.txt","wb+")

  5. leetcode-mid-math-172. Factorial Trailing Zeroes-NO-????

    mycode 问题:为甚在小于200的时候,答案ok,大于等于200的时候,就少一个1??? class Solution(object): def trailingZeroes(self, n): ...

  6. rosbag 那些事

    ..bag文件转.txt 将file_name.bag文件中topic_name话题的消息转换到Txt_name.txt文件中: rostopic echo -b file_name.bag -p / ...

  7. 【c++进阶:c++ algorithm的常用函数】

    c++ algorithm的常用函数 https://blog.csdn.net/hy971216/article/details/80056933 reverse() reverse(it,it2) ...

  8. HTTPS 证书制作及使用

    一 证书的制作 进入jdk/bin,使用keytools.exe制作证书. 1.创建keystore 创建一个别名为serverkeystore的证书,该证书存放在名为server.keystore的 ...

  9. PHP操作json

    输出json文件中文处理 <?php $json_array = array(); // 1.转换为json字符串(不自动转换为unicode编码) if (version_compare(PH ...

  10. c# 窗口关闭方法

    背景:点击datagridview某条信息弹出信息详情窗口,当连续点击时需要关闭之前的详情窗口. 实现方式: 父窗口中 全局创建子窗口(MsgDetailFrm  ): MsgDetailFrm de ...