从 Xamarin Forms 中的另一个页面访问特定的 Shell FlyoutItem
Access a specific Shell FlyoutItem from another page in Xamarin Forms
在我的 AppShell.xaml.cs 页面中,我可以轻松访问 Shell:
的 FlyoutItems
accountFlyoutItem.IsEnabled = false;
accountFlyoutItem.IsVisible = false;
但是,如何从另一个页面访问这些内容?我发现的唯一方法是尝试遍历“Shell.Current.FlyoutItems”。我缺少更简单的方法吗?
Shell.Current.CurrentItem
用于获取当前选中的弹出窗口。
如果我们想在xaml(<FlyoutItem x:Name="a">
)中命名后访问特定的FlyoutItem
,我们可以通过以下两种方式获取
- 在
AppShell
中创建一个全局public变量并在构造函数中赋值。
public ShellItem AItem;
public AppShell()
{
InitializeComponent();
RegisterRoutes();
BindingContext = this;
AItem = a;
}
//In another page
var item = (Shell.Current as AppShell).AItem;
- 为 return 项目创建一个方法,这样我们就不需要创建 public 变量。
public ShellItem GetA()
{
return a;
}
//In another page
var item = (Shell.Current as AppShell).GetA();
在我的 AppShell.xaml.cs 页面中,我可以轻松访问 Shell:
的 FlyoutItemsaccountFlyoutItem.IsEnabled = false;
accountFlyoutItem.IsVisible = false;
但是,如何从另一个页面访问这些内容?我发现的唯一方法是尝试遍历“Shell.Current.FlyoutItems”。我缺少更简单的方法吗?
Shell.Current.CurrentItem
用于获取当前选中的弹出窗口。
如果我们想在xaml(<FlyoutItem x:Name="a">
)中命名后访问特定的FlyoutItem
,我们可以通过以下两种方式获取
- 在
AppShell
中创建一个全局public变量并在构造函数中赋值。
public ShellItem AItem;
public AppShell()
{
InitializeComponent();
RegisterRoutes();
BindingContext = this;
AItem = a;
}
//In another page
var item = (Shell.Current as AppShell).AItem;
- 为 return 项目创建一个方法,这样我们就不需要创建 public 变量。
public ShellItem GetA()
{
return a;
}
//In another page
var item = (Shell.Current as AppShell).GetA();