Skip to content

Commit 896631c

Browse files
Add warnings to Microsoft.NET.Sdk.WindowsDesktop to improve UseWpf/UseWinforms and multi-targeting experience (#1027)
* Fixes the following: - #685: Remove **/*.xaml from None - #327: Cannot multi-target netcoreap < 3.0 and use WindowsDesktop SDK unconditionally. - #867: Show error when neither UseWpf nor UseWindowsForms is set to true - #746: [Epic] Support WPF and WinForms specific FrameworkReferences a Profiles Also cleans up the way in which we import Microsoft.WinFX.targets - UseLegacyPresentationBuildTasks has been broken for some time now and unusable.
1 parent 2651715 commit 896631c

File tree

3 files changed

+106
-52
lines changed

3 files changed

+106
-52
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tools": {
3-
"dotnet": "3.0.100-preview6-012264",
3+
"dotnet": "3.0.100-preview7-012512",
44
"runtimes": {
55
"dotnet": [
66
"2.1.7",

packaging/Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.props

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,29 @@
22
<PropertyGroup>
33
<_MicrosoftNetSdkWindowsDesktop>true</_MicrosoftNetSdkWindowsDesktop>
44
</PropertyGroup>
5-
6-
<ItemGroup Condition=" '$(EnableDefaultItems)' == 'true' And '$(UseWPF)' == 'true'">
5+
6+
<PropertyGroup>
7+
<!--
8+
WindowsDesktop SDK supports WPF and WinForms on
9+
- .NET Core 3.0 and greater
10+
- .NET Framework 3.0 and greater
11+
12+
Note that on .NET Framework versions < 4.0, additional workarounds may be required to build applications
13+
using the SDK style projects. For e.g., see https://github.com/microsoft/msbuild/issues/1333
14+
15+
Irrespective of whether '$(TargetFrameworkIdentifier)' is '.NETCoreApp' or '.NETFramework',
16+
the minimum value of $(_TargetFrameworkVersionWithoutV) we will be testing for is '3.0'
17+
18+
Note:
19+
Please see https://github.com/microsoft/msbuild/issues/3212 for a discussion about the use of
20+
the private $(_TargetFrameworkVersionWithoutV) property - which will likely remain supported and
21+
is safe to use here.
22+
-->
23+
<_WindowsDesktopSdkTargetFrameworkVersionFloor>3.0</_WindowsDesktopSdkTargetFrameworkVersionFloor>
24+
</PropertyGroup>
25+
26+
<ItemGroup Condition=" ('$(EnableDefaultItems)' == 'true') And ('$(UseWPF)' == 'true') And
27+
('$(_TargetFrameworkVersionWithoutV)' != '') And ('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
728
<ApplicationDefinition Include="App.xaml"
829
Condition="'$(EnableDefaultApplicationDefinition)' != 'false' And Exists('$(MSBuildProjectDirectory)/App.xaml') And '$(MSBuildProjectExtension)' == '.csproj'">
930
<Generator>MSBuild:Compile</Generator>
@@ -13,30 +34,43 @@
1334
<Generator>MSBuild:Compile</Generator>
1435
</ApplicationDefinition>
1536

16-
<None Remove="@(ApplicationDefinition)"
17-
Condition="'$(EnableDefaultApplicationDefinition)' != 'false'" />
18-
1937
<Page Include="**/*.xaml"
2038
Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(ApplicationDefinition)"
2139
Condition="'$(EnableDefaultPageItems)' != 'false'" >
2240
<Generator>MSBuild:Compile</Generator>
2341
</Page>
2442

25-
<None Remove="@(Page)"
26-
Condition="'$(EnableDefaultPageItems)' != 'false'" />
27-
</ItemGroup>
2843

29-
<ItemGroup Condition="'$(DisableImplicitFrameworkReferences)' != 'true' And '$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(UseWPF)' == 'true' And '$(UseWindowsForms)' == 'true'">
30-
<FrameworkReference Include="Microsoft.WindowsDesktop.App" IsImplicitlyDefined="true" />
31-
</ItemGroup>
44+
<!--
45+
See https://github.com/dotnet/wpf/issues/685
46+
Visual Studio would prefer that we remove **/*.xaml instead of
47+
being more precise.
3248
33-
<ItemGroup Condition="'$(DisableImplicitFrameworkReferences)' != 'true' And '$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(UseWPF)' == 'true' And '$(UseWindowsForms)' != 'true'">
34-
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" IsImplicitlyDefined="true" />
49+
<None Remove="@(Page)"
50+
Condition="'$(EnableDefaultPageItems)' != 'false'" />
51+
<None Remove="@(ApplicationDefinition)"
52+
Condition="'$(EnableDefaultApplicationDefinition)' != 'false'" />
53+
-->
54+
<None Remove="**/*.xaml"
55+
Condition="'$(EnableDefaultApplicationDefinition)' != 'false' And '$(EnableDefaultPageItems)' != 'false'" />
3556
</ItemGroup>
3657

37-
<ItemGroup Condition="'$(DisableImplicitFrameworkReferences)' != 'true' And '$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(UseWPF)' != 'true' And '$(UseWindowsForms)' == 'true'">
38-
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" IsImplicitlyDefined="true" />
58+
59+
<ItemGroup Condition="('$(DisableImplicitFrameworkReferences)' != 'true') And
60+
('$(TargetFrameworkIdentifier)' == '.NETCoreApp') And
61+
('$(_TargetFrameworkVersionWithoutV)' != '') And
62+
('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
63+
64+
<FrameworkReference Include="Microsoft.WindowsDesktop.App" IsImplicitlyDefined="true"
65+
Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' == 'true')"/>
66+
67+
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" IsImplicitlyDefined="true"
68+
Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' != 'true')"/>
69+
70+
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" IsImplicitlyDefined="true"
71+
Condition="('$(UseWPF)' != 'true') And ('$(UseWindowsForms)' == 'true')"/>
3972
</ItemGroup>
73+
4074

4175
<!--
4276
Traditionally, Visual Studio has supplied these references for .NET Framework based
@@ -57,16 +91,21 @@
5791
.NET 4.5+: PresentationCore, PresentationFramework, WindowsBase, System.Xaml,
5892
UIAutomationClient, UIAutomationClientSideProviders, UIAutomationProvider, UIAutomationTypes
5993
System.Windows.Controls.Ribbon
60-
61-
Note:
62-
Please see https://github.com/microsoft/msbuild/issues/3212 for a discussion about the use of
63-
the private $(_TargetFrameworkVersionWithoutV) property - which will likely remain supported and
64-
is safe to use here.
94+
6595
-->
66-
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' And '$(_TargetFrameworkVersionWithoutV)' != ''">
67-
<_WpfCommonNetFxReference Include="WindowsBase" Condition="'$(_TargetFrameworkVersionWithoutV)' >= '3.0'" />
68-
<_WpfCommonNetFxReference Include="PresentationCore" Condition="'$(_TargetFrameworkVersionWithoutV)' >= '3.0'" />
69-
<_WpfCommonNetFxReference Include="PresentationFramework" Condition="'$(_TargetFrameworkVersionWithoutV)' >= '3.0'" />
96+
<ItemGroup Condition="('$(TargetFrameworkIdentifier)' == '.NETFramework') And ('$(_TargetFrameworkVersionWithoutV)' != '') And
97+
('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
98+
99+
<!--
100+
The following 3 _WpfCommonNetFxReference items normally require Condition="'$(_TargetFrameworkVersionWithoutV)' >= '3.0'", since
101+
they are supported on .NET Framework 3.0 and above.
102+
103+
This condition is implicitly satisfied by '$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)'
104+
in the outer ItemGroup
105+
-->
106+
<_WpfCommonNetFxReference Include="WindowsBase" />
107+
<_WpfCommonNetFxReference Include="PresentationCore" />
108+
<_WpfCommonNetFxReference Include="PresentationFramework" />
70109

71110
<_WpfCommonNetFxReference Include="System.Xaml" Condition="'$(_TargetFrameworkVersionWithoutV)' >= '4.0'">
72111
<RequiredTargetFramework>4.0</RequiredTargetFramework>
@@ -79,15 +118,16 @@
79118
<_WpfCommonNetFxReference Include="System.Windows.Controls.Ribbon" Condition="'$(_TargetFrameworkVersionWithoutV)' >= '4.5'" />
80119
</ItemGroup>
81120

82-
<ItemGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework' And '$(UseWPF)' == 'true' ">
83-
<_SDKImplicitReference Include="@(_WpfCommonNetFxReference)" />
84-
</ItemGroup>
85-
86-
<ItemGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework' And '$(UseWindowsForms)' == 'true' ">
87-
<_SDKImplicitReference Include="System.Windows.Forms"/>
88-
</ItemGroup>
89-
90-
<ItemGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework' And '$(UseWindowsForms)' == 'true' And '$(UseWPF)' == 'true' ">
91-
<_SDKImplicitReference Include="WindowsFormsIntegration"/>
121+
<ItemGroup Condition="('$(DisableImplicitFrameworkReferences)' != 'true') And ('$(TargetFrameworkIdentifier)' == '.NETFramework') And
122+
('$(_TargetFrameworkVersionWithoutV)' != '') And ('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
123+
124+
<_SDKImplicitReference Include="@(_WpfCommonNetFxReference)"
125+
Condition="'$(UseWPF)' == 'true'"/>
126+
127+
<_SDKImplicitReference Include="System.Windows.Forms"
128+
Condition="('$(UseWindowsForms)' == 'true') " />
129+
130+
<_SDKImplicitReference Include="WindowsFormsIntegration"
131+
Condition=" ('$(UseWindowsForms)' == 'true') And ('$(UseWPF)' == 'true') "/>
92132
</ItemGroup>
93133
</Project>
Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<Project>
2+
<Import Project="Microsoft.WinFX.targets" />
23

3-
<!--
4-
For now, UseLegacyPresentationBuildTasks=true can be used to use the in-box framework
5-
PresentationBuildTasks and targets when running in full framework MSBuild. This is
6-
just an escape hatch while the new PresentationBuildTasks bake.
7-
8-
When we remove this,we can remove the correpsonding corresponding mscorlib swap targets in
9-
Microsoft.DesktopUI.App.targets, and the production of ref-mscorlib shims
10-
-->
11-
<Import Project="$(MSBuildToolsPath)\Microsoft.WinFX.targets"
12-
Condition ="'$(MSBuildRuntimeType)' != 'Core' and '$(UseLegacyPresentationBuildTasks)' == 'true'"/>
13-
14-
<Import Project="Microsoft.WinFX.targets"
15-
Condition="'$(MSBuildRuntimeType)' == 'Core' or '$(UseLegacyPresentationBuildTasks)' != 'true'" />
16-
17-
<ItemGroup Condition=" '$(EnableDefaultItems)' == 'true' And '$(UseWPF)' == 'true'">
4+
<ItemGroup Condition=" ('$(EnableDefaultItems)' == 'true') And ('$(UseWPF)' == 'true') And ('$(_TargetFrameworkVersionWithoutV)' != '') And
5+
('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
186

197
<!-- In the WindowsDesktop .props, we globbed all .xaml files as Page items. If any of those files are included
208
as Resource, Content, or None items, then remove them from the Page items. -->
@@ -25,9 +13,10 @@
2513
<!-- Generate error if there are duplicate page items. The task comes from the .NET SDK, and this target follows
2614
the pattern in the CheckForDuplicateItems task, where the .NET SDK checks for duplicate items for the item
2715
types it knows about. -->
28-
<Target Name="CheckForDuplicatePageItems" BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile"
16+
<Target Name="CheckForDuplicatePageItems"
17+
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile"
2918
DependsOnTargets="CheckForDuplicateItems"
30-
Condition="'$(UseWPF)' == 'true'">
19+
Condition="('$(UseWPF)' == 'true') And ('$(_TargetFrameworkVersionWithoutV)' != '') And ('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
3120

3221
<CheckForDuplicateItems
3322
Items="@(Page)"
@@ -47,4 +36,29 @@
4736

4837
</Target>
4938

39+
<!--
40+
This warning can be suppressed by setting $(MSBuildWarningsAsMessages) property, like this:
41+
<PropertyGroup>
42+
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);NETSDK1106</MSBuildWarningsAsMessages>
43+
</PropertyGroup>
44+
-->
45+
<Target Name="_WindowsDesktopFrameworkRequiresUseWpfOrUseWindowsForms"
46+
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
47+
Condition="('$(_TargetFrameworkVersionWithoutV)' != '') And ('$(_TargetFrameworkVersionWithoutV)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
48+
<NetSdkWarning ResourceName="WindowsDesktopFrameworkRequiresUseWpfOrUseWindowsForms"
49+
Condition="'$(UseWpf)' != 'true' And '$(UseWindowsForms)' != 'true'"/>
50+
</Target>
51+
52+
<!--
53+
This warning can be suppressed by setting $(MSBuildWarningsAsMessages) property, like this:
54+
<PropertyGroup>
55+
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);NETSDK1105</MSBuildWarningsAsMessages>
56+
</PropertyGroup>
57+
-->
58+
<Target Name="_WindowsDesktopFrameworkRequiresVersion30"
59+
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
60+
Condition="('$(TargetFrameworkIdentifier)' == '.NETCoreApp') And
61+
('$(_TargetFrameworkVersionWithoutV)' != '') And ('$(_TargetFrameworkVersionWithoutV)' &lt; '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
62+
<NetSdkWarning ResourceName="WindowsDesktopFrameworkRequiresVersion30" />
63+
</Target>
5064
</Project>

0 commit comments

Comments
 (0)