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
WinUI .Net MAUI "error CS5001: Program does not contain a static 'Main' method suitable for an entry point" #21559
Comments
Hi @Musicnotes , can you share more details about "Add some native WinUI code"? If you can upload a project that reproduces this issue that would be most helpful. This could be a problem in the CSPROJ file or perhaps an issue with the C# code that was added. |
Thanks for the prompt reply @Eilon . Time permitting we can try to get something together I guess. However, just so we're on the same page, is there a Microsoft sample project using native WinUI within .Net MAUI that we can download and reference? Thanks! |
@Eilon I found this .Net MAUI project declaring UseWinUI so I downloaded the repo and tried to build that specific solution with WinUI (packaged or unpackaged) but it won't build on two different workstations (one with VS Enterprise with latest update): |
@Musicnotes - in general in .NET MAUI you always have access to native UI on each platform. You could, for example, in public MainPage()
{
InitializeComponent();
#if WINDOWS
// Add some WinUI control here
#endif
} OK it's a bit more complicated than that, but it's not too far off. The sample at https://github.com/dotnet/maui-samples/tree/8fdb441f1a01d2b91979af9588f6ef3ac2ad9ffd/8.0/PlatformIntegration/NativeEmbeddingDemo is about the opposite: how to take MAUI controls and place them in native apps (WinUI / Android / etc.). So, do you want to:
|
Hi @Eilon we need option #1 "Have a cross-platform .NET MAUI app that has some platform-specific controls". As I mentioned, we have an iOS, Android, and Windows (UWP) app that is written using Xamarin.Forms. We have already stood up iOS and Android in the single project setup and are just getting to the Windows (WinUI) app but having issues. What we're seeing is that the app won't build unless we create a Program.cs with a static Main method (clearly we shouldn't need to) but when we do that the app won't create the APPX folders and launch within the debuggger. |
@mnxamdev - this doc has some guidance on platform-specific code in .NET MAUI apps: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code?view=net-maui-8.0. The specific examples there are focused on Android and iOS, but the exact same pattern is used for all supported platforms. If you can upload a sample that demonstrates the problem you're seeing we can take a look. I think I've seen similar errors before, but based on what you're describing I don't expect that error at all. |
I've updated the description above with a link to the repository with code to reproduce the issue. |
Thank you @Musicnotes ! I saw one thing that could be the problem: All the lines in this section should be removed: https://github.com/Musicnotes/Sample_WinUI_Maui_App/blob/main/MauiTestApp/MauiTestApp.csproj#L73-L101 Probably some action in VS accidentally added them, but I think they should all be removed. Can you give that a try and see if your app compiles and works? |
Done committed/pushed. We now get: I added the line for AppxManifest and we get a whole slew of errors. Lines 73-101 were added by us to try to get the project to build at all. Does Microsoft have a MAUI sample project using WinUI native views and controls? Can someone at Microsoft please download the sample we spent time submitting and help out? Thanks. |
Thank you @Musicnotes , I think I found something important. When I comment out this line: https://github.com/Musicnotes/Sample_WinUI_Maui_App/blob/main/MauiTestApp/MauiTestApp.csproj#L26 To be: <!--<EnableDefaultMauiItems>false</EnableDefaultMauiItems>--> Then the project builds fine. I guess the "default MAUI items" affect the processing of the Windows AppX Manifest file, so if it's set to Does your project need that setting? |
Yes because we have WinUI views XAML that need to be built properly so we are doing this according to Microsoft's guidance: Also, if you remove that setting in the test project maybe it builds but the app crashes when you press the button. In our real-world app if I remove that setting I get a whole slew of errors. |
Ah, that makes sense. I've tried debugging this some more but I can't figure out why the manifest file doesn't become available in that scenario. If I add this: <ItemGroup>
<AppxManifest Include="Platforms\Windows\Package.appxmanifest" />
</ItemGroup> Then it does pass that check, but then I inevitably got lots of other errors. There's a new template that makes it easy to create a .NET MAUI project that is multi-project, one for each platform, but sharing MAUI stuff in one project (similar to what Xamarin.Forms used to do). This should make it easier to achieve what you're doing. The template was added in PR #20365, but I'm not sure the easiest way for you to get the template (without building parts of the MAUI repo). I'll find out and get back to you. |
Apparently if you're .NET MAUI is up-to-date enough, then you should have a new .NET MAUI Multi-Project App available in Visual Studio (or command-line): And from that you can create a solution that includes a WinUI-specific project, which I think can have WinUI-specific XAML in it. All the .NET MAUI XAML goes in a shared project. Like this: I suggest giving that a try because it might give you what you want. A couple of notes:
|
Hi @Musicnotes unfortunately this is now at the limit of how much I know about WinUI usage in .NET MAUI projects. It seems like with enough MSBuild XML tags there should be a way to make it work, but I'm not sure which ones. My only thought on how to debug this further is to try the MAUI multi-project solution and see if you can get things to work (hopefully you don't need all your app's code, but just the smaller sample you posted). And if you can get that to work, then building it with a diagnostic-level MSBuild BinLog and comparing that to the binlog of the failing project might help understand which settings are different and presumably cause the failure. That is, ultimately it seems that in the 'broken' project some files are not getting interpreted in the format you need them. But it isn't clear why. Probably there's something in the MAUI SDK that is re-interpreting a file as something that you don't want. And comparing what's broken there vs. what would hopefully work in the multi-project template could help gain an understanding of that. |
Description
We have a large project that has cross platform code with iOS, Android, and Windows. However, on each platform there is some native code used. We just finished loading up our iOS and Android app in MAUI but the Windows project has been problematic. While it finally builds. we get the "error CS5001: Program does not contain a static 'Main' method suitable for an entry point" and the app doesn't launch. We followed this guide to add some relevant options but upon various rounds of tweaking we are not much further.
https://learn.microsoft.com/en-us/dotnet/maui/migration/uwp-projects?view=net-maui-8.0
Steps to Reproduce
Link to public reproduction project repository
https://github.com/Musicnotes/Sample_WinUI_Maui_App
Version with bug
8.0.10 SR3
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
No response
Did you find any workaround?
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: