代码之家  ›  专栏  ›  技术社区  ›  wbk727

如何访问现有AppBarButton并将其禁用

  •  0
  • wbk727  · 技术社区  · 4 年前

    有没有办法访问现有数据库 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;
        }
    }
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   wbk727    4 年前

    当然,从您的代码派生,您可以访问 AppBarButton 名字在代码后面。例如:

    private void Btn_Click(object sender, RoutedEventArgs e)
    {
        ContactUs.IsEnabled = false;
    }
    

    使现代化

    你可以制造静电 附件按钮 记录 ContactUs 并使用页面类名访问。

    public static AppBarButton MyAppBarButton;
    
    public TestPage()
    {
        this.InitializeComponent();
        MyAppBarButton = ContactUs;
    }
    

    用法

    TestPage.MyAppBarButton.IsEnabled = false;