【发布时间】: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>
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 Storagedocs.microsoft.com/en-us/xamarin/essentials/… 更好更容易
标签: c# xaml xamarin xamarin.forms data-storage