如何将我的 UserControl 从另一个项目 (Dll) 添加到我的 WPF 解决方案中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17422940/
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
How to add my UserControl from another project (Dll) into my WPF Solution?
提问by Marc
So, everything is in the title, I just want to add a UserControl in my WPF Window. It looks like easy but the UserControl is in another project (Dll project) in the same solution. And I just can't reference it.
所以,一切都在标题中,我只想在我的 WPF 窗口中添加一个 UserControl。看起来很简单,但 UserControl 位于同一解决方案中的另一个项目(Dll 项目)中。我只是无法引用它。
So, my best try is something like that:
所以,我最好的尝试是这样的:
<Window xmlns:local="clr-namespace:MyWindowProject" x:Name="window"
xmlns:other="clr-namespace:MyDllProject"
x:Class="MyWindowProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<other:MyUserControl />
</Window>
I know I am near to find how to make it because I've got just one error and the autocompletion is working on MyUserControl. But, I've got this weird error: "The name 'MyUserControl' does not exist in the namespace "clr-namespace:MyDllProject".
我知道我快要找到如何制作它了,因为我只有一个错误并且自动完成功能正在 MyUserControl 上运行。但是,我遇到了这个奇怪的错误:“命名空间“clr-namespace:MyDllProject”中不存在名称“MyUserControl”。
I'm sure that I'm doing something wrong, but I really don't know what...
我确定我做错了什么,但我真的不知道是什么......
回答by JSJ
xmlns:other="clr-namespace:MyDllProject"
xmlns:other="clr-namespace:MyDllProject"
The above import statement show's that you are pointing the namespace of your local project. if you want to add a refrance of the other project (The Dll Project) then you must add the namespace as like
上面的导入语句表明您指向的是本地项目的命名空间。如果你想添加另一个项目(The Dll Project)的refrance,那么你必须像这样添加命名空间
xmlns:other="clr-namespace:MyDllProjectNamespace;assembly=MyDllProject"
xmlns:other="clr-namespace:MyDllProjectNamespace;assembly=MyDllProject"
This will point the namespace which exists in Other assembly.
这将指向存在于其他程序集中的命名空间。
See this Articleto understand the namespaceing in Xaml
看到这个文章来了解XAML中namespaceing
ConditionMake sure the using control is accessible from the other assemblies.
条件确保可以从其他程序集访问使用控件。
回答by Oscar
Add your user control to the controls tool box, and drag'n drop from there to your form. Visual Studio will take care of namespaces and things like that.
将您的用户控件添加到控件工具箱,然后从那里拖放到您的表单中。Visual Studio 会处理命名空间之类的事情。
回答by Arushi Agrawal
Add the dll reference to your project. Post that add the namespace of this usercontrol to your xaml
将 dll 引用添加到您的项目中。将此用户控件的命名空间添加到您的 xaml 的帖子
say xmlns:local="... dll path;assembly=...."
说 xmlns:local="... dll path;assembly=...."
Now the user control will be accessible in your xaml
现在可以在您的 xaml 中访问用户控件

