【问题标题】:Xamarin.Forms Navigate to another page using ViewModelXamarin.Forms 使用 ViewModel 导航到另一个页面
【发布时间】:2015-02-26 10:21:30
【问题描述】:

我有几个 ContentPages,我想通过点击页面中的一个元素从一个内容页面导航到另一个内容页面。我有我的 ViewModel 类:

 class JumpVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private INavigation _navigation;

        public ICommand NewPage
        {
            get
            {
                return new Command(async () =>
                {
                    await _navigation.PushAsync(new MySettingsPage());
                });
            }
        }

        public JumpVM() { }

        public JumpVM(INavigation navitation)
        {
            _navigation = navitation;
        }
    }

这是我的一页(为了篇幅,我只放了相关代码):

BindingContext = new JumpVM(this.Navigation);

....

 Image fbInvite = new Image
            {
                Source = ImageSource.FromResource(Constants.ASSETLOCATION + ".facebookInviteIcon.png"),
                HorizontalOptions = LayoutOptions.Center
            };
            fbInvite.GestureRecognizers.Add(new TapGestureRecognizer(sender =>
                {
                    //navigation in the method below
                    FaceboonInviteFriends();
                    fbInvite.Opacity = 0.8;
                    fbInvite.FadeTo(1);
                }));

我希望在单击图像时执行 JumpVM 类中的命令并导航到那里的页面。我该怎么做?

【问题讨论】:

    标签: c# mvvm xamarin xamarin.forms


    【解决方案1】:

    这是在 ViewModel 概念中将一页导航到另一页的答案。

    public ICommand NavigationList { get; set; }
    
    NavigationList = new Command(GetListview);  
    
    public void  GetListview()
    {
       Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new ListViewPerson());
    }
    

    【讨论】:

      【解决方案2】:

      尝试在FadeTo 行之后添加以下行: ((JumpVM)BindingContext).NewPage.Execute(null).

      【讨论】:

      • 实际上,它确实回答了。我将在我的答案中添加一个小提示。
      • 谢谢 :) 这有帮助。我将标记为已回答,但如果您能帮助我解决与此相关的另一件事,那就太好了。正如你所看到的,我已经硬编码了它会跳转到“await _navigation.PushAsync(new MySettingsPage());”的地方。 .但是为了获得更大的灵活性,我可以根据我点击的按钮将某个页面放在那里吗?
      • 当然,您可以创建任何您想要的页面并将其用作参数。
      • 喜欢 BindingContext = new JumpVM(new MyNewPage(),this.Navigation);?
      【解决方案3】:

      如果您使用 ViewModels,您可以使用 ICommand 轻松实现。

      namespace YourApp.ViewModels
      {
          public class CurrentPageViewModel
          {
              public ICommand BackToPage  {get; private set; }
      
              public CurrentPageViewModel()
              {
                  BackToPage = new Command(async () => {
                    await  Application.Current.MainPage.Navigation.PushModalAsync(new MainPage());
                  });
              }
          }
      }
      

      而在你想去的页面的ViewModel中,你需要实现PopAsync如下。

      namespace YourApp.ViewModels
      {
          public class MainPageViewModel 
          {
              public ICommand BackToMain { get; private set; }
      
              public MainPageViewModel()
              {
                  BackToMain = new Command(async () => {
                      await Application.Current.MainPage.Navigation.PopAsync();
                  });
              }
          }
      }
      

      还记得在当前页面和你想要这样的视图代码隐藏中使用绑定。

      namespace RealmApp1.Views
      {
          public partial class MainPage : ContentPage
          {
              public MainPage()
              {
                  InitializeComponent();
                  BindingContext = new MainPageViewModel();
              }      
          }
      }
      

      希望它对你有用! 有一个漂亮的代码!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 1970-01-01
        • 2015-08-12
        相关资源
        最近更新 更多