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

问题描述 投票:0回答:1

当我不在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;
    }
}
windows uwp uwp-xaml
1个回答
0
投票

当然,从您的代码派生,您可以使用后面代码中的名称访问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;
© www.soinside.com 2019 - 2024. All rights reserved.