C# 如何从左侧菜单刷新页面

C# 如何从左侧菜单刷新页面,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,使用MVVM的Xamarin表单..使用burger示例创建左侧菜单 //MainPage [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MainPage : MasterDetailPage { int idOfNewPage; Dictionary<int, NavigationPage>

使用MVVM的Xamarin表单..使用burger示例创建左侧菜单

    //MainPage
    [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MainPage : MasterDetailPage
        {
            int idOfNewPage;
            Dictionary<int, NavigationPage> MenuPages = new Dictionary<int, NavigationPage>();
            public MainPage()
            {
                InitializeComponent();
    
                MasterBehavior = MasterBehavior.Popover;
    
                MenuPages.Add((int)MenuItemType.Products, (NavigationPage)Detail);
            }
    
            public async Task NavigateFromMenu(int id)
            {
                if (!MenuPages.ContainsKey(id))
                {
                    switch (id)
                    {
                        case (int)MenuItemType.Products:
                            MenuPages.Add(id, new NavigationPage(new ProductPage()));
                            break;
                        case (int)MenuItemType.Shopping:
                            MenuPages.Add(id, new NavigationPage(new ShoppingPage()));
                            break;
                        case (int)MenuItemType.Browse:
                            MenuPages.Add(id, new NavigationPage(new ItemsPage()));
                            break;
                        case (int)MenuItemType.About:
                            MenuPages.Add(id, new NavigationPage(new AboutPage()));
                            break;
                    }
    
                    idOfNewPage = id;
                }
    
                idOfNewPage = id;
    
                var newPage = MenuPages[id];
    
                if (newPage != null)
                {
                    Detail = newPage;
    
                    if (Device.RuntimePlatform == Device.Android)
                        await Task.Delay(100);
    
                    IsPresented = false;
                }
            }
    }
    
     [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MenuPage : ContentPage
        {
            MainPage RootPage { get => Application.Current.MainPage as MainPage; }
            List<HomeMenuItem> menuItems;
        
            public MenuPage()
            {
                InitializeComponent();
    
                menuItems = new List<HomeMenuItem>
                {
                    new HomeMenuItem {Id = MenuItemType.Products, Title="Products", Icon =  "back_nav.png", Name = "test" },
                    new HomeMenuItem {Id = MenuItemType.ShoppingCart, Title="Shopping Cart", Icon =  "back_nav.png"},
                    new HomeMenuItem {Id = MenuItemType.Browse, Title="Browse", Icon =  "back_nav.png"},
                    new HomeMenuItem {Id = MenuItemType.About, Title="About", Icon =  "back_nav.png"}
                };
    
                ListViewMenu.ItemsSource = menuItems;
       
                ListViewMenu.ItemSelected += async (sender, e) =>
                {
                    if (e.SelectedItem == null)
                     return;
    
                    var id = (int)((HomeMenuItem)e.SelectedItem).Id;
                    var MenuBtnClicked = (string)((HomeMenuItem)e.SelectedItem).Title;
    
    
                    await RootPage.NavigateFromMenu(id);
    
                    ListViewMenu.SelectedItem = null;
                };
            }
}
因此,通过这一步,我可以看到我已在productPage_ViewModal.WineList和WineListView.ItemsSource中更新了项目的正确数量

然后,即使再次尝试bindingContext…但从购物车页面返回后,该页面仍会加载原始的DrinksToPurchaseList


为什么会这样?为什么我不能刷新VW以进行更新?

如果实际问题出现在ProductsPage上,显示该页面的代码将很有帮助。通常,我建议每次访问页面时使用OnAppearing刷新数据。或者,您也可以在页面之间使用一个通用VM。谢谢您的回复。我已经包含了ProductsPage的代码,现在我添加了OnAppearing,它在从购物车返回到products页面时被调用。我可以看到Listview的VM和itemSource已更新,但屏幕上仍显示old obseravableCollection-DrinksToOrder…请查看Q的更新以包括此感谢…有什么建议吗?“仍显示old obseravableCollection”和“仍加载原始DrinksToPurchaseList”-根据你发布的代码,我不知道它们是什么。对不起-我现在也发布了ShoppingCart代码。。。并补充了更好的解释,这确实不能回答我的任何一个问题。如果你能把你的解决方案发布到某个地方,我可以快速查看一下
Create an event on master page, invoke event from master page in your case blog and consume that event on another page where you want to refresh data..
on master page what you've to do is..
declare event..
public delegate void refreshDelegate();
public static event  refreshDelegate refreshEvent;
__
now invoke this event,
  refreshEvent?.inovke();

now,
   on the page where you want to refresh data, at constructor access the declared event,
masterpage.refreshEvent+=masterpage_refreshEvent;  
   private void masterpage_refreshEvent()
  { 
BindData();
}
       
now, bind the data in invoked event, it'll refresh your screen as expected.  
    public ShoppingCartPage ()
            {
                InitializeComponent();
    
                if (App.globalShoppingCartOC != null)
                {
                    foreach (ProductModel Model in App.globalShoppingCartOC)
                    {
                        if (Model.Quantity > 0)
                        {
//populate LV with global shopping OC
                            Model.SubTotalForItem = Model.Quantity * Model.Price;
                            ShoppingCartViewModel.ShoppingCartList.Add(Model);//ShoppingCartList is the ListView on xaml
                            TotalForAllItems += Model.SubTotalForItem;
                        }
                    }
                }
    
                this.BindingContext = this;
                BindingContext = ShoppingCartViewModel;
            }
    
         void QuantityChanged(object sender, EventArgs e)
            {
    //updates correct quantity on screen and calls
    //UpdateShoppingCartItems()
    }
    
        //update item in shopping cart with new quantity
            void UpdateShoppingCartItems(ProductModel product, int quantity)
            {
                ProductModel result = ShoppingCartViewModel.ShoppingCartList.ToList().Find(x => x.ProductId == product.ProductId);
    
                if (result != null)
                {
                    //remove from list
                    if (quantity < 1)
                    {
                        ShoppingCartViewModel.ShoppingCartList.Remove(result);
                    }
                    else // else update new quantity
                    {
                        result.Quantity = quantity;
                    }
                }
    
                App.globalShoppingCartOC = ShoppingCartViewModel.ShoppingCartList;
            }
    
        //xaml
          <StackLayout Grid.Row="0">
    
                <ListView ItemsSource="{Binding ShoppingCartList}" HasUnevenRows="True" SeparatorVisibility="None">
                    <ListView.Header>
                        <Button Text="Place Order" Clicked="OrderPlaced_BtnClicked"/>
                    </ListView.Header>
                    <ListView.Footer>
                        <Label x:Name="TotalForItems" HorizontalTextAlignment="End" VerticalTextAlignment="Start" Margin="20,20" FontAttributes="Bold"/>
                    </ListView.Footer>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
    
                                <Grid x:Name="ShoppingCartGrid" RowSpacing="25" ColumnSpacing="10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
    
                                    <Label Grid.Column="0" Text="{Binding Id}" VerticalOptions="End" IsVisible="False"/>
                                    <controls:CircleImage  Grid.Column="1"  Grid.Row="1" HeightRequest="60" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Aspect="AspectFill" WidthRequest="66" Grid.RowSpan="2" Source="{Binding Image}"/>
                                    <Label Grid.Column="2" Grid.Row="1" Text="{Binding ProductName}" VerticalOptions="End"/>
                                    <Label Grid.Column="2" Grid.Row="2" VerticalOptions="Start" Text="{Binding Description}"/>
                                    <Label Grid.Column="3" Grid.Row="2" VerticalOptions="Start" Text="{Binding Price, StringFormat='£{0:0.00}'}"/>
    
                                    <Picker Grid.Column="4" Grid.Row="2" VerticalOptions="Start" SelectedIndexChanged="QuantityChanged" SelectedIndex="{Binding Quantity}">
                                        <Picker.Items>
                                            <x:String>0</x:String>
                                            <x:String>1</x:String>
                                            <x:String>2</x:String>
                                            <x:String>3</x:String>
                                            <x:String>4</x:String>
                                            <x:String>5</x:String>
                                            <x:String>6</x:String>
                                        </Picker.Items>
                                    </Picker>
    
                                </Grid>
                            </ViewCell>
protected override void OnAppearing()
    {
        base.OnAppearing();
        
        if (App.globalShoppingCartOC != null)
        {
            if (App.globalShoppingCartOC.Count > 0)
            {
                 foreach(var item in App.globalShoppingCartOC)
                {
                    if(item.Genre == "Wine")
                    {
                        productPage_ViewModal.WineList.Where(x => x.ProductId == item.ProductId).FirstOrDefault().Quantity = item.Quantity;
                    }
                }

                 WineListView.ItemsSource = productPage_ViewModal.WineList;
                 
                  this.BindingContext = this.productPage_ViewModal;
                  
                            }
        }
    }
Create an event on master page, invoke event from master page in your case blog and consume that event on another page where you want to refresh data..
on master page what you've to do is..
declare event..
public delegate void refreshDelegate();
public static event  refreshDelegate refreshEvent;
__
now invoke this event,
  refreshEvent?.inovke();

now,
   on the page where you want to refresh data, at constructor access the declared event,
masterpage.refreshEvent+=masterpage_refreshEvent;  
   private void masterpage_refreshEvent()
  { 
BindData();
}
       
now, bind the data in invoked event, it'll refresh your screen as expected.