How to Refresh or Recall Page

bifedefrangobifedefrango Member ✭✭
edited February 21 in Xamarin.Forms

So, I have this app where I can choose a car and see the car infos... I'm displaying the car's 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 wich 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);
                    Navigation.PushAsync(Navigation.);
                }

            }
        }

        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>

     public partial class CarDetails : BaseMainPage
    {
        public CarDetails()
        {
            InitializeComponent();
            CarInfo car = App.choosedCar;
            BindingContext = car;
            if (car != null)
            {
                this.Title = "Dados de " + car.MakerandModel;
            }
            else
            {
                this.Title = "Escolha uma Viatura";
            }
        }

        public CarDetails(CarInfo car)
        {
            InitializeComponent();
            BindingContext = car;
            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

Best Answers

  • ColeXColeX Xamurai
    Accepted Answer
    1. Use MessageCenter or Event callback to pass the model to the class CarDetails .

    2. Make model CarInfo implement INotifyPeopertyChanged to trigger the changes.

Answers

  • M_Shah04M_Shah04 USMember ✭✭✭

    You can try by having one common view model for both pages, one having your car icon and another one is popup,
    So once you select the car from popup you will get that selected one on that view model only display the content accordingly, no need to reload the page then.!

    Hope this may solve your issue.

  • bifedefrangobifedefrango Member ✭✭
    edited February 21

    The thing is, I want to use this for multiple pages... So it's kind of hard to do this

  • bifedefrangobifedefrango Member ✭✭

    and I'm saving the current car in an global variable and not binding it directly...

  • amirvenusamirvenus USMember ✭✭✭

    If you use BindingContext, that is what I would do:

    BindingContext = null;
    BindingContext = Singleton.Instance;

    (Singleton.Instance is the name of your ViewModel)

    Good luck

  • bifedefrangobifedefrango Member ✭✭

    I can't because I never know when the App.currentCar is changed

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer
    1. Use MessageCenter or Event callback to pass the model to the class CarDetails .

    2. Make model CarInfo implement INotifyPeopertyChanged to trigger the changes.

  • bifedefrangobifedefrango Member ✭✭

    That's it! This might work! Thanks a lot!

Sign In or Register to comment.