【问题标题】:Storing of Data with Application.Current.Properties使用 Application.Current.Properties 存储数据
【发布时间】:2020-12-02 21:15:19
【问题描述】:

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

但是,我不知道如何使用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());
        }
    }
}

【问题讨论】:

  • 您阅读过文档吗?这是一个非常简单易用的 API。你有什么具体问题? docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 首先谢谢@Jason。我只想存储这些信息,如电子邮件和密码(对我来说最重要的)。然后我想在登录页面调用他们来检查密码和邮箱组合是否正确。
  • @Jason,其实我也想问你,我怎样才能将它们声明为一对我的意思是用于在登录页面检查过程的电子邮件和密码?
  • 在导航时将它们作为参数传递而不是将它们存储在属性中会更有意义
  • 使用Xamarin Essential Secure Storage docs.microsoft.com/en-us/xamarin/essentials/… 更好更容易

标签: c# xaml xamarin xamarin.forms data-storage


【解决方案1】:

正如 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;
  
}

如果您想将电子邮件与密码配对。您可以使用 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

}

【讨论】:

  • 感谢您的详细回答。我想问你,我应该在哪里写这些在项目中? (我的意思是 App.xaml.cs ?)。第一个当然进入 Signup.xaml 页面,但其他 Signup.xaml.cs 或 App.xaml.cs ?
  • 此外,最后一个代码 sn -p 我有点困惑,因为它写的是“您输入的电子邮件”。实际上我想从真实用户那里收到电子邮件?所以我应该写在那里“电子邮件”
  • @xBurak34 (1)写在​​你想检索存储数据的位置。(例如,保存在Signup.xam.cs中,并在Loginpage.xaml.cs中检索。
  • @xBurak34 (2)“您输入的电子邮件”是指您在登录页面的Entry中输入的电子邮件。(例如您保存电子邮件“abc@xxx.com”和密码“123456 ”在注册页面Application.Current.Properties["abc@xxx.com"] = "123456"),然后你想在登录页面查看,当你输入电子邮件“abc@xxx.com”并输入密码“xxxxx”时,你可以找回存储的密码var password = Application.Current.Properties ["abc@xxx.com"] as string;然后验证它们是否相同。当然,如果您输入的电子邮件是其他人,则无法检索数据,这意味着一个新帐户。
  • 我已经添加了我的 Signup.xaml 、 Signup.xaml.cs 、 MainPage.xaml 、 MainPage.xaml.cs 。如果你有时间可以帮帮我吗?我应用了你的步骤(尽可能多地)。我只想在注册页面中获取电子邮件和密码,然后在登录页面中使用用户输入的值对其进行检查。由于我是新手,看起来我在语法上有点挑战。
猜你喜欢
  • 1970-01-01
  • 2012-12-02
  • 2017-04-28
  • 2012-01-25
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多