在类库项目中使用wpf窗口 [英] using a wpf window in a class library project

查看:591
本文介绍了在类库项目中使用wpf窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



可以帮助我在类库项目中创建一个wpf窗口。

请分享示例代码。

Hi All,

Can any help me on creating a wpf window in a class library project.
Please share a sample code.

推荐答案

如果你开始在绿色领域建立你的类库我会建议:

1.以新的 WPF应用程序项目

2.像往常一样添加UI组件

3.在 Solution Expoler 中右键单击项目并选择属性

4.在应用程序选项卡 Windows应用程序输出类型 >到类库

5.要清除编译错误,请从项目中删除 App.xaml 文件



完成所有这些后,当你构建这个特定的项目时,你会得到一个DLL,可以在其他项目中使用。



如果您已经拥有了一些类库项目,而不是像Pheonyx中提到的那样,可以添加对WPF所需库的引用他们的评论,但Visual Studio可以单独完成。

1.右键单击类库项目

2.选择添加>新项目

3.在 Visual C#项目的左侧栏中选择 WPF>用户控件(WPF)

4.要使用XAML,请添加对Windows.XAML程序集的引用(项目> 右键单击引用>添加参考> 来自装配添加 Windows.XAML



在项目中,你在哪里想要使用库:

1.将输出DLL添加到References /或只引用你的Library项目

2.考虑到你的库的命名空间是ClassLibrary1你可以使用它在XAML中的组件使用以下代码:

If you start building your class library on a green field I would suggest this:
1. Start with a new WPF Application project
2. Add your UI components as you usually do
3. In Solution Expoler right click on the project and choose Properties
4. On Application tab change Output type from Windows Application to Class Library
5. To get rid of compilation errors delete the App.xaml file from your project

After all this, when you build this particular project you will get a DLL, that can be used in other projects.

If you already have some Class Library project, than you can add references to libraries that are needed for WPF, as Pheonyx mentioned in their comment, but Visual Studio can do it by itself.
1. Right click on your Class Library project
2. Choose Add > New Item
3. In the left bar from Visual C# items select WPF > User Control (WPF)
4. To use XAML add reference to Windows.XAML assembly (Project > right click on References > Add Reference > from Assemblies add Windows.XAML )

In the project, where you want use the library:
1. Add the output DLL to References / or just reference your Library project
2. Considering that the namespace of your library is ClassLibrary1 your can use its components in XAML with this code:
<Window  
...
xmlns:lib="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
...
>
<lib:UserControl1 /> <!-- User Control from the library -->
</Window>


这篇关于在类库项目中使用wpf窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆