2019-11-28-win10-uwp-提示-Cannot-find-a-Resource-with-the-Name-Key-找不到资源
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 的容器没有资源
最新文章
- Yii2 ActiveRecord save失败
- 理解 Cinder 架构 - 每天5分钟玩转 OpenStack(45)
- MMORPG大型游戏设计与开发(服务器 游戏场景 聊天管道和寻路器)
- SVN服务器搭建和使用(二)
- python之正则表达式
- json格式的日期格式化
- Nancy 学习-自宿主 继续跨平台
- BZOJ2453维护队列&;&;BZOJ2120数颜色
- 如何禁用IE10的明文显示密码和快速清除功能
- Homebrew- MAC上的包管理利器
- IOSFramework打包。
- Android 多屏幕适配
- Codeforces Round #206 (Div. 2)
- 《OpenCL异构计算》新版中译本派送中!
- UVa 1658 Admiral(最小费用最大流)
- HDU 5868 Different Circle Permutation
- documentsUI源码分析
- Distance on the tree(数剖 + 主席树)
- redis quick start
- tomcat操作
热门文章
- sqli-labs(6)
- [CSP-S模拟测试]:123567(莫比乌斯函数+杜教筛+数论分块)
- 【后台管理系统】—— Ant Design Pro页面相关(二)
- MS入门学习笔记
- 1、Shiro简介以及整体架构
- Oracle 无备份情况下的恢复--控制文件/数据文件
- oracle 11g 数据库恢复技术 --rman catalog
- Gradle之Android Gradle Plugin 主要 Task 分析(三)
- virtualbox压缩虚拟机硬盘文件vhd
- 第八周课程总结&;实验报告六