如何将Wpf项目迁移到新的VS2017格式

时间:2017-04-29 08:44:42

标签: c# wpf visual-studio-2017

我正在将我的项目迁移到新的visual studio 2017格式,这种格式现在只适用于所有标准库,但是我使用Wpf / Xaml的UI库遇到了问题。

我无法弄清楚如何为我的用户控件执行此操作。旧项目似乎不再有效。

任何人都知道如何做到这一点,或者甚至可能。

7 个答案:

答案 0 :(得分:35)

2018年12月13日 - .NET Core 3 Preview 1 was announced

.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>

上一个回答

您可以使用下面的模板替换旧的.csproj。它解决了其他人模板的几个问题。

  1. 您不必像某些建议那样包含中间*.g.cs个文件。
  2. 不会发生Main not found错误。
  3. 不会发生Unable to run your project. The "RunCommand" property is not defined.错误。
  4. 包括已配置的默认设置和资源。
  5. 模板:

    <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>
    

答案 1 :(得分:11)

经过一番搜索和反复试验,我得到了它的工作!

这是最终的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>

答案 2 :(得分:3)

上述解决方案适用于Wpf dll,但我还原它,因为Resharper和Visual Studio设计器在此更改后不再起作用。主要是因为他们在设计时无法对xaml和代码隐藏进行配对。但该项目编制和工作。

对于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>

答案 3 :(得分:3)

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>

当您使用msbuild构建此项目时(特别是我对dotnet build没有好运),它会自动从NuGet下载SDK并设置所有内容。

答案 4 :(得分:1)

.NET Core 3现在已经发布,如果可以的话,应该使用它。

但是,如果没有,我发现我可以仅为XAML项目创建一个单独的Shared项目,并且可以从SDK风格的项目中引用该项目。一切正常构建。

答案 5 :(得分:0)

上述解决方案可能不适用于VS2019上的Xamarin.Platforms.WPF

这是一个专为.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>

答案 6 :(得分:0)

使用官方更新工具来转换项目:

dotnet tool install --global Project2015To2017.Migrate2019.Tool
dotnet migrate-2019 wizard MyTestProject.csproj

适用于项目和解决方案文件。更多信息here