Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 以编程方式设置HubSection ContentTemplate_Wpf_Templates - Fatal编程技术网

Wpf 以编程方式设置HubSection ContentTemplate

Wpf 以编程方式设置HubSection ContentTemplate,wpf,templates,Wpf,Templates,我正在为Windows8开发一个应用程序,并且正在使用集线器和集线器分区。 我试图做的是根据我获取的JSON创建多个HubSection。 我遇到的问题是,当我试图设置新的HubSection ContentTemplate时,程序崩溃,指向global::System.Diagnostics.Debugger.Break() 问题是,如果我将hs.ContentTemplate设置为已经存在的HubSection的ContentTemplate,它可以正常工作,因此我认为问题与尚未加载模板有关

我正在为Windows8开发一个应用程序,并且正在使用集线器和集线器分区。 我试图做的是根据我获取的JSON创建多个HubSection。 我遇到的问题是,当我试图设置新的HubSection ContentTemplate时,程序崩溃,指向
global::System.Diagnostics.Debugger.Break()


问题是,如果我将hs.ContentTemplate设置为已经存在的HubSection的ContentTemplate,它可以正常工作,因此我认为问题与尚未加载模板有关?

这不会解决您的问题,但值得注意的是,这样做是相当糟糕的做法,原因有很多:

HubSection hs = new HubSection();
hs.ContentTemplate = this.Resources["canteenSectionDataTemplate"] as DataTemplate;
相反,当使用
作为
关键字时,您应该始终检查
null
(除非您毫无疑问地知道它永远不会是
null
,在这种情况下,您可以直接转换值):

除了处理
null
错误外,此代码还允许您检查
canteenSectionDataTemplate DataTemplate
是否为
null


那么关于你的问题,
canteensiondatatemplate DataTemplate
是否等于
null
?如果是,那么从哪里调用此代码?您可能需要推迟它才能正常工作。

我修复了这个问题,这是我的xaml数据模板声明中的一个错误。不管怎样,谢谢你的回答!
HubSection hs = new HubSection();
hs.ContentTemplate = this.Resources["canteenSectionDataTemplate"] as DataTemplate;
HubSection hs = new HubSection();
DataTemplate canteenSectionDataTemplate = this.Resources["canteenSectionDataTemplate"] 
    as DataTemplate;
if (canteenSectionDataTemplate != null) 
    hs.ContentTemplate = canteenSectionDataTemplate;