命名空间混乱

时间:2012-03-19 11:48:35

标签: c# wpf xaml namespaces

我有一个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'.

1 个答案:

答案 0 :(得分:0)

尝试设置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);