[英]How can i change content after login success in xamarin.forms using c#
I have a page flyview and a login login.I want if I login successfully, it will change label in my username.我有一个页面 flyview 和一个登录登录。我想如果我登录成功,它会在我的用户名中更改 label。
mainpage.xaml :主页.xaml :
<Shell.FlyoutHeader>
<local:HeaderContentView></local:HeaderContentView>
</Shell.FlyoutHeader>
HeaderContentView.xaml : HeaderContentView.xaml :
<Grid BackgroundColor="#326e52"
HeightRequest="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackLayout Padding="10,0,10,20"
VerticalOptions="EndAndExpand" Orientation="Horizontal" Grid.Column="0">
<Label Text="Đăng nhập"
TextColor="White"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Link_dang_nhap"/>
</Label.GestureRecognizers>
</Label>
<Label Text="Đăng ký"
TextColor="White"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Link_dang_ky"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</Grid>
I want if I login successfully it will change label text="Đăng nhập" and "Đăng ký" to my username我想如果我成功登录,它会将 label text="Đăng nhập" 和 "Đăng ký" 更改为我的用户名
You can create a View Model
and bind the AppShell to change the label text to your username.您可以创建一个
View Model
并绑定 AppShell 以将 label 文本更改为您的用户名。 When logining, publish a message by MessagingCenter and change the label text.登录时,通过MessagingCenter发布消息,修改label文本。 You could also set a static member or use a static Properties dictionary to save the login status.
您还可以设置一个 static 成员或使用static 属性字典来保存登录状态。
App.cs:应用程序:
public partial class App : Application
{
public static Boolean IsLogin { set; get; } = false; // set a static member
public App()
{
......
// Application.Current.Properties["IsLogin"] = false; // use static Properties dictionary
......
}
}
Xaml: Xaml:
<Shell.BindingContext>
<vm:ShellViewModel></vm:ShellViewModel>
</Shell.BindingContext>
The username binding with Label:与Label绑定的用户名:
<Label Text="{Binding UserName}"
TextColor="White"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Link_dang_nhap"/>
</Label.GestureRecognizers>
</Label>
ViewModel:视图模型:
public class ShellViewModel: BaseViewModel// the BaseViewMode calls INotifyPropertyChanged method
{
string _username = string.Empty;
public string UserName
{
get { return _username ; }
set { SetProperty(ref _username, value); }
}
}
Push message when logging in successfully:登录成功时推送消息:
private async void OnLoginClicked(object obj)
{
......
MessagingCenter.Send<Object, string>(this, "UserName", "Setusername");// You could set the user name
App.IsLogin = true;
// Application.Current.Properties["IsLogin"] = true;
}
Subscribe this message in the constructor of ShellViewModel
:在
ShellViewModel
的构造函数中订阅此消息:
public ShellViewModel()
{
MessagingCenter.Subscribe<Object, string>(this, "UserName", (sender, s) =>
{
this.UserName = s;
});
UserName= App.IsLogin ? "Đăng nhập" : "testname";// test
}
Please pay attention to unsubscribing the message: https://docs.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/messaging-center#unsubscribe-from-a-message退订消息请注意: https://docs.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/messaging-center#unsubscribe-from-a-message
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.