没有在 DLL 项目中创建 WPF 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3573339/
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
No creation of a WPF window in a DLL project?
提问by Antoine Jeanrichard
Is there a reason why Visual Studio won't let me create a WPF window in a DLL project?
Visual Studio 不允许我在 DLL 项目中创建 WPF 窗口是否有原因?
I "solved" it by creating a window in an Application Project and copying it to my DLL project. I also found that I could just create a UserControl and change the base class to "Window".
我通过在应用程序项目中创建一个窗口并将其复制到我的 DLL 项目来“解决”它。我还发现我可以创建一个 UserControl 并将基类更改为“Window”。
But if I had to do it this way, it's maybe because I shouldn't do it...
但如果我必须这样做,那可能是因为我不应该这样做......
回答by Florian Doyon
Make sure the project type is WPF User Control Library
when you create your project.
确保项目类型是WPF User Control Library
您创建项目时的类型。
If it isn't then no sweat, just edit the csproj file and make sure the <ProjectTypeGuids>
element under Project/PropertyGroup
contain the following GUIDs
如果不是,那就不用担心,只需编辑 csproj 文件并确保下面的<ProjectTypeGuids>
元素Project/PropertyGroup
包含以下 GUID
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.
此外,请确保您在项目中引用 PresentationFramework 和 System.Xaml,否则您的代码中将不会获得任何 WPF。
回答by electron
You can try adding new WPF User Control Item and change that to Window.
您可以尝试添加新的 WPF 用户控件项并将其更改为 Window。
Add New Item->WPF->User Control
添加新项目->WPF->用户控件
In XAML:
在 XAML 中:
Change <UserControl>
tag as <Window>
将<UserControl>
标签更改为<Window>
In CS:
在 CS:
Change base class from System.Windows.Controls.UserControl
to System.Windows.Window
.
将基类从 更改System.Windows.Controls.UserControl
为System.Windows.Window
.
回答by Viliam
I do it this way: 1) create "WPF Application" 2) remove App.xaml 3) change Project properties -> Application Output type: to Class Library (originally there is Windows Application)
我这样做:1)创建“WPF应用程序”2)删除App.xaml 3)更改项目属性->应用程序输出类型:到类库(原来有Windows应用程序)
Otherwise you will get errors:
否则你会得到错误:
"Library project file cannot specify ApplicationDefinition element"
“库项目文件无法指定 ApplicationDefinition 元素”
"The project file contains a property value that is not valid"
“项目文件包含无效的属性值”
回答by Andy
What do you mean that Visual Studio won't let you create a WPF window in a DLL project? Do you mean that if you right click the project, there is no option to add a Window there?
你的意思是 Visual Studio 不允许你在 DLL 项目中创建 WPF 窗口?你的意思是如果你右键单击项目,就没有在那里添加窗口的选项?
If that is the case, I think that means that you created a project type that isn't a WPF project type. I encountered something similar a while back when I wanted to upgrade a WinForms project to use WPF instead - see this questionfor more information.
如果是这种情况,我认为这意味着您创建的项目类型不是 WPF 项目类型。不久前,当我想升级 WinForms 项目以使用 WPF 时遇到了类似的情况 - 请参阅此问题以获取更多信息。