How to add my UserControl from another project (Dll) into my WPF Solution?

你离开我真会死。 提交于 2020-08-05 08:58:48

问题


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.

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".

I'm sure that I'm doing something wrong, but I really don't know what...


回答1:


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

xmlns:other="clr-namespace:MyDllProjectNamespace;assembly=MyDllProject"

This will point the namespace which exists in Other assembly.

See this Article to understand the namespaceing in Xaml

Condition Make sure the using control is accessible from the other assemblies.




回答2:


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.




回答3:


Add the dll reference to your project. Post that add the namespace of this usercontrol to your xaml

say xmlns:local="... dll path;assembly=...."

Now the user control will be accessible in your xaml



来源:https://stackoverflow.com/questions/17422940/how-to-add-my-usercontrol-from-another-project-dll-into-my-wpf-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!