Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF App Using new csproj format

Tags:

I am experimenting with migrating a WPF project, defined using the old csproj format, to the new format under VS 2017.

I was able to get most of the way to a successful build using information I found at How-to migrate Wpf projects to the new VS2017 format.

But I'm stuck at getting past this error:

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

My new-style csproj file is as follows:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>     <OutputType>winexe</OutputType>     <TargetFramework>net47</TargetFramework>     <ApplicationIcon />     <OutputTypeEx>winexe</OutputTypeEx>     <StartupObject />   </PropertyGroup>    <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" />     <Compile Update="Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />     <None Update="Settings.settings" LastGenOutput="Settings.Designer.cs" Generator="SettingsSingleFileGenerator" />      <Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />     <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />      <Resource Include="assets\*.*" />   </ItemGroup>    <ItemGroup>     <PackageReference Include="Autofac" Version="4.6.0" />     <PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="4.0.0" />     <PackageReference Include="Extended.Wpf.Toolkit" Version="3.0.0" />     <PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.0.8" />     <PackageReference Include="MaterialDesignColors" Version="1.1.3" />     <PackageReference Include="MaterialDesignThemes" Version="2.3.0.823" />     <PackageReference Include="MvvmLightLibs" Version="5.3.0" />     <PackageReference Include="Serilog" Version="2.4.0" />     <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />   </ItemGroup>    <ItemGroup>     <ProjectReference Include="..\..\WPFUtilities\J4JUI\J4JUI.csproj" />   </ItemGroup>    <ItemGroup>     <Reference Include="System.ComponentModel.DataAnnotations" />   </ItemGroup>    <Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" /> </Project> 

How do I configure the csproj file so that entry point gets built?

Update

Based on the tip about ApplicationDefinition I was able to get the project to compile. I could not set ApplicationDefinition in the BuildAction -- it was not one of the choices -- but had to edit the csproj file manually to include it. Here's the working version:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>     <OutputType>winexe</OutputType>     <TargetFramework>net47</TargetFramework>     <ApplicationIcon />     <OutputTypeEx>winexe</OutputTypeEx>     <StartupObject />   </PropertyGroup>    <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" />     <Compile Update="Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />     <None Update="Settings.settings" LastGenOutput="Settings.Designer.cs" Generator="SettingsSingleFileGenerator" />      <Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" Exclude="App.xaml" />     <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />      <Resource Include="assets\*.*" />      <ApplicationDefinition Include="App.xaml">       <Generator>MsBuild:Compile</Generator>       <SubType>Designer</SubType>     </ApplicationDefinition>    </ItemGroup>    <ItemGroup>     <PackageReference Include="Autofac" Version="4.6.0" />     <PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="4.0.0" />     <PackageReference Include="Extended.Wpf.Toolkit" Version="3.0.0" />     <PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.0.8" />     <PackageReference Include="MaterialDesignColors" Version="1.1.3" />     <PackageReference Include="MaterialDesignThemes" Version="2.3.0.823" />     <PackageReference Include="MvvmLightLibs" Version="5.3.0" />     <PackageReference Include="Serilog" Version="2.4.0" />     <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />   </ItemGroup>    <ItemGroup>     <ProjectReference Include="..\..\WPFUtilities\J4JUI\J4JUI.csproj" />   </ItemGroup>    <ItemGroup>     <Reference Include="System.ComponentModel.DataAnnotations" />   </ItemGroup>    <Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" /> </Project> 

Note also the Exclude filter on the directive. It's necessary to keep MSBuild from attempting to compile App.xaml.cs twice.

like image 288
Mark Olbert Avatar asked May 23 '17 16:05

Mark Olbert


People also ask

Does .NET Core 3.1 support WPF?

. NET Core 3.1 Support for ASP.NET Core, WinForms, and WPF Controls (v19. 2.5)

Can WPF use .NET core?

To develop a . NET Core WPF application, you need Visual Studio 2019 and . NET Core 3.0 installed in your machine.

Is WPF supported in .NET 6?

NET 5/6+ gives us an updated version of Windows Presentation Foundation (WPF). Unlike Web Forms or the Windows Communication Foundation (WCF), Microsoft brought this .

What is the format of Csproj?

A CSPROJ file is a C# (C Sharp) programming project file created by Microsoft Visual Studio. It contains XML-formatted text that lists a project's included files and compilation options. Developers compile CSPROJ files using MSBuild (the Microsoft Build Engine).


1 Answers

You need to set the Build Action of App.xaml to ApplicationDefinition. The result is the following item in your csproj file:

<ApplicationDefinition Include="App.xaml">   <Generator>MSBuild:Compile</Generator>   <SubType>Designer</SubType> </ApplicationDefinition> 
like image 83
Fruchtzwerg Avatar answered Sep 28 '22 04:09

Fruchtzwerg