Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing a MAUI library gives "Type 'App' already defines a member called 'InitializeComponent' with the same parameter types" #10113

Closed
BlueRaja opened this issue Nov 25, 2024 · 8 comments

Comments

@BlueRaja
Copy link

BlueRaja commented Nov 25, 2024

Description

I'm not sure if this is a WPF bug, a MAUI bug, Visual Studio, Roslyn, etc.

With the latest VS/Maui/WPF/.Net, if you have a WPF app and reference a MAUI Class Library, the WPF application suddenly stops compiling with weird errors, even in a brand new WPF project with no changes from the defaults.

Specifically, the following files are generated by Visual Studio when creating a new WPF project:

App.xaml

<Application x:Class="Deletemetest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Deletemetest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

App.xaml.cs

using System.Configuration;
using System.Data;
using System.Windows;

namespace Deletemetest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }

}

And results in the following errors:

App.xaml.sg.cs: 20: Type 'App' already defines a member called 'InitializeComponent' with the same parameter types
App.xaml.cs:10: 'Application' is an ambiguous reference between 'Microsoft.Maui.Controls.Application' and 'System.Windows.Application'

The second error (which is confusing because the code is already specifying using System.Windows) can be fixed by changing App : Application to App : System.Windows.Application, but the first error seems to be unfixable on our end.

Reproduction Steps

  1. Create a new Maui Class Library
  2. Create a new WPF App which references the Maui Class Library
  3. Attempt to compile

Expected behavior

App compiles successfully

Actual behavior

Compiler gives non-sensical, unfixable compilation errors

Regression?

No, this is new behavior. It was working prior to upgrading to .Net 9

Known Workarounds

none

Impact

MAJOR. App is now completely uncompilable.

Configuration

No response

Other information

No response

@miloush
Copy link
Contributor

miloush commented Nov 25, 2024

Since the executable is WPF and MAUI is only a library, I am not sure why MAUI should be generating an app class. Perhaps this would be more useful in dotnet/maui than here.

@BlueRaja
Copy link
Author

BlueRaja commented Nov 25, 2024

@miloush The issue seems to be deeper than simply Maui defining a class called "App". If I create a new WPF XAML file with any name, the compiler complains that IntializeComponent() is defined twice. If I add any component to that the page's Content and give it a variable name in the XAML, the compiler complains that the variable is defined twice.

In other words, referencing a Maui library completely prevents any WPF XAML component from compiling, with ridiculous error messages.

@miloush
Copy link
Contributor

miloush commented Nov 25, 2024

Sounds like MAUI library is bringing in source generators to the project that run on XAML files.

@BlueRaja
Copy link
Author

I'm not sure what that means. Do you know of any way to stop or workaround that?

@miloush
Copy link
Contributor

miloush commented Nov 25, 2024

You can disable source generators, for example in the project file:

<Project>
    <Target Name="DisableAnalyzers" BeforeTargets="CoreCompile">
        <ItemGroup>
            <Analyzer Remove="@(Analyzer)" />
        </ItemGroup>
    </Target>
</Project>

This disables them all. You can disable specific ones, but you need a path. I am not set up to run MAUI projects, so you have to find out yourself in the solution explorer:

Image

ref: dotnet/roslyn#55518 (comment)

@himgoyalmicro
Copy link
Contributor

@BlueRaja Does the comment from @miloush resolve your issue?

@himgoyalmicro himgoyalmicro added the 📭 waiting-author-feedback To request more information from author. label Nov 27, 2024
@StephaneDelcroix
Copy link

<EnableDefaultMauiItems>false</EnableDefaultMauiItems> should disable handling xaml files as maui xaml files

@BlueRaja
Copy link
Author

BlueRaja commented Dec 2, 2024

The latest comment resolved the issue.

For posterity, you need to open the .csproj for the WPF project in a text editor, and add the above line inside of the <PropertyGroup>

@BlueRaja BlueRaja closed this as completed Dec 2, 2024
@dotnet-policy-service dotnet-policy-service bot removed the 📭 waiting-author-feedback To request more information from author. label Dec 2, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants