dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

Cannot use source generator code in xaml file #8128

Open ye4241 opened 1 year ago

ye4241 commented 1 year ago

Description

Cannot use generated code from analyzers in xaml file, it reported missing assembly or reference.

Reproduction Steps

Steps

  1. Create a WPF project with .net6.0-windows
  2. Add <IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation> in csproj
  3. Reference Aigamo.ResXGenerator to generate designer file of resx manually.
  4. Add Resources.resx file, remove Custom Tool of it, add HelloWorld key into resx.
  5. Add textblock to use this string in xaml, <TextBlock Text="{x:Static res:Resources.HelloWorld}" />
  6. Build the project

Error

Target CoreCompile:
1>    xxx\WpfApp2\WpfApp2\obj\Debug\net6.0-windows\MainWindow.g.cs(34,15,34,25): error CS0234: The type or namespace name 'Properties' does not exist in the namespace 'WpfApp2' (are you missing an assembly reference?)

Sample SamleApplication.zip

Expected behavior

IncludePackageReferencesDuringMarkupCompilation should works for xaml file. Please include sources from analyzers in CoreCompile target.

Actual behavior

IncludePackageReferencesDuringMarkupCompilation not works in xaml file.

Regression?

No response

Known Workarounds

https://github.com/dotnet/wpf/issues/3404

Impact

No response

Configuration

No response

Other information

No response

Kuldeep-MS commented 1 year ago

@ye4241 - I attempted to recreate the problem using the sample application you provided, but I encountered different errors as shown in the image here:
image

Would you be able to verify if the sample application is functioning as intended while I continue my investigation?

ye4241 commented 1 year ago

@Kuldeep-MS You may need to remove the default Custom Tool for .resx files in .csproj file, as it might have been accidentally added when reopening the project.

Or just delete below codes to make sure there is no generator of resx file.

image

Errors: