如何在Xamarin表单上刷新或调用页面

问题描述 投票:0回答:3

所以,我有这个应用程序,我可以选择一辆车,看到汽车信息......我正在展示像this这样的汽车。

我正在使用Rg.Plugins.Popup,所以当我点击汽车图标时,它会打开这个弹出的“我的车”

所以现在我遇到了一个问题,就是当我选择一辆车时,我想刷新我当前的页面,以便显示汽车的信息......我正在处理汽车按钮点击下一个视图模型:

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;
        }
}

我希望这个视图能够刷新

<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>

和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";
                }
            }

        }

我在这里遇到很多问题因为我的汽车图标是我的“BaseMainPage”的一部分,它被其他视图扩展(所以图标可以在所有视图上显示)...

所以,当我点击按钮时,应用程序不知道它的当前页面......我想我可能会使用导航堆栈来重新加载它但我不知道该怎么做...希望你们能帮忙

c# xamarin xamarin.forms
3个回答
1
投票

好吧,基本上你不需要刷新页面或重新加载页面,你只需要刷新数据。

因为你正在使用OnPropertyChanged(INotifyPropertyChanged),所以你在那里。而不是使用List CarList使用ObservableCollection CarList。

如果你故意想重新加载页面,在解除pop.up时保存你的数据并调用构造函数/重新启动页面。

希望你应该达到你想要的。


0
投票

我认为您不需要重新加载页面,您需要重新加载数据。您的页面将使用数据绑定自动更新。

对我来说,看起来你正在使用Prism,所以你可以覆盖OnNavigatingTo方法并在每次页面“打开”时加载数据。


0
投票

我刚刚使用了MessagingCenter,并且我用OnPropertyChanged调用了它,这似乎完成了工作!非常感谢!

查看型号:

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

我的另一个视图模型的构造函数

MessagingCenter.Subscribe<MyCarViewModel>(this, "Hi", (sender) => {
                this.currentCar = App.choosedCar;
            });
© www.soinside.com 2019 - 2024. All rights reserved.