c# - 根据应用身份验证更改母版页项目标签
问题描述
在我的 xamarin 表单应用程序中-当用户未登录时,我希望母版页上的按钮说“登录”。用户登录后,我希望同一个按钮说“注销”。
我使用 INotifyPropertyChanged 创建了一个 masterpageitem 类。我创建了一个 ObservableCollection masterPageItems。我有 listView.ItemsSource = App.masterPageItems;
//masterpageitem.cs
public class MasterPageItem : INotifyPropertyChanged
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
NotifyPropertyChanged("title");
}
}
public string IconSource { get; set; }
public Type TargetType { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
//App.xaml.cs
public static ObservableCollection<MasterPageItem> masterPageItems = new ObservableCollection<MasterPageItem>
{
new MasterPageItem
{
Title = "Home",
IconSource = "home.png",
TargetType = typeof(HomePage)
},
new MasterPageItem
{
Title = "Competitions",
IconSource = "logo.png",
TargetType = typeof(Views.PlayerTournaments)
},
new MasterPageItem
{
Title = App.authenticated?"Sign out":"Sign in",
IconSource = "logo.png",
TargetType = typeof(LoginPage)
}
};
//Masterpage.xaml.cs
public partial class MasterPage : ContentPage
{
public ListView ListView { get { return listView; } }
public MasterPage()
{
InitializeComponent();
listView.ItemsSource = App.masterPageItems;
}
//Masterpage.xaml
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding IconSource}" />
<Label Grid.Column="1" Text="{Binding Title}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//LoginPage.xaml.cs
void UpdateSignInState(UserContext userContext)
{
var isSignedIn = userContext.IsLoggedOn;
btnSignInSignOut.Text = isSignedIn ? "Sign out" : "Sign in";
App.authenticated = isSignedIn;
App.masterPageItems[2].Title = App.authenticated ? "Sign out" : "Sign in";
}
当我注销时 - 当我希望母版页项目说“登录”时,它仍然是“注销”
解决方案
推荐阅读
- r - 在 R 中循环数据帧的最佳方法
- python - 如何在python中的子数组上划分数组?
- python - 渲染存储在 Database Flask SQLAlchemy 中的 HTML
- r - 是否有像 MsAccess-display-two-columns-in-combo-box 这样的 R Shiny 组合框
- arangodb - 设计问题:哪个更好?许多数据库或许多集合?
- python - 在 Pycharm 中基于项目定义应用程序设置
- mysql - 如何从2个表中计算一个月内的最大值
- android - 没有安装 SIM 卡时,有没有办法在 Android 上为 VoIP 应用程序使用本机呼叫屏幕?
- javascript - 打字稿:有没有办法将泛型约束放在方法内的泛型类上?
- javascript - 如何修复移动设备上不可点击的 YT 播放按钮?