星期六, 4月 21, 2018

[XF] NavigationPage - 登錄畫面

使用者登錄 App 後,透過重置 MainPage 來開啟一個 NavigationPage,避免使用者進入 App 後還可以回到登錄畫面

App.xaml.cs
namespace NaviLogin
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            // 一開始顯示 Login 畫面
            MainPage = new Login();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}
Login.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NaviLogin.Login">
    <ContentPage.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <StackLayout Orientation="Horizontal">
                <Label Text="帳號"/>
                <Entry WidthRequest="100"></Entry>
            </StackLayout>
            <StackLayout Orientation="Horizontal">
                <Label Text="密碼"/>
                <Entry WidthRequest="100"></Entry>
            </StackLayout>
            <Button x:Name="btnLogin" Text="登錄" Clicked="BtnLogin_Clicked"></Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
Login.xaml.cs
namespace NaviLogin
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Login : ContentPage
    {
        public Login()
        {
            InitializeComponent();
        }

        private void BtnLogin_Clicked(object sender, EventArgs e)
        {
            // 登錄後把 MainPage 置換掉
            App.Current.MainPage = new NavigationPage(new MainPage());
        }
    }
}
[XF] NavigationPage - 登錄畫面-1
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:NaviLogin"
             x:Class="NaviLogin.MainPage"
             Title="主頁面">

 <Label Text="回不去囉" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />

</ContentPage>
[XF] NavigationPage - 登錄畫面-2

沒有留言:

張貼留言