[英]How to access existing AppBarButton and disable it
當我在 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;
}
更新
您可以制作 static AppBarButton
來記錄ContactUs
並使用頁面 class 名稱訪問。
public static AppBarButton MyAppBarButton;
public TestPage()
{
this.InitializeComponent();
MyAppBarButton = ContactUs;
}
用法
TestPage.MyAppBarButton.IsEnabled = false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.