0

The .NET7-Windows project with SourceGenerator e.g. MimeTypes is working. When adding a local namespace to the WPF-UserControl the generated file is not included anymore. How can I modify the project file, that all generated files are automatically included?

Reproducable Example:

Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-windows</TargetFramework>
    <LangVersion>10</LangVersion>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
    <NoWarn>CA1416</NoWarn>
  </PropertyGroup>
  <PropertyGroup>
    <OutputType>Library</OutputType>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="MimeTypes" Version="2.4.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

UserControl1.xaml:

<UserControl x:Class="Example.UserControl1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            >
   <Grid/>
</UserControl>

UserControl1.xaml.cs:

using System.Windows.Controls;

namespace Example
{
   public partial class UserControl1 : UserControl
   {
       public UserControl1()
       {
           InitializeComponent();
           var x = MimeTypes.GetMimeType("Testfile.exe");
       }
   }
}

This is working as expected, but when adding:

            xmlns:local="clr-namespace:Example"

The generated MimeTypes file is not included anymore, which leads to the compiler error CS0103 (The name 'MimeTypes' does not exist in the current context)

  • That's a library. You have another project for the entry point? Have you forgotten to reference that library in your exe project? Which is the context you'd run in. – Andy Jan 09 '23 at 16:50
  • Yes it's a library, that's why the entry point is not relevant. – Benny Bürger Jan 10 '23 at 09:31
  • It does build for me using `dotnet build` on SDK `7.0.100` maybe some VS error? (closing/reopening VS could help) – Martin Ullrich Jan 20 '23 at 09:51
  • I'm using the command line with dotnet build. But the used msbuild is based on an up-to-date VS installation. MSBuild version 17.4.0+18d5aef85 for .NET And you did add the local namespace in the xaml file? – Benny Bürger Jan 23 '23 at 14:51

0 Answers0