Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

visual studio - c# - Call Interclass Asynch Task

I am kinda new to Xamarin.Android and still have a steep learning curve ;-). But i approached a problem which i don't find a solution even after a few days of googling and YouTube-ing.

Do somebody know how to call a async Task from another Class?

i want to call the public async Task ReadStringAsync() from the class MainActivity. How can i do that?

i want to call this:

class TextfileRead
{
    string[] StrData;

    String filename = "Arbeitszeiten.txt";
    String filepath = "myFileDir";
    String fileContent = "";

    public async Task<string> ReadStringAsync()
    {
        var backingFile = Path.Combine(filepath, filename);

        if (backingFile == null || !System.IO.File.Exists(backingFile))
        {
            return "oO, irgendwas ging schief";
        }

        string line;
        using (var reader = new StreamReader(backingFile, true))
        {
            //string line;
            while ((line = await reader.ReadLineAsync()) != null)
            {
                //return line;
            }
        }
        return line;
    }
question from:https://stackoverflow.com/questions/65939662/c-sharp-call-interclass-asynch-task

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In MainActiviy.cs, you could use following ways to call public async Task ReadStringAsync().

public async void getValue()
{
    TextfileRead textfileRead = new TextfileRead();
    string value = await textfileRead.ReadStringAsync();
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...