如何检测动态加载的其他DataTemplate引用的未定义DataTemplate?

时间:2013-10-30 18:37:20

标签: wpf

在我们的应用程序中,我们根据用例在运行时动态加载一些DataTemplates。而这些DataTemplate引用了另一层DataTemplates,这可以持续两到三层。 由于有许多DataTemplate定义,我们可能会错过定义一个较低层DataTemplates。

糟糕的是,VisualStudio不会警告缺少的DataTemplate,并且构建在没有相关警告的情况下运行正常,并且在运行时抛出异常。

如果没有编译器辅助工具,那么获得无错误代码绝对是人为的努力。 任何工具都可以帮助检测未定义但引用的DataTemplate?

非常感谢。

例如,以下简化的XAML说明了这个问题。 Layer1 DataTemplates指的是Layer2 DataTemplates。 Layer1_0x00010D83_Layer2List_0x00010712_0x00010719在运行时动态加载,它指的是Layer2_0x00010712和Layer2_0x00010719。如果未定义Layer2_0x00010719,则编译正常,但在运行时抛出错误。

    <DataTemplate  x:Key="Layer2_0x00010712">
        <StackPanel >
            <TextBlock Text="{Binding Converter={StaticResource NameConverter}}"
                       Style="{StaticResource NameTextBlockStyle}"  />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="Layer1_0x00010D83_Layer2List_0x00010712_0x00010719">
        <local:Layer1Border x:Name="" Layer1ID="0x00010D83" >
            <Grid>
                <local:Layer2Border Grid.Column="0" Grid.Row="1"
                    <Button Content="{Binding}" ContentTemplate="{StaticResource Layer2_0x00010712}"/>
                </local:Layer2Border>
                <local:Layer2Border Grid.Column="1" Grid.Row="1"
                    <Button Content="{Binding}" ContentTemplate="{StaticResource Layer2_0x00010719}"/>
                </local:Layer2Border>
            </Grid>
        </local:Layer1Border>
    </DataTemplate>

0 个答案:

没有答案
相关问题