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 的容器没有资源
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号