Solved

MvvmCross Xamarin Error Handling

Posted on 2016-09-20
2
36 Views
Last Modified: 2016-11-05
I need help concerning error handling in MvvmCross Xamarin framework. How can I catch errors from async methods in the ViewModels.
0
Comment
Question by:Bassel Chaitani
2 Comments
 
LVL 9

Accepted Solution

by:
Paweł earned 500 total points
ID: 41806862
your async function can't be void, it has to return a type so that you can await it

void Void_BTN_Click(object sender, RoutedEventArgs e)
{
    var n = Convert.ToInt32(this.Numerator_TXT.Text);
    var d = Convert.ToInt32(this.Denominator_TXT.Text);

    try
    {
        //Cant await
        CalculateAsync1(n, d);
    }
    catch (DivideByZeroException ex)
    {
        this.Result_TXT.Text = "Void undefined";
    }
}

async void CalculateAsync1(int n, int d)
{
    this.Result_TXT.Text = (n / d).ToString();
}

Open in new window


because our asynchronous void method cannot be awaited its exceptions cannot be caught however it doesn't just silently fail it'll crash your application, hence it's best that your asynchronous functions return tasks.

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Can await
                output_TXT.Text = await CalculateAsync(3,0);
            }
            catch (DivideByZeroException ex)
            {
                output_TXT.Text = ex.Message;
            }
        }

        private Task<string> CalculateAsync(int v1, int v2)
        {
            return Task.Factory.StartNew(()=>(v1 / v2).ToString());
        }

Open in new window


basically the same thing but since we return a task we can catch the exception.

not sure if it'll function the same way in xamarin, but i'd think it should
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question