当我不在MainPage之外的特定页面上时,是否可以通过编程方式访问现有的AppBarButton
并禁用它?
<CommandBar Grid.Row="0">
<CommandBar.Content>
<Button
Style="{StaticResource NavigationBackButtonNormalStyle}"
Name="BackButton"
VerticalAlignment="Top"
Click="Back_Click"/>
</CommandBar.Content>
<AppBarButton Icon="Mail" Name="ContactUs"/>
</CommandBar>
更新
主页
public sealed partial class MainPage : Page
{
public static AppBarButton MyAppBarButton;
public MainPage()
{
this.InitializeComponent();
Current = this;
Frame_Main.Navigate(typeof(Frame1));
MyAppBarButton = AppBarButtonSettings;
}
public static MainPage Current;
private void Back_Click(object sender, RoutedEventArgs e)
{
On_BackRequested();
}
private bool On_BackRequested()
{
if (Frame_Main.CanGoBack)
{
Frame_Main.GoBack();
return true;
}
return false;
}
private void BackInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
On_BackRequested();
args.Handled = true;
}
private void AppBarButtonContactUs_Click(object sender, RoutedEventArgs e)
{
Frame_Main.Navigate(typeof(ContactUs));
}
}
与我们联系页面
public sealed partial class ContactUs: Page
{
public ContactUs()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MainPage.Current.BackButton.Visibility = Visibility.Visible;
MainPage.Current.BackButton.IsEnabled = true;
MainPage.MyAppBarButton.IsEnabled = false;
}
}
当然,从您的代码派生,您可以使用后面代码中的名称访问AppBarButton
。例如:
private void Btn_Click(object sender, RoutedEventArgs e)
{
ContactUs.IsEnabled = false;
}
更新
您可以使静态AppBarButton
记录ContactUs
并使用页面类名称进行访问。
public static AppBarButton MyAppBarButton;
public TestPage()
{
this.InitializeComponent();
MyAppBarButton = ContactUs;
}
用法
TestPage.MyAppBarButton.IsEnabled = false;