无法从另一个程序集实例化 UserControl - 找不到资源 [英] Cannot instantiate UserControl from another assembly - Resource cannot be found

查看:46
本文介绍了无法从另一个程序集实例化 UserControl - 找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个 WPF 项目的解决方案:Primary 和 Secondary.

I have a solution with two WPF projects: Primary and Secondary.

在 Primary 项目中,名为 PrimaryView 的 Window 实例化了一个名为 SecondaryControl 的 UserControl,在 Secondary 项目中定义.

In the Primary project, a Window named PrimaryView instantiates an UserControl called SecondaryControl, defined in Secondary project.

SecondaryControl 使用在 SecondaryResourceDictionary 中定义的 SecondaryStyle(您可能已经猜到,在 SecondaryProject 中定义).

SecondaryControl uses SecondaryStyle, which is defined in SecondaryResourceDictionary (as you might have guessed already, defined in SecondaryProject).

事实是:当我尝试运行该解决方案时,我得到了一个 XamlParseError,并挖掘了 InnerExceptions 我最终找到了罪魁祸首,ResourceNotFound 错误.

The fact is: when I try to run the solution, I get a XamlParseError, and digging InnerExceptions I eventually find the culprit, the ResourceNotFound error.

所以我的问题是:

  • 如果 SecondaryControl 和它的 SecondaryStyle 定义在同一个程序集中,为什么我不能将它实例化为 PrimaryAssembly?

  • If SecondaryControl and its SecondaryStyle are defined in the same assembly, why can't I instantiate it it PrimaryAssembly?

我应该以某种方式使 PrimaryProject 命名空间可以使用 SecondaryStyle 吗?为什么?

Should I make SecondaryStyle available to PrimaryProject namespace somehow? Why?

推荐答案

我试图通过解释它在我的项目中的工作原理来帮助您.一个单独的程序集包含一个公共控件和公共资源字典,如下所示:

I try to help you by explanation how it works in my projects. A separate assembly contains a common control and common resource dictionary like this:

CommonResources.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--  Some resources  --> 

</ResourceDictionary>

SomeCommonControl.xaml

<UserControl x:Class="YourAssembly.SomeCommonControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <!-- Some specific content -->

</UserControl>

我可以像这样使用来自其他程序集和 WPF 项目的此控件和资源:

I can use this control and resources from another assemblies and WPF-projects like this:

<Window x:Class="YourWPFProject.SomeWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="clr-namespace:YourAssembly">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <common:SomeCommonControl />

</Window>

希望对你有所帮助.

这篇关于无法从另一个程序集实例化 UserControl - 找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆