bifedefrango
bifedefrango

Reputation: 85

How to Refresh or Recall Page on Xamarin Forms

So, I have this app where I can choose a car and see the car info... I'm displaying the cars like this.

I'm using the Rg.Plugins.Popup so when I click the car icon, it opens this popup with "my cars"

So now I'm facing a problem which is, when I choose a car, I want to refresh my current page so the car's info can be shown... I'm handling the car button click on this next view model:

public class MyCarViewModel : ViewModelBase
    {

        public MyCarViewModel()
        {
        }
        public MyCarViewModel(INavigation navigation)
        {
            this.Navigation = navigation;
            this.SelectedCar = null;
            GetClientCars();
        }

        private Page page { get; set; }
        private List<CarInfo> _CarList;

        public List<CarInfo> CarList
        {
            get
            {
                return _CarList;
            }

            set
            {
                _CarList = value;
                OnPropertyChanged("CarList");
            }
        }

        private CarInfo _SelectedCar;

        public CarInfo SelectedCar
        {
            get
            {
                return _SelectedCar;
            }

            set
            {
                _SelectedCar = value;
                OnPropertyChanged("SelectedCar");
                if (_SelectedCar != null)
                {
                    CarSelected(_SelectedCar);
                }

            }
        }

        public INavigation Navigation { get; set; }

        private void CarSelected(CarInfo car)
        {

            App.choosedCar = car;
            PopupNavigation.Instance.PopAllAsync();
            this.SelectedCar = null;
        }
}

And I want this View to refresh

<views:BaseMainPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="OficinaDigitalX.Views.CarDetails"
    xmlns:views="clr-namespace:OficinaDigitalX.Views">
    <views:BaseMainPage.Content>
        <StackLayout>
            <Label Text="{Binding VID, StringFormat='Modelo: {0:F0}'}" FontAttributes="Bold"
               FontSize="Large"/>
            <Label Text="{Binding LicencePlate, StringFormat='Matrícula: {0:F0}'}"/>
            <Label Text="{Binding Chassis, StringFormat='Chassis: {0:F0}'}"/>
            <Label Text="{Binding Km, StringFormat='Ultimos Km Registados: {0:N0}'}"/>
        </StackLayout>
    </views:BaseMainPage.Content>
</views:BaseMainPage>

and xaml.cs

public partial class CarDetails : BaseMainPage
        {

            public CarDetails(CarInfo car)
            {
                InitializeComponent();
                BindingContext = new CarDetailViewModel(this);
                App.currentPage = this;
                if (car != null)
                {
                    this.Title = "Dados de " + car.MakerandModel;
                }
                else
                {
                    this.Title = "Escolha uma Viatura";
                }
            }

        }

I'm facing a lot of issues here because my car icon is a part of my "BaseMainPage" which is extended by the other Views (so the icon can be shown on all views)...

So when I click the button, the application doesn't know its current page... I thought I might use the Navigation Stack to reload it but I don't quite know how to do this... Hope you guys can help

Upvotes: 2

Views: 25592

Answers (3)

bifedefrango
bifedefrango

Reputation: 85

I've just used MessagingCenter and I've called it with my OnPropertyChanged and this seemed to do the work! Thanks a lot!

View Model:

OnPropertyChanged("SelectedCar");
                if (_SelectedCar != null)
                {
                    CarSelected(_SelectedCar);
                    MessagingCenter.Send(this, "Hi");
                }

My other view model's constructor

MessagingCenter.Subscribe<MyCarViewModel>(this, "Hi", (sender) => {
                this.currentCar = App.choosedCar;
            });

Upvotes: 1

Aditya Deshpande
Aditya Deshpande

Reputation: 131

Well, essentially you do not need to refresh page or reload page, you just need to refresh the data.

since you are using OnPropertyChanged(INotifyPropertyChanged) you are half way there. instead of using List CarList use ObservableCollection CarList.

and if you deliberately want to reload the page, on dismissing the pop.up save your data and call the constructor/reinitiate the Page.

hopefully you should achieve what you are looking for.

Upvotes: 4

Jannik R.
Jannik R.

Reputation: 176

I think you don't need to reload the page, you need to reload your data. Your page will be updated automatically with the databindings.

For me it looks like you're using Prism, so you could override the OnNavigatingTo Method and load the data every time the page is "opened".

Upvotes: 1

Related Questions