-
-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
(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,
- create an empty .NET 9 WPF app.
- modify
WpfApp1.csprojas 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.
- modify
MainWindow.xaml.csas 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.
- add a file
NativeMethods.txt:
SetForegroundWindow
- do a clean build. This will work.
- now, modify
WpfApp1.csprojagain:
<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.
- 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
Labels
No labels