Share via

Issue to add task code to the class

Peter Liang 3,286 Reputation points
2026-04-04T07:13:54.2666667+00:00

Hi,

I want to add the task code like

User's image

into the following. What to adjust?

User's image

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author
  1. Marcin Policht 85,250 Reputation points MVP Volunteer Moderator
    2026-04-04T11:07:38.8833333+00:00

    You already have a valid class, so to “add task code” you don’t need to change the structure—just add an async method inside the class and make sure you import System.Threading.Tasks.

    Try the following

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Maui.ApplicationModel.DataTransfer;
    using Microsoft.Maui.Storage;
    
    namespace MauiApp10_1
    {
        internal class Code1
        {
            public async Task DoSomethingAsync()
            {
                await Task.Delay(1000);
                Console.WriteLine("Task completed");
            }
        }
    }
    

    If your task involves MAUI features like sharing or storage, place that logic inside the async method:

    public async Task ShareTextAsync()
    {
        await Share.RequestAsync(new ShareTextRequest
        {
            Text = "Hello from MAUI",
            Title = "Share"
        });
    }
    

    If this method needs to be called from UI (like a button), make sure the caller is also async:

    await new Code1().DoSomethingAsync();
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


0 additional answers

Sort by: Most 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.