如何使用mvvm以xamarin形式创建登录页面

时间:2018-02-24 09:25:58

标签: xamarin.forms

我尝试使用mvvm设计模式在xamarin表单中创建登录页面 我在我的代码中得到了期待,但我不知道原因 我创建了ViewModel页面的代码

 public class LoginViewModel:ContentPage,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public User user { get; set; }
        private string username;


        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Username"));
            }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Password"));
            }
        }

        public ICommand SubmitCommand { set; get; }

        public LoginViewModel()
        {
            user = new User();
            SubmitCommand = new Command(OnSubmit);
        }

        private async Task<bool> CheckLogin()
        {
            user = await Services.LoginService.GetUserByUsername(username);

            if (user != null)
            {

                if (user.Password.Trim().Equals(password))
                {
                    return true;
                }
                else
                {
            await LoginView.loginView.DisplayAlert("Error", "Invalid Login, try again", "OK");

                    return false;
                }

            }


            return false;

        }


        private async void OnSubmit()
        {
            await CheckLogin();
        }    
    }
}

和MyLoginView.xaml

<ContentPage 
             xmlns:vm="clr-namespace:PickNgo.ViewModel"
             x:Class="PickNgo.View.LoginView"
             BackgroundColor="White">
    <ContentPage.Content>           
        <StackLayout>
            <StackLayout BackgroundColor="#F27D0C">
            <Image HorizontalOptions="Center" WidthRequest="300" Source="logo.png"/>
            </StackLayout>
            <StackLayout Padding="100,10,100,0">
                <Frame CornerRadius="10" BackgroundColor="White" Padding="0" Margin="0">
                    <Entry x:Name="Email" Text="{Binding Username}" Placeholder="Enter your username"   
                           PlaceholderColor="Black"   
                           Keyboard="Email"  
                           TextColor="Black"/>
                </Frame>                    
                <BoxView HeightRequest="10"/>
                <Frame CornerRadius="10" BackgroundColor="White" Padding="0" Margin="0">
                    <Entry x:Name="Password" Text="{Binding Password}" Placeholder="Enter your password"   
                           PlaceholderColor="Black" HeightRequest="40" WidthRequest="40"  
                           IsPassword="True"  
                           TextColor="Black"/>
                </Frame>                   
            </StackLayout>
            <StackLayout Padding="100,30,100,0">
                <Frame CornerRadius="20" BackgroundColor="#FF6600" Padding="0" Margin="0">
                    <Button Command="{Binding SubmitCommand}" Text="Login" Margin="0" TextColor="White" BackgroundColor="#FF6600"  
            FontAttributes="Bold" FontSize="Large"/>
                </Frame>
            </StackLayout>                
        </StackLayout>    
    </ContentPage.Content>
</ContentPage>

和我的login.cs

public partial class LoginView : ContentPage
    {
        public Action DisplayInvalidLoginPrompt;
        public static LoginView loginView;

        public LoginView ()
        {                
            InitializeComponent();            
        }     
    }

抛出objectSystem.NullReferenceException: Object reference not set to an instance of an object. 在这一行

await LoginView.loginView.Navigation.PushModalAsync(new WelcomeView());

当我调试时发现loginview设置为null但我不知道为什么......有任何帮助吗?

1 个答案:

答案 0 :(得分:1)

因为您尚未初始化LoginView的{​​{1}}变量。

说实话,你真的不需要loginView变量(类本身已经是public static LoginView loginView类型)。如果您想显示提醒,只需使用LoginView(假设PickNgo.App.Current.MainPage.DisplayAlert("", "", "OK");是您项目的名称)