■ 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 62 63 |
<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}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="50" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Spacing="10"> <CommandBar Name="commandBar" IsOpen="False" DefaultLabelPosition="Right"> <AppBarButton Name="addButton" Icon="Add" Label="추가" Click="appBarButton_Click" /> <AppBarButton Name="editButton" Icon="Edit" Label="편집" Click="appBarButton_Click" /> <AppBarButton Name="shareButton" Icon="Share" Label="공유" Click="appBarButton_Click" /> <CommandBar.SecondaryCommands> <AppBarButton Name="settingsButton" Icon="Setting" Label="설정" Click="appBarButton_Click"> <AppBarButton.KeyboardAccelerators> <KeyboardAccelerator Modifiers="Control" Key="I" /> </AppBarButton.KeyboardAccelerators> </AppBarButton> </CommandBar.SecondaryCommands> </CommandBar> <TextBlock Name="textBlock" Margin="0 10 0 0" /> </StackPanel> <StackPanel Grid.Column="2" Spacing="10"> <TextBlock Text="표시/닫기" /> <Button Content="보조 명령바 표시" Click="openButton_Click" /> <Button Content="보조 명령바 닫기" Click="closeButton_Click" /> <TextBlock Text="컨텐트 수정" /> <Button Content="보조 명령 추가" Click="addSecondaryCommandButton_Click" /> <Button Content="보조 명령 제거" Click="removeSecondaryCommandButton_Click" /> </StackPanel> </Grid> </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 |
using Windows.Foundation; using Windows.Graphics.Display; using Windows.System; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); #region 윈도우 크기를 설정한다. double width = 800d; double height = 600d; double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi; ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi); ApplicationView.PreferredLaunchViewSize = windowSize; Window.Current.Activate(); ApplicationView.GetForCurrentView().TryResizeView(windowSize); #endregion #region 윈도우 제목을 설정한다. ApplicationView.GetForCurrentView().Title = "CommandBar 엘리먼트 사용하기"; #endregion this.addButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.A, Modifiers = VirtualKeyModifiers.Control } ); this.editButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.E, Modifiers = VirtualKeyModifiers.Control } ); this.shareButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.F4 } ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// 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) { AppBarButton button = sender as AppBarButton; this.textBlock.Text = $"클릭했습니다 : {button.Label}"; } #endregion #region 보조 명령바 표시 버튼 클릭시 처리하기 - openButton_Click(sender, e) /// <summary> /// 보조 명령바 표시 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openButton_Click(object sender, RoutedEventArgs e) { this.commandBar.IsOpen = true; this.commandBar.IsSticky = true; } #endregion #region 보조 명령바 닫기 버튼 클릭시 처리하기 - closeButton_Click(sender, e) /// <summary> /// 보조 명령바 닫기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeButton_Click(object sender, RoutedEventArgs e) { this.commandBar.IsOpen = false; this.commandBar.IsSticky = false; } #endregion #region 보조 명령 추가 버튼 클릭시 처리하기 - addSecondaryCommandButton_Click(sender, e) /// <summary> /// 보조 명령 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addSecondaryCommandButton_Click(object sender, RoutedEventArgs e) { if(this.commandBar.SecondaryCommands.Count == 1) { AppBarButton newButton = new AppBarButton { Icon = new SymbolIcon(Symbol.Add), Label = "버튼 1" }; newButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.N, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(newButton); newButton = new AppBarButton { Icon = new SymbolIcon(Symbol.Delete), Label = "버튼 2" }; this.commandBar.SecondaryCommands.Add(newButton); newButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Delete } ); this.commandBar.SecondaryCommands.Add(new AppBarSeparator()); newButton = new AppBarButton { Icon = new SymbolIcon(Symbol.FontDecrease), Label = "버튼 3" }; newButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Subtract, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(newButton); newButton = new AppBarButton { Icon = new SymbolIcon(Symbol.FontIncrease), Label = "버튼 4" }; newButton.KeyboardAccelerators.Add ( new KeyboardAccelerator() { Key = VirtualKey.Add, Modifiers = VirtualKeyModifiers.Control } ); this.commandBar.SecondaryCommands.Add(newButton); } } #endregion #region 보조 명령 제거 버튼 클릭시 처리하기 - removeSecondaryCommandButton_Click(sender, e) /// <summary> /// 보조 명령 제거 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void removeSecondaryCommandButton_Click(object sender, RoutedEventArgs e) { RemoveSecondaryCommand(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 보조 명령 제거하기 - RemoveSecondaryCommand() /// <summary> /// 보조 명령 제거하기 /// </summary> private void RemoveSecondaryCommand() { while(this.commandBar.SecondaryCommands.Count > 1) { this.commandBar.SecondaryCommands.RemoveAt(this.commandBar.SecondaryCommands.Count - 1); } } #endregion } } |