Issues to call the event

Scott Huang 3,511 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 Core | ASP.NET API

Answer accepted by question author
Jack Dang (WICLOUD CORPORATION) 18,980 Reputation points Microsoft External Staff Moderator
2026-04-10T03:02:01.2066667+00:00

Hi @Scott Huang ,

From the screenshot, the issue is that DownloadAndShareFileAsync(...) is being called inside CreateMauiApp().

CreateMauiApp() is intended only for configuring and building the MAUI app. It must return MauiApp synchronously, so it is not a suitable place to run an async download/share operation.

If DownloadAndShareFileAsync returns Task, then calling it there will either:

  • produce a compile/build problem if the async flow is handled incorrectly, or
  • run as fire-and-forget at startup, which is not recommended for this scenario.

I would suggest moving this call out of MauiProgram.cs and invoking it from UI logic instead, such as a button click handler, command, or page lifecycle method.

For example:

private async void OnDownloadClicked(object sender, EventArgs e)
{
  await MauiProgram.DownloadAndShareFileAsync("https://...", "Myfile.txt");
}

And keep CreateMauiApp() focused on app startup only:

public static MauiApp CreateMauiApp()
{
  var builder = MauiApp.CreateBuilder();

  // register services, fonts, toolkit, etc.

  return builder.Build();
}

Please also double-check the method call syntax in the screenshot. It looks like there may be a missing comma between the URL and file name arguments.

Note: the code snippets above are for reference only, to show the recommended pattern. They may need to be adjusted to match your page, event handler name, and current project structure rather than copied directly as-is.

Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 85,786 Reputation points
    2026-04-09T15:31:26.45+00:00

    You could use fire and forget syntax

    
    Task.Run(async () => 
    {
         await DownloadAndShareFileAsync(…);
    }
        
    

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

  2. AI answer

    2026-04-09T10:08:44.9266667+00:00

    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:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    2 people found this answer helpful.

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.