eduardogr

Untitled

Jun 1st, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. Class
  2.  
  3.     class User : INotifyPropertyChanged {
  4.  
  5.         private string _username;
  6.         public string username {
  7.             get { return _username; }
  8.             set {
  9.                 if (_username != value) {
  10.                     _username = value;
  11.                     RaisePropertyChanged();
  12.                 }
  13.             }
  14.         }
  15.  
  16.  
  17.         private string _password;
  18.         public string password {
  19.             get { return _password; }
  20.             set {
  21.                 if (_password != value) {
  22.                     _password = value;
  23.                     RaisePropertyChanged();
  24.                 }
  25.             }
  26.         }
  27.  
  28.  
  29.         #region Notify Property Changed Members
  30.         public event PropertyChangedEventHandler PropertyChanged;
  31.         private void RaisePropertyChanged([CallerMemberName] string propertyName = "") {
  32.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  33.         }
  34.         #endregion
  35.     }
  36. }
  37.  
  38. viewModel
  39.  
  40.     class RegisterViewModel {
  41.  
  42.         public RegisterCommand RegisterCommand { get; set; }
  43.         User User;
  44.  
  45.         public RegisterViewModel(User user) {
  46.             RegisterCommand = new RegisterCommand(this);
  47.            
  48.         }
  49.  
  50.         internal void SendData(object data) {
  51.             Application.Current.MainPage.Navigation.PushAsync(new Page1());
  52.         }
  53.     }
  54. }
  55.  
  56. Register Command
  57.  
  58.    class RegisterCommand : ICommand {
  59.  
  60.         public RegisterViewModel RegisterViewModel { get; set; }
  61.  
  62.         public RegisterCommand(RegisterViewModel registerViewModel) {
  63.             RegisterViewModel = registerViewModel;
  64.         }
  65.  
  66.         public event EventHandler CanExecuteChanged;
  67.  
  68.         public bool CanExecute(object parameter) {
  69.             if (parameter != null) {
  70.                 return true;
  71.             }
  72.             return false;
  73.         }
  74.  
  75.         public void Execute(object parameter) {
  76.             RegisterViewModel.SendData(parameter);
  77.         }
  78.     }
  79. }
  80.  
  81. MainPge.xaml
  82.  
  83.  <ContentPage.Resources>
  84.         <model:User x:Key="model" />
  85.         <vm:RegisterViewModel x:Key="RegisteVM" />
  86.     </ContentPage.Resources>
  87.  
  88.     <StackLayout>
  89.         <Entry Placeholder="Username"
  90.                Text="{Binding username, Source={StaticResource model}}" />
  91.         <Entry Placeholder="Password"
  92.                Text="{Binding password, Source={StaticResource model}}"/>
  93.         <Button BackgroundColor="DarkCyan" Command="{Binding RegisterCommand, Source={StaticResource RegisteVM}}" >
  94.             <Button.CommandParameter>
  95.                 <model:User/>
  96.             </Button.CommandParameter>
  97.         </Button>
  98.     </StackLayout>
  99.  
  100. SecondPage
  101.  
  102. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  103.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  104.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  105.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  106.              mc:Ignorable="d"
  107.              x:Class="Question.View.Page1">
  108.     <ContentPage.Content>
  109.         <StackLayout>
  110.             <Label Text="Welcome to Xamarin.Forms!"
  111.                 VerticalOptions="CenterAndExpand"
  112.                 HorizontalOptions="CenterAndExpand" />
  113.         </StackLayout>
  114.     </ContentPage.Content>
  115. </ContentPage>
Add Comment
Please, Sign In to add comment