如何将 Wpf 项目迁移到新的 VS2017 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43693591/
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
How-to migrate Wpf projects to the new VS2017 format
提问by Niek Jannink
I'm migrating my projects to the new visual studio 2017 format which is working nicely for all standard libraries only now I run into problems with my UI libraries where I use Wpf / Xaml.
我正在将我的项目迁移到新的 Visual Studio 2017 格式,该格式仅适用于所有标准库,现在我在使用 Wpf/Xaml 的 UI 库中遇到了问题。
I cannot figure out howto do this for my user controls. The old item doesn't seem to be valid anymore.
我无法弄清楚如何为我的用户控件执行此操作。旧项目似乎不再有效。
Anybody has an idea howto do this or if it's even possible.
任何人都知道如何做到这一点,或者甚至可能。
回答by stil
December 13th 2018 - .NET Core 3 Preview 1 was announced
2018 年 12 月 13 日 - .NET Core 3 Preview 1 发布
.NET Core 3 will support WPF and WinForms applications. You may try it with Preview version of SDK:
.NET Core 3 将支持 WPF 和 WinForms 应用程序。你可以用预览版的 SDK 试试:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Previous answer
上一个答案
You can use template below to replace old .csproj with. It resolves couple of issues other people templates had.
您可以使用下面的模板来替换旧的 .csproj。它解决了其他人模板的几个问题。
- You don't have to include intermediary
*.g.csfiles like some suggested to do. - No
Main not founderror will occur. - No
Unable to run your project. The "RunCommand" property is not defined.error will occur. - Includes already configured default Settings and Resources.
- 您不必
*.g.cs像建议的那样包含中间文件。 - 否
Main not found会发生错误。 - 否
Unable to run your project. The "RunCommand" property is not defined.会发生错误。 - 包括已配置的默认设置和资源。
Template:
模板:
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<TargetFramework>net47</TargetFramework>
<OutputType>WinExe</OutputType>
<StartupObject />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<!-- App.xaml -->
<ApplicationDefinition Include="App.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</ApplicationDefinition>
<!-- XAML elements -->
<Page Include="**\*.xaml" Exclude="App.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</Page>
<Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
<!-- Resources -->
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" AutoGen="True" DependentUpon="Resources.resx" DesignTime="True" />
<!-- Settings -->
<None Update="Properties\Settings.settings" Generator="SettingsSingleFileGenerator" LastGenOutput="Settings.Designer.cs" />
<Compile Update="Properties\Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
</Project>
回答by Niek Jannink
After some searching and trial and error I got it working!
经过一些搜索和反复试验,我让它工作了!
This is the final wpf csproj:
这是最终的 wpf csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<TargetFrameworks>net451</TargetFrameworks>
<RootNamespace>MyWpfLibrary</RootNamespace>
<AssemblyName>MyWpfLibrary</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Rx-Xaml" Version="2.2.5" />
<PackageReference Include="reactiveui-core" Version="7.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="MyOtherLibrary.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="ReachFramework" />
<Reference Include="System.Net" />
<Reference Include="System.Printing" />
<Reference Include="System.Xaml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx"/>
<Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
<Resource Include="Fonts\*.otf" />
<Resource Include="Images\*.png" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>
回答by Niek Jannink
The above solution works for Wpf dll's, but I reverted it because Resharper and the Visual Studio designer where not functional anymore after this change. Mainly because they couldn't pair the xaml and the code-behind at design time. But the project compiles and works.
上述解决方案适用于 Wpf dll,但我将其还原,因为 Resharper 和 Visual Studio 设计器在此更改后不再起作用。主要是因为他们无法在设计时将 xaml 和代码隐藏配对。但是该项目可以编译并运行。
For a wpf executable you need to do the following:
对于 wpf 可执行文件,您需要执行以下操作:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<TargetFramework>net451</TargetFramework>
<OutputType>WinExe</OutputType>
<RootNamespace>MyNamespace</RootNamespace>
<AssemblyName>MyExe</AssemblyName>
<ApplicationIcon>MyExe.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<StartupObject>MyNamespace.App</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<None Update="Properties\Settings.settings" Generator="SettingsSingleFileGenerator" LastGenOutput="Settings.Designer.cs" />
<Compile Update="Properties\Settings.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Settings.settings" />
<Page Include="MainWindow.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<Compile Update="MainWindow.xaml.cs" DependentUpon="MainWindow.xaml" />
<Resource Include="Images\*.png" />
<ApplicationDefinition Include="App.xaml" SubType="Designer" Generator="XamlIntelliSenseFileGenerator" />
<Compile Update="App.xaml.cs" DependentUpon="App.xaml" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>
回答by ForNeVeR
There's Sunburst.NET.Sdk.WPFthat allows to use it as .NET SDK. Here's complete example for WPF application where any .csand .xamlfiles will be included automatically:
有Sunburst.NET.Sdk.WPF,允许使用它作为.NET SDK。在此处,任何对WPF应用程序完整的例子.cs和.xaml文件将包含自动:
<Project Sdk="Sunburst.NET.Sdk.WPF/1.0.47">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net40</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../WpfMath/WpfMath.csproj" />
</ItemGroup>
</Project>
When you build this project with msbuild(notably I had no luck with dotnet buildthough), it will automatically download SDK from NuGet and set everything up.
当你用它构建这个项目时msbuild(特别是我没有运气dotnet build),它会自动从 NuGet 下载 SDK 并设置一切。
回答by Zev Spitz
Now that .NET Core 3 has been released, you should use it if you can.
现在 .NET Core 3 已经发布,如果可以,您应该使用它。
But if not, I've found that I can create a separate Shared project for only the XAML items, and I can reference that project from the SDK-style project. Everything builds properly.
但如果没有,我发现我可以只为 XAML 项目创建一个单独的共享项目,并且我可以从 SDK 样式的项目中引用该项目。一切正常。
Note that the XAML designer doesn't work -- no Intellisense or red squigglies. Visual Studio opens the XAML files with the XML editor.
请注意,XAML 设计器不起作用——没有 Intellisense 或红色波浪线。Visual Studio 使用 XML 编辑器打开 XAML 文件。
回答by Alexander Vasilyev
The above solutions may not work with Xamarin.Platforms.WPF on VS2019
上述解决方案可能不适用于 VS2019 上的 Xamarin.Platforms.WPF
Here's a project(based on previous answers) designed for the .net framework (not the .net core app), but can handle .net standard dependencies:
这是一个专为 .net 框架(不是 .net 核心应用程序)设计的项目(基于以前的答案),但可以处理 .net 标准依赖项:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<RootNamespace>TestWPF</RootNamespace>
<AssemblyName>TestWPF</AssemblyName>
<TargetFramework>net461</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.0.0" />
<PackageReference Include="Xamarin.Forms" Version="4.2.0.848062" />
<PackageReference Include="Xamarin.Forms.Platform.WPF" Version="4.2.0.848062" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsBase" />
<Reference Include="System.Xaml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<None Update="Properties\Settings.settings" Generator="SettingsSingleFileGenerator" LastGenOutput="Settings.Designer.cs"/>
<Compile Update="Properties\Settings.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Settings.settings" />
<ApplicationDefinition Include="App.xaml" Generator="MSBuild:Compile" />
<Page Include="**\*.xaml" Exclude="App.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
<EmbeddedResource Remove="**\*.xaml" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>

