简体   繁体   English

如何从.net类访问UserControl资源

[英]How do I access a UserControl resource from a .net class

I am trying to access a resource in a UserControl from my TreeViewGroupItemTemplateSelector class. 我试图从我的TreeViewGroupItemTemplateSelector类访问UserControl的资源。 I do not have access to the UserControl class within the TreeViewGroupItemTemplateSelector class. 我无权访问TreeViewGroupItemTemplateSelector类内的UserControl类。 I am having problems formatting the Uri . 我在格式化Uri时遇到问题。

PackageExplorer.xaml is the UserControl and resides in the root namespace of the local assembly which also contains the TreeViewGroupItemTemplateSelector class. PackageExplorer.xamlUserControl ,位于本地程序集的根名称空间中,该程序集还包含TreeViewGroupItemTemplateSelector类。

I am getting the following error: 我收到以下错误:

Cannot locate resource 'resources/packageexplorer.xaml' 找不到资源“ resources / packageexplorer.xaml”

During the instantiation of the Uri. 在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;

        }
    }
}

The XAML (snippet) looks as follows: 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}"/>

I've also tried accessing the resource as shown below with the same results: 我还尝试过访问资源,如下所示,结果相同:

    resourceDictionary.Source = new 
         Uri("/Resources/myUserControl.xaml", UriKind.RelativeOrAbsolute);

and: 和:

resourceDictionary.Source = new 
     Uri("myUserControl.xaml", UriKind.RelativeOrAbsolute);

it's not clear what you're trying to achieve - but as I understand... 目前尚不清楚您要达到的目标-但据我了解...

If you're trying to load the entire user-control XAML file - as a resource 如果您尝试加载整个用户控制XAML文件-作为资源

User control XAML is not a 'resource dictionary' - I'm not sure as to what error that might produce. 用户控件XAML不是“资源字典”-我不确定可能产生什么错误。

But just separate things . 但是只是分开的事情 You can use the MergeDictionary to merge any outside 'resource dictionary' XAML into UserControl.Resources - but keep that 'resource' as file on its own - load that, not entire user control, hope that helps 您可以使用MergeDictionary将任何外部“资源字典” XAML合并到UserControl.Resources中,但可以单独保留该“资源”作为文件-加载而不是整个用户控件,希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM