■ TabView 엘리먼트의 KeyboardAccelerators 속성을 사용해 키보드 가속기를 사용하는 방법을 보여준다.
▶ SamplePage1.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 |
<Page x:Class="TestProject.SamplePage1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ScrollViewer> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.Resources> <x:Double x:Key="TileHeightKey">150</x:Double> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid Grid.Row="1" Grid.Column="1" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="DarkGray" /> <Grid Grid.Row="1" Grid.Column="2" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="LightGray" /> <Grid Grid.Row="2" Grid.Column="1" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="LightGray" /> <Grid Grid.Row="2" Grid.Column="2" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="DarkGray" /> <Grid Name="sourceGrid" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Margin="5" MinWidth="250" MinHeight="{StaticResource TileHeightKey}" Background="{ThemeResource SystemAccentColor}" /> <Grid Grid.Row="3" Grid.ColumnSpan="3" Margin="5 10"> <TextBlock TextWrapping="WrapWholeWords" Style="{ThemeResource BodyTextBlockStyle}"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </TextBlock> </Grid> </Grid> </ScrollViewer> </Page> |
▶ SamplePage1.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 |
using Windows.Foundation.Metadata; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 1 /// </summary> public sealed partial class SamplePage1 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage1() /// <summary> /// 생성자 /// </summary> public SamplePage1() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 연결 애니메이션 준비하기 - PrepareConnectedAnimation(configuration) /// <summary> /// 연결 애니메이션 준비하기 /// </summary> /// <param name="configuration">연결 애니메이션 구성</param> public void PrepareConnectedAnimation(ConnectedAnimationConfiguration configuration) { ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("ForwardConnectedAnimation", this.sourceGrid); if(configuration != null && ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7)) { animation.Configuration = configuration; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().GetAnimation("BackwardAnimation"); if(animation != null) { animation.TryStart(this.sourceGrid); } } #endregion } } |
▶ SamplePage2.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 |
<Page x:Class="TestProject.SamplePage2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ScrollViewer> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.Resources> <x:Double x:Key="TileHeightKey">150</x:Double> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid Name="targetGrid" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Margin="10" Width="150" MinHeight="{StaticResource TileHeightKey}" Height="200" Background="{ThemeResource SystemAccentColor}" /> <StackPanel Name="contentStackPanel" Grid.Row="1" Grid.Column="1" Margin="10" MinHeight="200"> <TextBlock Style="{ThemeResource TitleTextBlockStyle}" Margin="0 0 0 10" TextWrapping="WrapWholeWords" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit" /> <TextBlock TextWrapping="WrapWholeWords"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </TextBlock> </StackPanel> </Grid> </ScrollViewer> </Page> |
▶ SamplePage2.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 |
using Windows.Foundation.Metadata; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 2 /// </summary> public sealed partial class SamplePage2 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage2() /// <summary> /// 생성자 /// </summary> public SamplePage2() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 연결 애니메이션 준비하기 - PrepareConnectedAnimation(configuration) /// <summary> /// 연결 애니메이션 준비하기 /// </summary> /// <param name="configuration">연결 애니메이션 구성</param> public void PrepareConnectedAnimation(ConnectedAnimationConfiguration configuration) { ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("BackwardAnimation", this.targetGrid); if(configuration != null && ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7)) { animation.Configuration = configuration; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색 되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색 되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().GetAnimation("ForwardAnimation"); if(animation != null) { SetContentStackPanelTransitionCollection(); animation.TryStart(this.targetGrid); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 컨텐트 스택 패널 전이 컬렉션 설정하기 - SetContentStackPanelTransitionCollection() /// <summary> /// 컨텐트 스택 패널 전이 컬렉션 설정하기 /// </summary> private void SetContentStackPanelTransitionCollection() { this.contentStackPanel.Transitions = new TransitionCollection { new EntranceThemeTransition() }; } #endregion } } |
▶ SamplePage3.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 |
<Page x:Class="TestProject.SamplePage3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ScrollViewer> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.Resources> <x:Double x:Key="TileHeightKey">150</x:Double> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="LightGray" /> <Grid Grid.Row="1" Grid.Column="1" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="DarkGray" /> <Grid Grid.Row="2" Grid.Column="1" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="Gray" /> <Grid Grid.Row="1" Grid.Column="2" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="LightGray" /> <Grid Grid.Row="2" Grid.Column="2" Margin="5" MinHeight="{StaticResource TileHeightKey}" Background="DarkGray" /> <Grid Grid.Row="3" Grid.ColumnSpan="3" Margin="5"> <TextBlock TextWrapping="WrapWholeWords"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </TextBlock> </Grid> </Grid> </ScrollViewer> </Page> |
▶ SamplePage3.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 |
using Windows.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 샘플 페이지 3 /// </summary> public sealed partial class SamplePage3 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage3() /// <summary> /// 생성자 /// </summary> public SamplePage3() { InitializeComponent(); } #endregion } } |
▶ 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 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel Spacing="10"> <TextBlock Margin="10 0 0 0" TextWrapping="WrapWholeWords" Text="- Ctrl+T 키 : 새로운 탭을 엽니다." /> <TextBlock Margin="10 0 0 0" TextWrapping="WrapWholeWords" Text="- Ctrl+W 키 : 선택 탭을 닫습니다." /> <TextBlock Margin="10 0 0 0" TextWrapping="WrapWholeWords" Text="- Ctrl+1 ~ Ctrl+8 키 : 특정 번호 탭을 선택합니다." /> <TextBlock Margin="10 0 0 0" TextWrapping="WrapWholeWords" Text="- Ctrl+9 키 : 마지막 탭을 선택합니다(탭 번호에 관련없이)." /> <muxc:TabView Name="tabView" SelectedIndex="0" Loaded="tabView_Loaded" AddTabButtonClick="tabView_AddTabButtonClick" TabCloseRequested="tabView_TabCloseRequested"> <muxc:TabView.KeyboardAccelerators> <KeyboardAccelerator Key="T" Modifiers="Control" Invoked="newTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="W" Modifiers="Control" Invoked="closeSelectedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number1" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number2" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number3" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number4" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number5" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number6" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number7" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number8" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> <KeyboardAccelerator Key="Number9" Modifiers="Control" Invoked="navigateToNumberedTabKeyboardAccelerator_Invoked" /> </muxc:TabView.KeyboardAccelerators> </muxc:TabView> </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 |
using Microsoft.UI.Xaml.Controls; 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 = "TabView 엘리먼트 : KeyboardAccelerators 속성을 사용해 키보드 가속기 사용하기"; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 탭뷰 로드시 처리하기 - tabView_Loaded(sender, e) /// <summary> /// 탭뷰 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tabView_Loaded(object sender, RoutedEventArgs e) { TabView tabView = sender as TabView; for(int i = 0; i < 3; i++) { TabViewItem item = GetTabViewItem(i); tabView.TabItems.Add(item); } } #endregion #region 탭뷰 탭 추가 버튼 클릭시 처리하기 - tabView_AddTabButtonClick(tabView, _) /// <summary> /// 탭뷰 탭 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="tabView">탭뷰</param> /// <param name="_">이벤트 인자</param> private void tabView_AddTabButtonClick(TabView tabView, object _) { int index = tabView.TabItems.Count; TabViewItem item = GetTabViewItem(index); tabView.TabItems.Add(item); } #endregion #region 탭뷰 탭 닫기 요청시 처리하기 - tabView_TabCloseRequested(tabView, e) /// <summary> /// 탭뷰 탭 닫기 요청시 처리하기 /// </summary> /// <param name="tabView">탭뷰</param> /// <param name="e">이벤트 인자</param> private void tabView_TabCloseRequested(TabView tabView, TabViewTabCloseRequestedEventArgs e) { tabView.TabItems.Remove(e.Tab); } #endregion #region 신규 탭 키보드 가속기 호출시 처리하기 - newTabKeyboardAccelerator_Invoked(sender, e) /// <summary> /// 신규 탭 키보드 가속기 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void newTabKeyboardAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e) { TabView tabView = e.Element as TabView; TabViewItem item = GetTabViewItem(tabView.TabItems.Count); tabView.TabItems.Add(item); e.Handled = true; } #endregion #region 선택 탭 닫기 키보드 가속기 호출시 처리하기 - closeSelectedTabKeyboardAccelerator_Invoked(sender, e) /// <summary> /// 선택 탭 닫기 키보드 가속기 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeSelectedTabKeyboardAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e) { TabView tabView = e.Element as TabView; TabViewItem item = tabView.SelectedItem as TabViewItem; if(item.IsClosable) { tabView.TabItems.Remove(item); } e.Handled = true; } #endregion #region 번호 탭 탐색 키보드 가속기 호출시 처리하기 - navigateToNumberedTabKeyboardAccelerator_Invoked(sender, e) /// <summary> /// 번호 탭 탐색 키보드 가속기 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void navigateToNumberedTabKeyboardAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e) { TabView tabView = e.Element as TabView; int index = 0; switch(sender.Key) { case VirtualKey.Number1 : index = 0; break; case VirtualKey.Number2 : index = 1; break; case VirtualKey.Number3 : index = 2; break; case VirtualKey.Number4 : index = 3; break; case VirtualKey.Number5 : index = 4; break; case VirtualKey.Number6 : index = 5; break; case VirtualKey.Number7 : index = 6; break; case VirtualKey.Number8 : index = 7; break; case VirtualKey.Number9 : index = tabView.TabItems.Count - 1; break; } if(index < tabView.TabItems.Count) { tabView.SelectedIndex = index; } e.Handled = true; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 탭뷰 항목 구하기 - GetTabViewItem(index) /// <summary> /// 탭뷰 항목 구하기 /// </summary> /// <param name="index">인덱스</param> /// <returns>탭뷰 항목</returns> private TabViewItem GetTabViewItem(int index) { TabViewItem item = new TabViewItem { Header = $"문서 {index}", IconSource = new Microsoft.UI.Xaml.Controls.SymbolIconSource() { Symbol = Symbol.Document } }; Frame frame = new Frame(); switch(index % 3) { case 0 : frame.Navigate(typeof(SamplePage1)); break; case 1 : frame.Navigate(typeof(SamplePage2)); break; case 2 : frame.Navigate(typeof(SamplePage3)); break; } item.Content = frame; return item; } #endregion } } |