Подтвердить что ты не робот

Как переносить проекты Wpf в новый формат VS2017

Я переношу свои проекты в новый формат visual studio 2017, который хорошо работает для всех стандартных библиотек только сейчас. Я сталкиваюсь с проблемами с моими библиотеками пользовательского интерфейса, где я использую Wpf/Xaml.

Я не могу понять, как это сделать для моих пользовательских элементов управления. Старый элемент больше не работает.

У кого-нибудь есть идея, как это сделать или если это возможно.

4b9b3361

Ответ 1

Есть 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), он автоматически загрузит SDK из NuGet и все настроит.

Ответ 2

13 декабря 2018 года - был объявлен.NET Core 3 Preview 1

.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. Unable to run your project. The "RunCommand" property is not defined. ошибка произойдет.
  4. Включает в себя уже настроенные настройки и ресурсы по умолчанию.

Шаблон:

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

Ответ 3

После некоторых поисков и проб и ошибок я получил его работу!

Это окончательный 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>

Ответ 4

Вышеупомянутое решение работает для 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>