PamanBeruang
PamanBeruang

Reputation: 1589

Refresh Xaml UI data bind

I'm working with windows phone apps and my apps getting data from the web service and sometimes when connection is not good there will be error and not showing data so I add one refresh button into my apps and in that apps I'm calling loaddata from mainviewmodel but nothing happen, what's wrong?

MainViewModel mv = new MainViewModel();
private void refreshButton_Click(object sender, EventArgs e)
{
     mv.LoadData();
}

and here is loadData() in my mainviewmodel.cs

public void LoadData()
        {

             Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;

            try
            {
                Geoposition position =
                await geolocator.GetGeopositionAsync(
                TimeSpan.FromMinutes(1),
                TimeSpan.FromSeconds(30));

                center = new GeoCoordinate(
                        position.Coordinate.Latitude,
                        position.Coordinate.Longitude);

                latitude = position.Coordinate.Latitude;
                longitude = position.Coordinate.Longitude;
                UpdateTransport();
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disable in phone settings.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.IsDataLoaded = true;
        }

Upvotes: 0

Views: 524

Answers (1)

har07
har07

Reputation: 89315

I guess DataContext of your page is a MainViewModel object. In that case, you should invoke LoadData of the DataContext instead of "creating a new MainViewModel object then invoke the newly created MainViewModel's LoadData". Because the page showing data from it's DataContext. If I guess the situation right, your refresh button code should be about like this :

private void refreshButton_Click(object sender, EventArgs e)
{
     var vm = (MainViewModel)this.DataContext;
     vm.LoadData();
}

And if you implemented INotifyPropertyChanged properly in MainViewModel you'll see the page updated.

Upvotes: 1

Related Questions