Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

UI Element does not get updated in an async Task from the ViewModel

$
0
0

Hey guys,

I'm trying to add the MVVM to my project but it doesn't work as expected.

In my View I have a label bound to a text in my ViewModel and a Button bound to a Command like this:

<Label Text="{Binding MeinText}"
             FontSize="Medium"
             VerticalOptions="Center"
             HorizontalOptions="Center"/>
      <Entry Placeholder="Username"/>
      <Entry Placeholder="Password"/>
            <Button Text="Command Test"
              Command="{Binding ConnectCommand}"/>

In my ViewModel I connect to a webservice but the label is never updated.
The Text of my Label is always "Test Text". Does anyone know what I should change or where my fault is that MVVM is not working right?

{
    public class HomePageViewModel : ObservableObject
    {
        public Command ConnectCommand { get; private set; }

        string _meinText;
        public string MeinText
        {
            set
            {
                _meinText = value;
                SetProperty(ref _meinText, "MeinText");
            }
            get { return _meinText; }
        }

        public HomePageViewModel()
        {
            _meinText = "Test Text";
            ConnectCommand = new Command(async() =>
            {
                await Connect();
            });
        }

        async Task<bool> Connect()
        {
            ObservableCollection<RootObject> obcl = new ObservableCollection<RootObject>();
            // TODO: dataservice abfrage
            try
            {
                obcl = await Task.Run(() => DataServices.Instance().GetProjects());
                if(obcl.Count == 0)
                {
                    _meinText = "Connected";
                    return true;

                }
                _meinText = "Not Connected";
                return false;
            }
            catch (Exception)
            {
            }
            return false;
        }
    }
}


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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