【问题标题】:Namespace Confusion命名空间混乱
【发布时间】:2012-04-03 21:42:12
【问题描述】:

我有一个 WPF 应用程序,我正在尝试将其转换为 DLL。问题是我在 App.XAML 中设置了许多应用程序级资源。在 dll 中,我试图以编程方式设置我的资源。

要以编程方式加载我的资源,我有这个:

ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("C:\\Users\\Ash\\Documents\\Visual Studio 2010\\Projects\\moo Process Flow Manager\\moo Process Flow Manager\\Resources\\Styles\\Shared.xaml");
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("C:\\Users\\Ash\\Documents\\Visual Studio 2010\\Projects\\moo Process Flow Manager\\moo Process Flow Manager\\Resources\\Styles\\ToolBar.xaml");
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("C:\\Users\\Ash\\Documents\\Visual Studio 2010\\Projects\\moo Process Flow Manager\\moo Process Flow Manager\\Resources\\Styles\\ZoomBox.xaml");
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

这一行:

myResourceDictionary.Source = new Uri("C:\\Users\\Ash\\Documents\\Visual Studio 2010\\Projects\\moo Process Flow Manager\\moo Process Flow Manager\\Resources\\Styles\\ZoomBox.xaml");

在运行时抛出以下错误:

'Failed to create a 'Type' from the text 's:ZoomBox'.' Line number '5' and line position '12'.

内部例外:

{"Type reference cannot find type named '{clr-namespace:Arkitec.moo.ProcessFlowManager.Controls}ZoomBox'."}

这是导致 zoombox.xaml 中错误的行:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:Arkitec.moo.ProcessFlowManager.Controls">

    <Style TargetType="{x:Type s:ZoomBox}">

这是在 zoombox.cs 中声明该类的位置:

namespace Arkitec.moo.ProcessFlowManager.Controls
{
    public class ZoomBox : Control
    {

如果您需要更多信息,请告诉我。

编辑:根据我试过的 Guilammes 回答:

myResourceDictionary.Source = new Uri("Resources/Styles/Shared.xaml", UriKind.Relative);

但是得到以下错误:

Cannot locate resource 'resources/styles/shared.xaml'.

【问题讨论】:

  • 我删除了我的答案,因为你编辑了你的问题。我希望有人能帮助你更多

标签: c# wpf xaml namespaces


【解决方案1】:

尝试如下设置 shared.xaml 属性:

Build Action --> Page
copy to output directory --> do not copy
custom tool --> MSBuild:Compile

然后按如下方式引用 xaml

new System.Uri("/YourNamespace;Resources/Styles/Shared.xaml", System.UriKind.Relative);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2011-11-26
    • 2012-09-06
    • 2010-11-19
    相关资源
    最近更新 更多