Skip to content

CompiledBindings.WPF package reference breaks CsWin32 #39

@chucker

Description

@chucker

(I'm unsure whether to file this here, or over at https://github.com/microsoft/CsWin32/)

Something about the build process is incompatible with the CsWin32 project (and perhaps other source generators as well?).

To reproduce,

  1. create an empty .NET 9 WPF app.
  2. modify WpfApp1.csproj as follows:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

I.e., add that one package reference.

  1. modify MainWindow.xaml.cs as follows:
using System.Windows;
using System.Windows.Interop;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void SetAsForegroundWindow()
        {
            // handle focus https://stackoverflow.com/a/75705911
            if (PresentationSource.FromVisual(this) is HwndSource hwndSource)
                _ = Windows.Win32.PInvoke.SetForegroundWindow((Windows.Win32.Foundation.HWND)hwndSource.Handle);
        }
    }
}

I.e., add that one method. It's just a dummy method that calls code generated by the Microsoft.Windows.CsWin32 dependency.

  1. add a file NativeMethods.txt:
SetForegroundWindow
  1. do a clean build. This will work.
  2. now, modify WpfApp1.csproj again:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CompiledBindings.WPF" Version="1.0.18"/>
    <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

I.e., add this project as a package reference.

  1. do another clean build. Suddenly, we get build errors:
1>C:\Users\chucker\source\repos\temp\WpfApp1\NativeMethods.txt(1,1,1,20): warning PInvoke001: Method, type or constant "SetForegroundWindow" not found
1>C:\Users\chucker\source\repos\temp\WpfApp1\MainWindow.xaml.cs(20,21,20,28): error CS0103: The name 'Windows' does not exist in the current context
1>C:\Users\chucker\source\repos\temp\WpfApp1\MainWindow.xaml.cs(20,64,20,71): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "WpfApp1.csproj" -- FAILED.

Is there something special I can do to make this combination work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions