使用 Application.Current.Properties 存储数据

Storing of Data with Application.Current.Properties

我有一个注册页面。首先,用户输入他的信息,如电子邮件、密码、体重、身高…… 用户填写此表单后,我想从此页面导航到我的登录页面。 (导航部分还好我能搞定)

但是,我不知道如何使用 Application.Current.Properties 选项存储电子邮件、密码组合,然后在登录页面中使用此信息进行输入。

我正在添加我的注册页面代码。如果您有任何想法或建议,我欢迎他们。

    <?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="HealNow.Signup"
             NavigationPage.HasNavigationBar="False">
    <ContentPage.Resources>
        <ResourceDictionary>
            <LinearGradientBrush x:Key="Theme" EndPoint="0,1">
                <GradientStop Color="#48b6a6" Offset="0.1" />
                <GradientStop Color="#2b78d4" Offset="1.0" />
            </LinearGradientBrush>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0">
        </OnPlatform>
    </ContentPage.Padding>
    <StackLayout Background="{StaticResource Theme}">
        <StackLayout Background="{StaticResource Theme}" >
        </StackLayout>
        <StackLayout Margin="0,35,0,0">
            <Image HorizontalOptions="Center"    HeightRequest="60" WidthRequest="60" Source="heartbeatt.png" ></Image>
        </StackLayout>
        <StackLayout Padding="0" Margin="10,20,10,0" HorizontalOptions="FillAndExpand" >
            <Frame BackgroundColor="Transparent" HeightRequest="600" Padding="0" Margin="0">
                <StackLayout>


                    <StackLayout Padding="0" Margin="15,10">

                        <Frame BackgroundColor="Transparent" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="user.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry x:Name ="email" Placeholder="Email" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0" Completed="OnChange"/>
                            </StackLayout>
                        </Frame>

                        <Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,12,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="broken.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry x:Name ="password" Placeholder="Password" IsPassword="True" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0" Completed="OnChange"/>
                            </StackLayout>
                        </Frame>


                        <Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,12,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="babyy.png" Aspect="AspectFill"  Margin="0"/>
                                </Frame>
                                <Entry Placeholder="Date of Birth"  TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
                            </StackLayout>
                        </Frame>

                        <Frame BackgroundColor="Transparent" Margin="0,12,0,0" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="barrr.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry Placeholder="Weight" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
                            </StackLayout>
                        </Frame>

                        <Frame BackgroundColor="Transparent" Margin="0,12,0,0" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="height.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry Placeholder="Height" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
                            </StackLayout>
                        </Frame>



                        <Button Margin="30" Text="SIGN UP" BackgroundColor="#2a52be" TextColor="White" CornerRadius="30" Clicked="Button_Clicked" />


                       

                        

                </StackLayout>
        </StackLayout>
            </Frame>
            
        </StackLayout>
        </StackLayout>
</ContentPage>

and it looks like this

Signup.xaml.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace HealNow
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Signup : ContentPage
    {
        public Signup()
        {
            InitializeComponent();
            Application.Current.Properties[email.Text] = password.Text;
            if (Application.Current.Properties.ContainsKey("Email"))
            {
                var email = Application.Current.Properties["Email"] as string;

            }
            if (Application.Current.Properties.ContainsKey("Password"))
            {
                var password = Application.Current.Properties["Password"] as string;

            }
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new MainPage());
        }

        private void OnChange(object sender, EventArgs e)
        {
            Application.Current.Properties["Email"] = email.Text;
            Application.Current.Properties["Password"] = password.Text;

            Application.Current.SavePropertiesAsync();
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
        }

    }
}

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"
             x:Class="HealNow.MainPage"
             NavigationPage.HasNavigationBar="False">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0">
        </OnPlatform>
    </ContentPage.Padding>
    <StackLayout>
        <StackLayout.Background>
            <LinearGradientBrush x:Name="Theme" EndPoint="0,1">
                <GradientStop Color="#48b6a6" Offset="0.1" />
                <GradientStop Color="#2b78d4" Offset="1.0" />
            </LinearGradientBrush>
        </StackLayout.Background>
        <StackLayout Margin="0,35,0,0">
            <Image HorizontalOptions="Center"    HeightRequest="60" WidthRequest="60" Source="heartbeatt.png" ></Image>
        </StackLayout>
        <StackLayout Padding="0" Margin="10,20,10,0" HorizontalOptions="FillAndExpand" >
            <Frame BackgroundColor="Transparent" HeightRequest="600" Padding="0" Margin="0">
                <StackLayout>

                    <StackLayout Padding="0" Margin="15,10">

                        <Frame BackgroundColor="Transparent" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="user.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry x:Name="email2"  Placeholder="Email" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
                            </StackLayout>
                        </Frame>

                        <Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,15,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
                            <StackLayout Orientation="Horizontal">
                                <Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
                                    <Image Source="broken.png" Aspect="AspectFill" Margin="0"/>
                                </Frame>
                                <Entry x:Name="password2" Placeholder="Password" IsPassword="True" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
                            </StackLayout>
                        </Frame>

                        <StackLayout  Orientation="Horizontal">
                            <CheckBox IsChecked="False" Color="White" />
                            <Label Text="Remember me" TextColor="White" FontSize="10" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />

                            <Label Text="Forgot Password" TextColor="White"  HorizontalOptions="EndAndExpand" FontSize="10" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                        </StackLayout>

                        <Button Margin="10" Text="SIGN IN" BackgroundColor="#2b78d4" TextColor="White" CornerRadius="30" Clicked="Button_Clicked" />
                        <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                            <Label Text="Still Not Connected ?" TextColor="White" FontSize="10"/>
                            <Button Margin="0,-17,0,0" Text="Sign Up" TextColor="White" FontAttributes="Bold" FontSize="10" BackgroundColor="Transparent" Clicked="Button_Clicked2" />

                        </StackLayout>

                        <StackLayout Margin="0,25,0,0" Padding="0">
                            <Grid>
                                <BoxView BackgroundColor="White" HeightRequest="1" WidthRequest="150"  HorizontalOptions="Center" VerticalOptions="Center"/>
                                <Frame BackgroundColor="White" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
                                    <Label Text="OR" TextColor="#666666" FontSize="Small" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
                                </Frame>
                            </Grid>
                        </StackLayout>

                        <StackLayout Margin="0,25" Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                            <Frame BackgroundColor="Transparent" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
                                <Image Source="https://www.pngitem.com/pimgs/m/44-440455_transparent-background-fb-logo-hd-png-download.png" Aspect="AspectFill" Margin="0"/>
                            </Frame>
                            <Frame BackgroundColor="Transparent" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
                                <Image Source="https://blog.hubspot.com/hubfs/image8-2.jpg" Aspect="AspectFill" Margin="0"/>
                            </Frame>
                        </StackLayout>

                    </StackLayout>
                </StackLayout>
            </Frame >
        </StackLayout>
    </StackLayout >
</ContentPage>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace HealNow
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new WelcomePage());
        }
        private async void Button_Clicked2(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new Signup());
        }
    }
}

正如 Jason 上面给出的文件所说,你只需要使用 Properties 字典,用 string 键保存数据。

在您的情况下,如果您只想存储您的电子邮件和密码:

<Entry x:Name ="email" Placeholder="Email" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
<Entry x:Name="password" Placeholder="Password" IsPassword="True" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>

报名成功后:

Application.Current.Properties["Email"] = email.Text;
Application.Current.Properties["Password"] = password.Text;

并读取数据:

if (Application.Current.Properties.ContainsKey("Email"))
{
   var email= Application.Current.Properties ["Email"] as string;
  
}
if (Application.Current.Properties.ContainsKey("Password"))
{
   var password = Application.Current.Properties ["Password"] as string;
  
}

如果您想将电子邮件与 password.You 配对,可以使用 emial 作为键,密码作为值。

Application.Current.Properties[email.Text] = password.Text;

然后当你想在登录页面查看邮箱和密码时:

if (Application.Current.Properties.ContainsKey(the email you entered))
{
  var password = Application.Current.Properties [the email you entered] as string;
  // then you could check that the saved password matches the one you entered

}