Share via

Issues to call the event

Peter Liang 3,286 Reputation points
2026-04-09T10:08:11.0166667+00:00

Hi,

DownloadAndShareFileAsync is declared within MauiProgram.cs, and was called within the same .cs file.

It is not a success to build the project. Any reason from the following?

User's image

Here is MauiProgram.cs

User's image

Developer technologies | ASP.NET | ASP.NET API

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-04-09T10:08:44.9266667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    DownloadAndShareFileAsync is declared as a public static method inside MauiProgram, but it is being called directly inside CreateMauiApp without await and without qualifying the call.

    Key issues:

    1. CreateMauiApp must return a MauiApp synchronously. It cannot be marked async and cannot await an async method.
    2. A Task-returning method like DownloadAndShareFileAsync should not be called and ignored in a startup method such as CreateMauiApp; that will either not compile (if await is used) or will compile with warnings and run at an unexpected time.
    3. If the call is inside CreateMauiApp body, it must be fully qualified as MauiProgram.DownloadAndShareFileAsync(...) because it is a static method of the same class, but even then it still returns a Task that cannot be awaited there.

    Recommended changes:

    • Remove the call to DownloadAndShareFileAsync from CreateMauiApp:
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            // configure builder...
    
            return builder.Build();
        }
    
        public static async Task DownloadAndShareFileAsync(string fileUrl, string fileName)
        {
            // implementation
        }
    }
    
    • Call DownloadAndShareFileAsync from UI code (for example, from a button click handler or command) where async/await is allowed:
    private async void OnDownloadButtonClicked(object sender, EventArgs e)
    {
        await MauiProgram.DownloadAndShareFileAsync("https://...", "MyFile.txt");
    }
    

    This keeps CreateMauiApp purely for app configuration and avoids build errors related to calling an async method from a non-async factory method.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.