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...
但如果我必须这样做,可能是因为我不应该这样做…
4 个解决方案
#1
67
Make sure the project type is WPF User Control Library
when you create your project.
在创建项目时,请确保项目类型是WPF用户控制库。
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文件,并确保Project/PropertyGroup下的
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
< ProjectTypeGuids > { 60 dc8134-eba5-43b8-bcc9-bb4bc16c2548 },{ fae04ec0 - 301 f - 11 - d3 - bf4b - 00 - c04f79efbc } < / ProjectTypeGuids >
Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.
另外,确保你参考了演示框架和系统。在项目中使用Xaml,否则在代码中不会获得任何WPF。
#2
6
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"
“库项目文件不能指定应用程序定义元素”
"The project file contains a property value that is not valid"
“项目文件包含无效的属性值”
#3
4
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 question for more information.
如果是这样,我认为这意味着您创建了一个不是WPF项目类型的项目类型。我以前遇到过类似的情况,当时我想升级WinForms项目来使用WPF——更多信息请参见这个问题。
#4
4
You can try adding new WPF User Control Item and change that to Window.
您可以尝试添加新的WPF用户控件项并将其更改为窗口。
Add New Item->WPF->User Control
添加新项- > WPF - >用户控制
In XAML:
在XAML:
Change <UserControl>
tag as <Window>
将
In CS:
在CS:
Change base class from System.Windows.Controls.UserControl
to System.Windows.Window
.
从System.Windows.Controls.中更改基类。System.Windows.Window用户控件。
#1
67
Make sure the project type is WPF User Control Library
when you create your project.
在创建项目时,请确保项目类型是WPF用户控制库。
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文件,并确保Project/PropertyGroup下的
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
< ProjectTypeGuids > { 60 dc8134-eba5-43b8-bcc9-bb4bc16c2548 },{ fae04ec0 - 301 f - 11 - d3 - bf4b - 00 - c04f79efbc } < / ProjectTypeGuids >
Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.
另外,确保你参考了演示框架和系统。在项目中使用Xaml,否则在代码中不会获得任何WPF。
#2
6
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"
“库项目文件不能指定应用程序定义元素”
"The project file contains a property value that is not valid"
“项目文件包含无效的属性值”
#3
4
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 question for more information.
如果是这样,我认为这意味着您创建了一个不是WPF项目类型的项目类型。我以前遇到过类似的情况,当时我想升级WinForms项目来使用WPF——更多信息请参见这个问题。
#4
4
You can try adding new WPF User Control Item and change that to Window.
您可以尝试添加新的WPF用户控件项并将其更改为窗口。
Add New Item->WPF->User Control
添加新项- > WPF - >用户控制
In XAML:
在XAML:
Change <UserControl>
tag as <Window>
将
In CS:
在CS:
Change base class from System.Windows.Controls.UserControl
to System.Windows.Window
.
从System.Windows.Controls.中更改基类。System.Windows.Window用户控件。