代码之家  ›  专栏  ›  技术社区  ›  dotnetdevcsharp

从xamarin表单中的视图模型导航

  •  0
  • dotnetdevcsharp  · 技术社区  · 6 年前

    我试图从视图模型中推送一个xamrian表单的视图,但是我不能让它真正工作当用户输入正确的用户名和密码时,它应该显示主页。

    通常我会用

    var stocktakepage = new StockTake();         
    await Navigation.PushAsync(stocktakepage);
    

    public  class LoginViewModel : INotifyPropertyChanged
    {
        public Action DisplayInvalidLoginPrompt;
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private string email;
        public string Email
        {
            get { return email; }
            set
            {
                email = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Email"));
            }
        }
        private string password;
        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Password"));
            }
        }
        public ICommand SubmitCommand { protected set; get; }
        public LoginViewModel()
        {
            SubmitCommand = new Command(OnSubmit);
        }
        public void OnSubmit()
        {
            if (email != "handheld1" || password != "test123")
            {
                DisplayInvalidLoginPrompt();
            }else
    
            {
    
    
    
    
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   dotnetdevcsharp    6 年前

    我在这里找到了答案

    https://forums.xamarin.com/discussion/21822/call-navigation-pushasync-from-viewmodel 但在我的登录主页上我也有这个。它的作用就像一个委托,允许您从原始调用页推送视图。

        public Login()
        {
             var vm = new LoginViewModel();
            this.BindingContext = vm;
    
    Password.Completed += (object sender, EventArgs e) =>
    {
         vm.SubmitCommand.Execute(null);
    
    
      };
    }
    
        2
  •  0
  •   GeralexGR    6 年前

    您还可以使用下面的从 ViewModel

    await App.Current.MainPage.Navigation.PushAsync(new PageName());
    

    await App.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new PageName()) { BarBackgroundColor = Color.FromHex("#101010"), BarTextColor = Color.White, }, true);
    
    推荐文章