[C#/WINUI3/.NET6] CommandBar 엘리먼트 사용하기
■ CommandBar 엘리먼트를 사용하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0"> <CommandBar Name="commandBar" DefaultLabelPosition="Right" IsOpen="False"> <AppBarButton Name="addAppBarButton" Icon="Add" Label="Add" /> <AppBarButton Name="editAppBarButton" Icon="Edit" Label="Edit" /> <AppBarButton Name="shareAppBarButton" Icon="Share" Label="Share" /> <CommandBar.SecondaryCommands> <AppBarButton Name="settingsAppBarButton" Icon="Setting" Label="Settings"> <AppBarButton.KeyboardAccelerators> <KeyboardAccelerator Modifiers="Control" Key="I" /> </AppBarButton.KeyboardAccelerators> </AppBarButton> </CommandBar.SecondaryCommands> </CommandBar> <TextBlock Name="textBlock" Padding="0 10 0 0" /> </StackPanel> <StackPanel Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="30 0 0 0"> <TextBlock Text="Show or hide" /> <Button Name="openCommandBarButton" Margin="0 10 0 0" Content="Open command bar" /> <Button Name="closeCommandBarButton" Margin="0 10 0 0" Content="Close command bar" /> <TextBlock Margin="0 15 0 0" Text="Modify content" /> <Button Name="addSecondaryCommandsButton" Margin="0 10 0 0" Content="Add secondary commands" /> <Button Name="removeSecondaryCommandsButton" Margin="0 10 0 0" Content="Remove secondary commands" /> </StackPanel> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
using Windows.System; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.addAppBarButton.Click += appBarButton_Click; this.editAppBarButton.Click += appBarButton_Click; this.shareAppBarButton.Click += appBarButton_Click; this.settingsAppBarButton.Click += appBarButton_Click; this.openCommandBarButton.Click += openCommandBarButton_Click; this.closeCommandBarButton.Click += closeCommandBarButton_Click; this.addSecondaryCommandsButton.Click += addSecondaryCommandsButton_Click; this.removeSecondaryCommandsButton.Click += removeSecondaryCommandsButton_Click; AddKeyboardAccelerators(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색을 종료할 경우 처리하기 - OnNavigatingFrom(e) /// <summary> /// 탐색을 종료할 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { RemoveSecondaryCommands(); base.OnNavigatingFrom(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 앱바 버튼 클릭시 처리하기 - appBarButton_Click(sender, e) /// <summary> /// 앱바 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void appBarButton_Click(object sender, RoutedEventArgs e) { this.textBlock.Text = "You clicked : " + (sender as AppBarButton).Label; } #endregion #region Open command bar 버튼 클릭시 처리하기 - openCommandBarButton_Click(sender, e) /// <summary> /// Open command bar 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openCommandBarButton_Click(object sender, RoutedEventArgs e) { this.commandBar.IsOpen = true; this.commandBar.IsSticky = true; } #endregion #region Close command bar 버튼 클릭시 처리하기 - closeCommandBarButton_Click(sender, e) /// <summary> /// Close command bar 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeCommandBarButton_Click(object sender, RoutedEventArgs e) { this.commandBar.IsOpen = false; this.commandBar.IsSticky = false; } #endregion #region Add secondary commands 버튼 클릭시 처리하기 - addSecondaryCommandsButton_Click(sender, e) /// <summary> /// Add secondary commands 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addSecondaryCommandsButton_Click(object sender, RoutedEventArgs e) { if(this.commandBar.SecondaryCommands.Count == 1) { AppBarButton appBarButton = new AppBarButton(); appBarButton.Icon = new SymbolIcon(Symbol.Add); appBarButton.Label = "Button 1"; appBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.N, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(appBarButton); appBarButton = new AppBarButton { Icon = new SymbolIcon(Symbol.Delete), Label = "Button 2" }; this.commandBar.SecondaryCommands.Add(appBarButton); appBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Delete } ); this.commandBar.SecondaryCommands.Add(new AppBarSeparator()); appBarButton = new AppBarButton(); appBarButton.Icon = new SymbolIcon(Symbol.FontDecrease); appBarButton.Label = "Button 3"; appBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Subtract, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(appBarButton); appBarButton = new AppBarButton(); appBarButton.Icon = new SymbolIcon(Symbol.FontIncrease); appBarButton.Label = "Button 4"; appBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Add, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(appBarButton); } } #endregion #region Remove secondary commands 버튼 클릭시 처리하기 - removeSecondaryCommandsButton_Click(sender, e) /// <summary> /// Remove secondary commands 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void removeSecondaryCommandsButton_Click(object sender, RoutedEventArgs e) { RemoveSecondaryCommands(); } #endregion #region 키보드 가속기 추가하기 - AddKeyboardAccelerators() /// <summary> /// 키보드 가속기 추가하기 /// </summary> private void AddKeyboardAccelerators() { this.editAppBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.E, Modifiers = VirtualKeyModifiers.Control } ); this.shareAppBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.F4 } ); this.addAppBarButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.A, Modifiers = VirtualKeyModifiers.Control } ); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 보조 명령 제거하기 - RemoveSecondaryCommands() /// <summary> /// 보조 명령 제거하기 /// </summary> private void RemoveSecondaryCommands() { while(this.commandBar.SecondaryCommands.Count > 1) { this.commandBar.SecondaryCommands.RemoveAt(this.commandBar.SecondaryCommands.Count - 1); } } #endregion } } |
TestProject.zip