2016-02-06 89 views
1

我有一个应用程序,检查用户是否是管理员,如果不是,我想隐藏一些命令,如AppBarButton和menuflyout。当用户不是管理员时,如何在后面的代码中使用visibility.collapsed隐藏这些命令?使用可见性隐藏AppBarButton折叠

private async void Button_OK(object sender, TappedRoutedEventArgs e) 
{ 
    Utilizador u = new Utilizador(null, TextBox1.Text, PasswordBox1.Password, null, null, false); 
    if (u.GetByLoginAndPassword()) 
    { 
     if (UtilizadorViewModel.Utilizador.Admin == false) 
     { 
      this.Frame.Navigate(typeof(BlankPage1), UtilizadorViewModel.Utilizador.Nome); 
      //Visibility.Collapsed; 
      MessageDialog a = new MessageDialog("Bem Vindo! ", u.Nome); 
      await a.ShowAsync(); 
     } else 
     { 
      this.Frame.Navigate(typeof(BlankPage1), UtilizadorViewModel.Utilizador.Nome); 
      //Visibility.Visible; 
      MessageDialog a = new MessageDialog("Bem Vindo! ", u.Nome); 
      await a.ShowAsync(); 
     } 
    } 
    else 
    { 
     MessageDialog md = new MessageDialog("Nome ou password errados"); 
     await md.ShowAsync(); 
    } 

<Page.BottomAppBar> 
     <CommandBar> 
      <AppBarButton x:Name="addp" Label="Add" Tapped="AppBarButton_Tapped" Icon="Add"/> 
     </CommandBar> 
    </Page.BottomAppBar> 

回答

0

您可以隐藏整个应用程序栏或设置它的能见度倒塌:

  • ApplicationBar.IsVisible = false;
  • ApplicationBar.Visibility = Visibility.Collapsed;

如果您需要访问单个按钮,你可以使用2方法 你可以完全删除按钮:

  • ApplicationBar.Buttons.RemoveAt(0);

或使用该按钮的指数,并设置其属性IsEnabled

  • ((ApplicationBarIconButton)ApplicationBar.Buttons[buttonIndex]).IsEnabled = false;

If you need additional information - this answer explains it very well.

Check out this one too.