Building a WPF Application with XAML through msbuild - wpf

I am attempting to build a simple WPF Application using msbuild via a custom script (below). The project builds and executes fine through Visual Studio, however if I use the msbuild script it builds successfully, and generates the executable, but crashes immediately on startup (with a "WPFApplication has stopped working" error message).
Has anyone experienced this before, or have any suggestions for things to try?
Thanks!
Build Script
<PropertyGroup>
<AssemblyName>WPFApplication</AssemblyName>
<OutputType>winexe</OutputType>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="WpfBuild\WpfBuild\App.xaml" />
<Compile Include="WpfBuild\WpfBuild\App.xaml.cs" />
<Page Include="WpfBuild\WpfBuild\Window1.xaml" />
<Compile Include="WpfBuild\WpfBuild\Window1.xaml.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
</Project>

Edit the csproj file and put a line similar to this near the end, near to the line with "Microsoft.CSharp.targets" in it:
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
See http://msdn.microsoft.com/en-us/library/aa970678%28VS.85%29.aspx
I'm currently looking for answers to something similar. I found another SO link wpf-app-builds-in-vs2k8-but-not-on-command-line-w-msbuild

Related

New Project Debug Dialog in Visual Studio 2022--Why and How It Changed?

I have a .NET 4.8 C# WinForms project loaded in Visual Studio Enterprise 2022. The Debug section of the project properties page (right-click the project in Solution Explorer and choose Properties...) looks something like this:
Notice that there is a clear Start action section.
I have another .NET 4.8 C# WinForms project loaded in Visual Studio Enterprise 2022. The Debug section of this project properties dialog looks something like this:
Notice that the Debug General section now states:
The management of launch profiles has moved to a dedicated dialog. It
may be accessed via the link below, via the Debug menu in the menu
bar, or via the Debug Target command on the Standard tool bar.
Other posters here and elsewhere on the web have concluded that (a) the new (latter) dialog is due to Visual Studio 2022 (it is not, as both sampled above appeared in Visual Studio 2022 Enterprise; or (b) that this occurs for only projects that are .NET Core projects (that's not true either--both projects above are WinForms 4.8 projects). So if it's not Visual Studio 2022, and it's not .NET Core--then what is it? What causes the former Debug property page to be replaced with the new "dedicated dialog" version?
Moreover, how do you set a StartAction when StartAction no longer appears on the Debug dialog?
[P.S. Please don't reprimand me for posting screen images--they serve a very clear purpose here and I included the searchable text that appears on those dialog boxes in my question so that this post can be found by others who encounter a similar issue...]
How/why does it show different project property pages for two .NET 4.8 projects?
It's about the project format:
The project files with MSBuild project format will use the old Debug settings window.
The project files with SDK project format will use the new Debug settings window.
How to set StartAction for SDK style projects?
In project properties, go to Debug section, General, and click on "Open debug launch profiles UI".
You can specify command line arguments for the current debug profile.
Or you can create a new profile (Executable) and specify the executable if you want (like specifying VS, when you want to debug design time of VS).
After you created the new profile, you can just choose it from debug toolbar dropdown button and start that profile.
Rest of the answer is just for people who want to see the difference between project format, and also see those new project debug settings.
MSBuild Project format
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DE16D3B5-E02E-44A1-B223-2C25ED3F14D9}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>WindowsFormsApp1</RootNamespace>
<AssemblyName>WindowsFormsApp1</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
SDK Project format
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net48</TargetFramework>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Using Include="System.Windows.Forms" />
</ItemGroup>
</Project>

How to exclude App.config from getting bundled in the .exe in a .net core 3 single-file self-contained WinForms app?

I have a Winforms .NET Core 3 app that I want to publish as a Self-Contained Single-File Deployment
Here is the relevant .csproj file
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
<ItemGroup>
<!--PackageReferences-->
</ItemGroup>
<ItemGroup>
<!--ProjectReferences-->
</ItemGroup>
</Project>
I am using <RuntimeIdentifier>win-x64</RuntimeIdentifier> so it generates a Self Contained Deployment for Windows x64 and <PublishSingleFile>true</PublishSingleFile> so everything gets embedded in the .exe file.
When publishing by running:
dotnet publish -c Release
I get the .exe and the .pdb files at bin\Release\netcoreapp3.0\win-x64\publish
- MyApp.exe
- MyApp.pdb
What do I need to change in the .csproj file so I get the MyApp.dll.config or MyApp.exe.config whichever is correct next to the .exe so the app actually reads config from it instead of its embedded App.Config?
I have tried adding this
<ItemGroup>
<Content Update="*.config">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
as hinted by this link Single-file Publish - Build System Interface but it still produces only the two files.
Your question helped me figure out this for me, so thanks.
hopefully this works for you too.
my .csproj looks like
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<Content Include="*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
just done some further testing with a .config file
<ItemGroup>
<None Update="*.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>
that worked for me, with the other config from above.

.net core / standard csproj how to trigger build when external file dependency changes

Trying to trigger a build of a .net standard/core csproj when an external file changes. The file is in the the $(USERPROFILE)\Documents directory.
Have tried Targets with BeforeTargets equal to PrepareForBuild, PreBuildEvent but none work.
<Project Sdk="Microsoft.NET.Sdk" Project="Sdk.props">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net471</TargetFrameworks>
</PropertyGroup>
<Target Name="PrepareForBuild2" BeforeTargets="PrepareForBuild">
<Exec Command="echo PrepareForBuild2" />
</Target>
</Project>
Found solution - use UpToDateCheckInput
<ItemGroup>
<UpToDateCheckInput Include="$(USERPROFILE)\Documents\MyFile.txt" />
</ItemGroup>

.NET standard 2.0 project not able to load Dapper 1.50.5

I am just starting out with .NET standard. In a proof-of-concept project I'm trying to use Dapper as my ORM. In the .NET Standard 2.0 class library project, I added Dapper 1.50.5 Nuget package. However, the assembly isn't getting loaded at runtime.
I get this error:
System.IO.FileNotFoundException HResult=0x80070002
Message=Could not load file or assembly 'Dapper, Version=1.50.5.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
The system cannot find the file specified.
Complete contents of my .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.5" />
<PackageReference Include="Npgsql" Version="4.0.4" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
</ItemGroup>
</Project>
So, you see I've installed the dependencies and dependencies of the dependencies.
What else should I do?
The .NETStandard assembly was added as a reference to my WPF project. I needed to make changes in the .csproj of the WPF project.
The solution mentioned in https://github.com/dotnet/sdk/issues/901 fixes it.
Steps:
Edit your core .csproj file in notepad.
Add the below two lines in each that you find in it.
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Clean and rebuild your solution.

Compile Xaml into Baml?

How can I convert a xaml to baml?
thanks
You can compile the XAML by creating an MSBuild project file that references it. This is what happens in Visual Studio "under the covers" when you do a compile on your project (it creates a temporary .proj file and builds it).
A fairly minimal project file (xamlcompile.csproj) is something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<OutputType>library</OutputType>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<ProjectGuid>{6B8967FF-37B7-43E8-B866-FFD6F13FFC0A}</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="PresentationFramework.Classic" />
</ItemGroup>
<ItemGroup>
<Page Include="Themes\Generic.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
And you can then generate the BAML file by running the command:
MSBuild /t:ResolveReferences;MarkupCompilePass1;MarkupCompilePass2 xamlcompile.csproj
This will create a baml file under obj\Debug, in the example above it will be obj\Debug\Themes\Generic.baml.
Hope that helps.
There is a Reflector plug-in that loads assemblies containing BAML resources (e.g. localized resource assemblies) and shows the corresponding XAML: BamlViewer
When you compile a WPF application in Visual Studio, all your XAML files are converted into BAML, and that BAML is then embedded as a resource into the final DLL or EXE assembly.

Resources