iT邦幫忙

2022 iThome 鐵人賽

DAY 24
0
Software Development

30天學習.Net MAUI系列 第 24

24.MAUI使用firebase進行Auth (一)

  • 分享至 

  • xImage
  •  

接下來,我們要加入會員登入登出功能,今天先來建立頁面

在Pages創建AuthPage
https://ithelp.ithome.com.tw/upload/images/20221009/20108931HhEOO7XCdm.png

以及RegisterPage
https://ithelp.ithome.com.tw/upload/images/20221009/20108931We8SPvzTDM.png

AuthPage.xaml

// AuthPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Todo.AuthPage"
             Title="AuthPage">
    <VerticalStackLayout>
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

// AuthPage.xaml.cs

namespace Todo;

public partial class AuthPage : ContentPage
{
	public AuthPage()
	{
		InitializeComponent();
	}
}

RegisterPage.xaml

// RegisterPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Todo.RegisterPage"
             Title="RegisterPage">
    <VerticalStackLayout>
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

// RegisterPage.xaml.cs

namespace Todo;

public partial class RegisterPage : ContentPage
{
	public RegisterPage()
	{
		InitializeComponent();
	}
}

首先先註冊route,打開我們的Todo\AppShell.xaml.cs

namespace Todo;

public partial class AppShell : Shell
{
	public AppShell()
	{
		InitializeComponent();
		Routing.RegisterRoute(nameof(AddTodoPage), typeof(AddTodoPage));
		// register
		Routing.RegisterRoute(nameof(AuthPage), typeof(AuthPage));
		Routing.RegisterRoute(nameof(RegisterPage), typeof(RegisterPage));
	}
}

接著打開我們的Todo\Pages\UserPage.xaml.cs
加入

namespace Todo;

public partial class UserPage : ContentPage
{
	public UserPage()
	{
		InitializeComponent();
	}

    private async void AuthRoute(object sender, EventArgs e)
    {
        await AppShell.Current.GoToAsync(nameof(AuthPage));
    }
}

在打開UI介面Todo\Pages\UserPage.xaml加入方法

<ContentView x:Name="Auth">
            <Frame Margin="15">
                <Button Text="Login" Clicked="AuthRoute" />
            </Frame>
        </ContentView>

Todo\Pages\UserPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:Todo.Views"
    x:Class="Todo.UserPage"
    Title="UserPage">
		
		...
        <ContentView x:Name="UserBody">
            <Frame Margin="15">
                <VerticalStackLayout>
                    <Label Text="Todo Count: 0" />
                    <Label Text="Done Count: 0" />
                </VerticalStackLayout>
            </Frame>
        </ContentView>

        <ContentView x:Name="Auth">
            <Frame Margin="15">
                <Button Text="Login" Clicked="AuthRoute" />
            </Frame>
        </ContentView>
    </VerticalStackLayout>
</ContentPage>

打開Todo\Pages\AuthPage.xaml.cs加入route方法

namespace Todo;

public partial class AuthPage : ContentPage
{
	public AuthPage()
	{
		InitializeComponent();
	}

    private async void RegisterRoute(object sender, EventArgs e)
    {
        await AppShell.Current.GoToAsync(nameof(RegisterPage));
    }
}

打開Todo\Pages\AuthPage.xaml加入UI

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Todo.AuthPage"
             Title="AuthPage">
    <VerticalStackLayout>
        <Entry Placeholder="Email" />
        <Entry Placeholder="Password" IsPassword="true" />

        <HorizontalStackLayout>
            <Label Text="No Account?" />
            <Button Text="Register" Clicked="RegisterRoute"/>
        </HorizontalStackLayout>
    </VerticalStackLayout>
</ContentPage>

打開Todo\Pages\RegisterPage.xaml加入UI

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Todo.RegisterPage"
             Title="RegisterPage">
    <VerticalStackLayout>
        <Label 
            Text="Register"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
        <Entry Placeholder="Email" />
        <Entry Placeholder="Password" IsPassword="true" />
        <Entry Placeholder="Confirm" IsPassword="true" />
        <Button Text="Register Account" />
    </VerticalStackLayout>
</ContentPage>

打開測試
https://ithelp.ithome.com.tw/upload/images/20221009/20108931LkRvikvDp3.png

https://ithelp.ithome.com.tw/upload/images/20221009/20108931T4uaGNRF9E.png

https://ithelp.ithome.com.tw/upload/images/20221009/20108931qCFv5HgLE4.png


今天先完成UI以及路由介面,明天開始實作並添加功能,並使用Firebase來進行我們的Auth


上一篇
23.關於.NET MAUI Shell (二)
下一篇
25.MAUI使用firebase進行Auth (二)
系列文
30天學習.Net MAUI30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言