从视图模型到另一个视图的方向

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

我在一个视图模型中过滤一个列表,但过滤完成后,我需要被重定向到另一个有数据的页面。这是我目前的做法,但是页面没有导航栏,即使我在xaml中把它设置为true,一旦我按下一个项目,我就会得到一个错误 我的错误 System.InvalidOperationException: 'PushAsync is not supported globally on Android, please use a NavigationPage'.

我的过滤器 视图模型

     bool _filterAllItems;
            public bool FilterAllItems
            {
                set
                {
                    _filterAllItems = value;
                    NotifyPropertyChanged();

                    if (_filterAllItems)
                    {
                        _parentCategoryId = -1;
                        FillArticles();
                         Application.Current.MainPage.Navigation.PushModalAsync(new ArticlesForPurchaseFiltered());


                    }

                }
                get => _filterAllItems;
            }


this is how i am selecting the item in the content page




 private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
            {
    #if !NAVIGATION
                var selectedItem = ((ListView)sender).SelectedItem;
                var articlePage = new MyArticle(selectedItem as ArticleDetailData);

                await Navigation.PushAsync(articlePage);
    #endif

        }

Can you please suggest how to redirect to View page from View model so i dont use 
 Application.Current.MainPage.Navigation.PushModalAsync(new ArticlesForPurchaseFiltered());
xamarin.forms
1个回答
0
投票

原因是

我们不能调用方法 PushModalAsync 在MasterDetailPage中。最好是在特定的ContentPage中调用它。

解决方案。

我们可以使用 消息中心 发送通知。

在ViewModel中

 _parentCategoryId = -1;
 FillArticles();

MessagingCenter.Send<Object>(this,"Push");

在ContentPage(包含列表视图)的构造函数中使用

public xxxPage()
{
    InitializeComponent();


    MessagingCenter.Subscribe<Object>(this, "Push", (args) => { 

       // Navigation.PushAsync(articlePage);

   });
}

解决方法2.可以将Page作为参数传递给ViewModel。

你可以把Page作为参数传递给ViewModel。

private ContentPage currentPage;

public FilterArticlesForPurchaseViewModel(ContentPage page)
{
    currentPage = page ;
}
currentPage.Navigation.PushModalAsync(new ArticlesForPurchaseFiltered());

并在ContentPage中

BindingContext = new FilterArticlesForPurchaseViewModel(this); 
© www.soinside.com 2019 - 2024. All rights reserved.