当从外部 xap 加载并且程序集由 MEF 提供时,如何获取 ResourceDictionary 样式?

发布于 2024-09-05 18:54:23 字数 687 浏览 9 评论 0原文

我有以下设置:

主应用程序加载带有 IPlugin 实现的 XAP。该插件包含一个“DisplayPanel”,其中包含引用的控件和其他控件。这里的 DisplayPanel 只是一个容器控件,用于显示引用的 Control。

此引用的控件来自程序集,使用此程序集中的 ResourceDictionary xaml 中的样式。至少那是我想要拥有的。问题是引用的 Control 抛出错误:

无法找到具有名称/键 PlayerPanelGrad 的资源 [行:1500 位置:127]

我尝试通过合并资源字典引用来引用 theResourceDictionary 来获取样式:

       <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="TableControls;component/ControlsStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

但这不起作用。

你会如何处理这个问题?

I've got the following setup:

The main application loads a XAP with an IPlugin implementation. The Plugin contains a 'DisplayPanel' that contains a referenced Control with other controls. The DisplayPanel here is simply a container control to show referenced Control.

This referenced Control, from an assembly, uses a Style from a ResourceDictionary xaml in this assembly. At least that's what I want to have. The problem is that the referenced Control throws an error:

Cannot find a Resource with the Name/Key PlayerPanelGrad [Line: 1500
Position: 127]

I've tried to get at the style by referencing theResourceDictionary through a Merged Resource dictionary reference:

       <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="TableControls;component/ControlsStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

But that doesn't work.

How would you approch this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最笨的告白 2024-09-12 18:54:23

我让它工作的唯一方法是在 InitializeComponent 调用之前以编程方式将资源字典加载到控件(在类库中):

public ActionPanel()
{
     StreamResourceInfo sr = Application.GetResourceStream(
          new Uri("TableControls;component/ControlsStyle.xaml", UriKind.Relative));
     Application.Current.Resources.Add("plop",sr.Stream);
     // Required to initialize variables
     InitializeComponent();
}

the only way i got it to work is by loading the Resource dictionary into the control (in a Class Library) programmatically before the InitializeComponent call:

public ActionPanel()
{
     StreamResourceInfo sr = Application.GetResourceStream(
          new Uri("TableControls;component/ControlsStyle.xaml", UriKind.Relative));
     Application.Current.Resources.Add("plop",sr.Stream);
     // Required to initialize variables
     InitializeComponent();
}
这个俗人 2024-09-12 18:54:23

这个问题可能会有所帮助,但老实说,我仍在尝试自己解决这个问题:
使用 MEF 导入 WPF 数据模板?

This question may be of help, although, honestly, I'm still trying to figure it out myself:
Using MEF to import a WPF DataTemplate?

空袭的梦i 2024-09-12 18:54:23

//先动态加载other.dll,然后使用以下代码:

       StreamResourceInfo srf = Application.GetResourceStream(new Uri("otherdll;component/Resources/Brush.xaml", UriKind.Relative));

        StreamReader sr = new StreamReader(srf.Stream);
        string stt = sr.ReadToEnd();
        ResourceDictionary dict = XamlReader.Load(stt) as ResourceDictionary;
        Application.Current.Resources.MergedDictionaries.Add(dict);

//load other.dll dynamically first,and then use the following code:

       StreamResourceInfo srf = Application.GetResourceStream(new Uri("otherdll;component/Resources/Brush.xaml", UriKind.Relative));

        StreamReader sr = new StreamReader(srf.Stream);
        string stt = sr.ReadToEnd();
        ResourceDictionary dict = XamlReader.Load(stt) as ResourceDictionary;
        Application.Current.Resources.MergedDictionaries.Add(dict);

为了将来参考,我的 XAML 文件位于需要 / 字符的解决方案的子目录中,但该文件还位于其中名为 Assets 的子目录中。

<ResourceDictionary 
     Source="/MyAssemblyName;component/Assets/RadResources.xaml" />

此外,.XAML 文件在解决方案中构建为 Page

For future reference, my XAML file was found in a subdirectory of the solution which needed the / character but also file was further in a subdirectory named Assets within it.

<ResourceDictionary 
     Source="/MyAssemblyName;component/Assets/RadResources.xaml" />

Also the .XAML file was built as Page in the solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文