Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to update UI after async Task in viewmodel

$
0
0

I have a xaml page with label. I want to update label after some async task. In my ViewModel constructor I set default text for my label. And create a async Task function named SomeTask().

Question 1: Where can I call SomeTask() function. Not able to call async Task function in constructor.
Question 2: How can I update Label text after async Task SomeTask() function.

    public class MyPageViewModel : ViewModelBase
    {  
        private String _selectedText;
        public String SelectedText
        {
            get { return _selectedText; }
            set {
                if (_selectedText != value)
                {
                    _selectedText = value;          
                }       
            }
        }

        public MyPageViewModel ()
        {
            _selectedText = "Welcome";   //Default text
        }

        private async Task<string> SomeTask()
        {            
            return await Task.Run(async () =>
            {
                await Task.Delay(3000); //Dummy task. It will return the status of Task.
                return "Thanks";         //Update Text       
            });         
        }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>