Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MvvmCross Xamarin Error Handling

Posted on 2016-09-20
2
Medium Priority
?
195 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 10

Accepted Solution

by:
Paweł earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

971 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