如何从 Xamarin Forms 中的不同 ContentPage access/inherit 私有字段 属性?
How to access/inherit a private field property from a different ContentPage in Xamarin Forms?
我有以下 ContentPage(“LoginPage”),其中提示用户输入用户名和密码:
<?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="WeatherApp.LoginPage"
BackgroundImageSource="https://i.pinimg.com/236x/dc/d8/e5/dcd8e5e716b307d6ecfcbc73168fdff5--abstract-iphone-wallpaper-wallpaper-s.jpg"
NavigationPage.HasNavigationBar="False">
<StackLayout Padding="20,20,20,20"
Spacing="20">
<StackLayout>
<Label Text="Username" FontAttributes="Bold" FontFamily="Roboto" TextColor="White" FontSize="20"/>
<Entry x:Name="Username" Placeholder="Please enter username..." FontFamily="Roboto"/>
</StackLayout>
<StackLayout>
<Label Text="Password" FontAttributes="Bold" FontFamily="Roboto" TextColor="White" FontSize="20"/>
<Entry x:Name="Password" Placeholder="Please enter password..." IsPassword="True" FontFamily="Roboto"/>
</StackLayout>
<Button Text="Log in" HorizontalOptions="Center" VerticalOptions="Center" FontFamily="OpenSans" TextColor="DeepSkyBlue" BackgroundColor="#bef9cf" CornerRadius="25"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage>
后面的代码如下:
using System;
using System.Collections.Generic;
using WeatherApp.Models;
using Xamarin.Forms;
namespace WeatherApp
{
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
User user = new User();
var username = Username.Text;
var password = Password.Text;
var _user = user.GetUser(username);
if (username == _user.Username && password == _user.Password)
{
await DisplayAlert("Log in successful", null, "OK");
await Navigation.PushAsync(new ProfilePage());
}
else
{
await DisplayAlert("Log in failed", $"No such combination with username: {username}", "Try again");
}
}
}
}
在另一个内容页面中,我想访问此条目 属性 用户名,以便我可以在“问候语”属性 中使用它以及基于时间的问候语(例如“晚上好, {用户名}!”)。我尝试了下面的代码,但还是无法从 LoginPage 访问属性:
using System;
using System.ComponentModel;
namespace WeatherApp.Models
{
public class ProfilePageViewModel : INotifyPropertyChanged
{
private string greeting;
public string Greeting
{
get
{
return greeting;
}
set
{
greeting = value;
OnPropertyChanged(nameof(Greeting));
}
}
public ProfilePageViewModel()
{
User user = new User();
var username = LoginPage.Username;
var _user = user.GetUser(username);
Greeting = TimeBasedGreeting() + ", {username}!";
}
因此我需要访问它,但它是受保护的 属性。
有没有一种方法可以继承 LoginPage 并因此继承其受保护的属性?
最简单的做法就是将用户对象传递到新页面。然后 ProfilePage
将引用用户对象并可以从那里获取用户名
if (username == _user.Username && password == _user.Password)
{
await DisplayAlert("Log in successful", null, "OK");
await Navigation.PushAsync(new ProfilePage(_user));
}
或者,您可以在 App
class 上创建一个 User
属性,这将允许您应用中的任何页面访问它
我有以下 ContentPage(“LoginPage”),其中提示用户输入用户名和密码:
<?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="WeatherApp.LoginPage"
BackgroundImageSource="https://i.pinimg.com/236x/dc/d8/e5/dcd8e5e716b307d6ecfcbc73168fdff5--abstract-iphone-wallpaper-wallpaper-s.jpg"
NavigationPage.HasNavigationBar="False">
<StackLayout Padding="20,20,20,20"
Spacing="20">
<StackLayout>
<Label Text="Username" FontAttributes="Bold" FontFamily="Roboto" TextColor="White" FontSize="20"/>
<Entry x:Name="Username" Placeholder="Please enter username..." FontFamily="Roboto"/>
</StackLayout>
<StackLayout>
<Label Text="Password" FontAttributes="Bold" FontFamily="Roboto" TextColor="White" FontSize="20"/>
<Entry x:Name="Password" Placeholder="Please enter password..." IsPassword="True" FontFamily="Roboto"/>
</StackLayout>
<Button Text="Log in" HorizontalOptions="Center" VerticalOptions="Center" FontFamily="OpenSans" TextColor="DeepSkyBlue" BackgroundColor="#bef9cf" CornerRadius="25"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage>
后面的代码如下:
using System;
using System.Collections.Generic;
using WeatherApp.Models;
using Xamarin.Forms;
namespace WeatherApp
{
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
User user = new User();
var username = Username.Text;
var password = Password.Text;
var _user = user.GetUser(username);
if (username == _user.Username && password == _user.Password)
{
await DisplayAlert("Log in successful", null, "OK");
await Navigation.PushAsync(new ProfilePage());
}
else
{
await DisplayAlert("Log in failed", $"No such combination with username: {username}", "Try again");
}
}
}
}
在另一个内容页面中,我想访问此条目 属性 用户名,以便我可以在“问候语”属性 中使用它以及基于时间的问候语(例如“晚上好, {用户名}!”)。我尝试了下面的代码,但还是无法从 LoginPage 访问属性:
using System;
using System.ComponentModel;
namespace WeatherApp.Models
{
public class ProfilePageViewModel : INotifyPropertyChanged
{
private string greeting;
public string Greeting
{
get
{
return greeting;
}
set
{
greeting = value;
OnPropertyChanged(nameof(Greeting));
}
}
public ProfilePageViewModel()
{
User user = new User();
var username = LoginPage.Username;
var _user = user.GetUser(username);
Greeting = TimeBasedGreeting() + ", {username}!";
}
因此我需要访问它,但它是受保护的 属性。 有没有一种方法可以继承 LoginPage 并因此继承其受保护的属性?
最简单的做法就是将用户对象传递到新页面。然后 ProfilePage
将引用用户对象并可以从那里获取用户名
if (username == _user.Username && password == _user.Password)
{
await DisplayAlert("Log in successful", null, "OK");
await Navigation.PushAsync(new ProfilePage(_user));
}
或者,您可以在 App
class 上创建一个 User
属性,这将允许您应用中的任何页面访问它