WPF应用程序工程类型转换-exe与dll相互转换

1.  Windows Application 转换成Class Library类型(exe → dll)

新建一个WPF应用程序,在该Project右键Properties,将Output type从Windows Application改成ClassLibrary。

编译不成功,报错如下:

网上搜到一种解决方法是:删掉App.xaml,重新编译就可以成功编译成dll类型了。

但是我还想在需要的时候再从ClassLibrary改成Windows Application。所以先把App.xaml及App.xaml.cs复制了一份作为备份,然后在工程中删除App.xaml(及App.xaml.cs),最终build出了WpfApplication1.dll。

 

2. Class Library转换成Windows Application类型(dll → exe)

在该Project右键Properties,将Output type从ClassLibrary改成Windows Application。

然后把App.xaml(及App.xaml.cs)再添加回Project。编译出错:

网上搜索这个错误的解决方案,查找到/obj/Debug/App.g.i.cs中没有生成Main,删除掉Obj后,重新编译生成Obj仍然解决不了。

最后搜到正确的解决方案: App.xaml右键属性中,Build Action从“Page”改成“ApplicationDefinition”,重新编译即可,成功生成WpfApplication1.exe。

对比一个正常的WPF应用程序的App.xaml的属性,确实都是Build Action:“ApplicationDefinition”。 因为后来我自己把App.xaml加回工程中,只是新加已存在项,所以加回去后Build Action默认变成了Page。

然后,就想到在Windows Application 转换成Class Library类型时,把App.xaml的Build Action改成Page,那么编译dll应该就不会报错了。一试之下,果然可以;还不用删除App.xaml这么麻烦。

转念一想,提供删除App.xaml来实现从WPF工程转换成类库工程的人,压根就没想过要反过来把类库工程转换成WPF工程吧。毕竟exe和dll都可以作为其他工程的引用,都可以正常使用其中的类等程序集资源。我对WPF工程和类库工程的这种相互转化的需求确实不太常见啊!

3.  总结

     exe → dll

  • Application Output type:Windows Application 转换成Class Library类型
  • App.xaml: Build Action从“ApplicationDefinition”改成“Page”

    dll → exe

  • Application Output type:Class Library转换成Windows Application类型
  • App.xaml: Build Action从“Page”改成“ApplicationDefinition”

 转载请注明出处:https://www.cnblogs.com/lichu-lc/p/8127739.html

posted @ 2017-12-27 21:19  double蠢  阅读(1216)  评论(0编辑  收藏  举报