Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Wpf项目迁移到新的VS2017格式_C#_Wpf_Visual Studio 2017 - Fatal编程技术网

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

C# 如何将Wpf项目迁移到新的VS2017格式,c#,wpf,visual-studio-2017,C#,Wpf,Visual Studio 2017,我正在将我的项目迁移到新的visual studio 2017格式,该格式在所有标准库中都能很好地工作,只是现在我在使用Wpf/Xaml的UI库中遇到了问题 我不知道如何为我的用户控件执行此操作。旧项目似乎不再有效 任何人都有办法做到这一点,或者如果可能的话。经过一番搜索和反复尝试,我终于让它工作了 这是最终的wpf csproj: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <LanguageTa

我正在将我的项目迁移到新的visual studio 2017格式,该格式在所有标准库中都能很好地工作,只是现在我在使用Wpf/Xaml的UI库中遇到了问题

我不知道如何为我的用户控件执行此操作。旧项目似乎不再有效


任何人都有办法做到这一点,或者如果可能的话。

经过一番搜索和反复尝试,我终于让它工作了

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

$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets
net451
我的图书馆
我的图书馆

上述解决方案适用于Wpf dll,但我恢复了它,因为在这次更改后,Resharper和Visual Studio designer不再起作用。主要是因为他们无法在设计时将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>

$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets
net451
温克斯
MyNamespace
迈克斯
MyExe.ico
应用程序清单
MyNamespace.App

2018年12月13日-

.NETCore3将支持WPF和WinForms应用程序。您可以使用SDK的预览版本进行尝试:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>
有一个允许将其用作.NETSDK的方法。以下是WPF应用程序的完整示例,其中任何
.cs
.xaml
文件都将自动包括在内:


温克斯
网络40

当您使用
msbuild
构建此项目时(值得注意的是,我没有幸使用
dotnet build
),它将自动从NuGet下载SDK并设置所有内容。

上述解决方案可能无法与VS2019上的Xamarin.Platforms.WPF一起使用

以下是一个为.net framework(不是.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>

温克斯
TestWPF
TestWPF
net461
真的
真的
满的
pdbonly

既然.NET Core 3已经发布,您应该尽可能使用它

但是如果没有,我发现我可以只为XAML项创建一个单独的共享项目,并且我可以从SDK样式的项目中引用该项目。一切正常

请注意,XAML设计器不起作用——没有Intellisense或红色波形。Visual Studio使用XML编辑器打开XAML文件。

使用官方更新实用程序转换项目:

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


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

您所说的旧项目似乎不再有效了?生成是否失败,或在运行时引发异常?新项目格式似乎无法识别/生成xaml文件您是否创建了新项目?为什么?您应该可以在VS 2017I中打开原始项目。我想使用新的包引用和*.cs includes简化我的项目文件。您安装了正确的工作负载吗?对于WPF,它的.Net桌面开发。您是否尝试迁移WPF可执行项目?如果是,你是怎么做到的?我在编译时遇到“Main not found”错误。@MarkOlbert您需要设置。事实上,我已经有了一份清单,上面列出了我要做的所有事情。你知道如何为Telerik reports做同样的事情吗?我不知道用什么做发电机。谢谢,尼克。顺便说一句,对LanguageTargets和MSBuildSDKExtrasTargets的引用是什么意思?我认为LanguageTargets的意思是它是C#,而不是VB.Net或F#,我猜MSBuildSDKExtrasTargets是MSBuild的东西。我没有试着移除它们来看看会发生什么:)你的回答对我很有用。在我的情况下,我有一个WPF库的情况。因此,我必须删除,而不是将OutputType设置为Exe。在编辑此项目结构时,当我编辑设置设置时,VS设计器不会重新生成设置。设计器。cs,而应该使用
MSBuild:Compile
,而不是
MSBuild:UpdateDesignTimeXaml
。同样,运行自定义工具也没有效果。出于某种原因,当我构建项目时,生成的文件不会更新,每次更改任何文件时,我都必须手动重建。XAMLsMight还需要为新CP添加此变通方法。就目前而言,这确实是一个更好的解决方案。让我们等待,直到MS释放对Wpf和Winforms的完全支持。@FizxMike我使用的是MSBuild 15。github上的页面显示
Sdk=“Sunburst.NET.Sdk.Wpf”
,但这只会给我带来错误。Thanx提供完整版本限定的提示
Sdk=“Sunburst.NET.Sdk.WPF/1.0.47”
。(github页面未启用任何问题:()@springy76请参阅没有Sunburst SDK的可能解决方案。它不是官方的更新工具,但由hvanbakel创建。看起来合法且有希望,但使用风险自负。
dotnet tool install --global Project2015To2017.Migrate2019.Tool
dotnet migrate-2019 wizard MyTestProject.csproj