我正在尝试UserControl
从我的TreeViewGroupItemTemplateSelector
班级访问 a 中的资源。我无权访问UserControl
班级内的TreeViewGroupItemTemplateSelector
班级。我在格式化Uri
.
PackageExplorer.xaml
是UserControl
并且驻留在也包含TreeViewGroupItemTemplateSelector
该类的本地程序集的根命名空间中。
我收到以下错误:
找不到资源'resources/packageexplorer.xaml'
在 Uri 的实例化期间。
public class TreeViewGroupItemTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var configGroup = item as IConfigurationGroup;
if (configGroup == null)
return null;
var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri
("/myAssembly;component/Resources/myUserControl.xaml",
UriKind.RelativeOrAbsolute);
if (configGroup.ItemType == typeof (ReferencePackageConfigurationItem))
return resourceDictionary["RefreshableHierarchicalDataTemplate"] as DataTemplate;
else
return resourceDictionary["DefaultHierarchicalDataTemplate"] as DataTemplate;
}
}
}
XAML(代码段)如下所示:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<HierarchicalDataTemplate x:Key="DefaultHierarchicalDataTemplate"
DataType="{x:Type localconfig:ConfigurationGroup}"
ItemsSource="{Binding Path=Items}"
ItemTemplateSelector="{StaticResource TreeViewGroupItemTemplateSelector}"/>
<HierarchicalDataTemplate x:Key="RefreshableHierarchicalDataTemplate"
DataType="{x:Type localconfig:ConfigurationGroup}"
ItemsSource="{Binding Path=Items}"
ItemTemplateSelector="{StaticResource TreeViewGroupItemTemplateSelector}"/>
我还尝试访问资源,如下所示,结果相同:
resourceDictionary.Source = new
Uri("/Resources/myUserControl.xaml", UriKind.RelativeOrAbsolute);
和:
resourceDictionary.Source = new
Uri("myUserControl.xaml", UriKind.RelativeOrAbsolute);