wpf 向 xaml 添加自定义命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3297660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:56:52  来源:igfitidea点击:

adding a custom namespace to xaml

wpfxamlnamespaces

提问by cemregoksu

I am quite new on C#, WPF and XAML, so I may not be able to use the right terms to ask the right question =) I am trying to add my own namespace to my xaml file in order to use my own class easily -I guess the reason is this- I wrote the following code in window tag for this:

我对 C#、WPF 和 XAML 很陌生,所以我可能无法使用正确的术语来提出正确的问题 =) 我正在尝试将我自己的命名空间添加到我的 xaml 文件中,以便轻松使用我自己的类 -我想原因是这个 - 我为此在 window 标签中编写了以下代码:

xmlns:myns="clr-namespace:LibNameSpace" 

Where my window tag also starts with the following definition:

我的窗口标签也以以下定义开头:

< Window x:Class="LibNameSpace.MainWindow"

I want to use the LibNameSpace:Class1class, and I was hoping to write myns:Class1for this. However, that command causes this error:

我想使用这个LibNameSpace:Class1类,我希望为此写作myns:Class1。但是,该命令会导致此错误:

Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'LibNameSpace' that is not included in the assembly.

未定义的 CLR 命名空间。“clr-namespace” URI 指的是未包含在程序集中的命名空间“LibNameSpace”。

How can I fix this?

我怎样才能解决这个问题?

回答by HCL

The name LibNameSpace sounds like its a library in another assembly. If this is the case, you must add the name of the assembly:

名称 LibNameSpace 听起来像是另一个程序集中的库。如果是这种情况,您必须添加程序集的名称:

xmlns:myns="clr-namespace:LibNameSpace;assembly=MyLibAssembly

Update:

更新:

The name of the assembly can be found in project-explorer in the propeties-screen of the project (of the library-assembly). In general also the file-name of the dll without the dll-suffix represents the assembly name.

程序集的名称可以在项目(库程序集的)的属性屏幕中的 project-explorer 中找到。通常,不带dll-suffix的 dll 文件名也代表程序集名称。

回答by HCL

Because for me it's not really clear what you want to do, here another try:

因为对我来说,你想做什么还不是很清楚,这里再试一次:

If MyLibAssemblyis the main namespace of your application and there in you have a Window named MainWindowand a class named Class1that you want to instantiate in your MainWindow-class:

如果MyLibAssembly是应用程序的主命名空间,并且其中有一个名为 Window 的名称MainWindow和一个Class1要在您的MainWindow-class 中实例化的类:

  • Make sure, that in Class1 is no error, the project must compile without errors. Remove first the namespace-declaration from the xaml and compile your project till you have no compilation errors.
  • Make sure that Class1 is public and has a paramterless constructor
  • Make sure that in the code behind your MainWindow is also in the MyLibAssembly-namcespace.
  • Add then the namspace-declaration xmlns:local="clr-namespace:LibNameSpaceinto your xaml. localis generally used to declare the same namespace as your current element, in your case the window, is in.
  • Insert your Class1with the <local:Class1/>-tag in the xaml. If Class1 does not derive from FrameworkElement or a higher level control, you must add it into the resources-section of your window. If this is true, give it a key. <local:Class1 x:Key="KeyToYourClass"/>
  • 确保在 Class1 中没有错误,项目必须编译没有错误。首先从 xaml 中删除命名空间声明并编译您的项目,直到没有编译错误。
  • 确保 Class1 是公共的并且有一个无参数的构造函数
  • 确保 MainWindow 后面的代码也在 MyLibAssembly-namcespace 中。
  • 然后将 namspace-declaration 添加 xmlns:local="clr-namespace:LibNameSpace到您的 xaml 中。local通常用于声明与当前元素(在您的情况下为窗口)相同的命名空间。
  • 在 xaml 中插入Class1带有 <local:Class1/>-tag 的你。如果 Class1 不是从 FrameworkElement 或更高级别的控件派生的,则必须将其添加到窗口的资源部分。如果这是真的,请给它一把钥匙。<local:Class1 x:Key="KeyToYourClass"/>

Maybe vs is out of sync. Click in the solution-explorer on the root-node Clean Solutionand then Rebuild Solution. Maybe that helps.

可能 vs 不同步。单击根节点Clean Solution 上的解决方案资源管理器,然后单击Rebuild Solution。也许这有帮助。

I hope this helped. If not, try to reformat your question (use the code-symbol to make the question more readable and try to rephrase to make more clear what your desire is).

我希望这有帮助。如果没有,请尝试重新格式化您的问题(使用代码符号使问题更具可读性,并尝试重新措辞以更清楚您的愿望)。

回答by gopal

Use Intellisense. In my case one space mattered. instead of

使用智能感知。就我而言,一个空间很重要。代替

xmlns:local="clr-namespace:DataAccess;assembly=DataAccess"

I hand typed

我手写

xmlns:local="clr-namespace:DataAccess; assembly=DataAccess"

Notice the space after ';'. This made the difference. So use visual studio Intellisense and it will render you correct xaml markup.

注意后面的空格';'。这造成了不同。因此,使用 Visual Studio Intellisense 它将为您呈现正确的 xaml 标记。

回答by Tiago Almeida

I found this answer while I was struggling with problems in Windows 8. I was trying to use a User Control and I had several errors. The last ones where:

我在 Windows 8 中遇到问题时找到了这个答案。我试图使用用户控件,但遇到了几个错误。最后一个:

Error 9 Cannot add 'ScrollControl' into the collection property 'Children', type must be 'UIElement'

错误 9 无法将“ScrollControl”添加到集合属性“Children”中,类型必须为“UIElement”

and:

和:

Error 10 Unknown type 'ScrollControl' in XML namespace 'clr-namespace:EventTests.Controls;assembly=EventTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

错误 10 XML 命名空间“clr-namespace:EventTests.Controls;assembly=EventTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的未知类型“ScrollControl”

ScrollControl is my user control.

ScrollControl 是我的用户控件。

I ended up replacing this:

我最终替换了这个:

xmlns:Controls="clr-namespace:EventTests.Controls"

For this:

为了这:

xmlns:Controls="using:EventTests.Controls"

I hope this saves the time I spent with this issue.

我希望这可以节省我花在这个问题上的时间。