■ 모든 입력 형식에 대한 컬렉션 명령을 만드는 방법을 보여준다.
※ ICommand 인터페이스, UserControl 클래스 ContextFlyout 속성, SwipeItem 클래스 Invoke 이벤트, KeyboardAccelerator 클래스 Invoke 이벤트를 사용한다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ FavoriteCommand.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 |
using System; using System.Windows.Input; namespace TestProject; /// <summary> /// 즐겨찾기 명령 /// </summary> public class FavoriteCommand: ICommand { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 변경시 - CanExecuteChanged /// <summary> /// 실행 가능 여부 변경시 /// </summary> public event EventHandler CanExecuteChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 구하기 - CanExecute(parameter) /// <summary> /// 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>실행 가능 여부</returns> public bool CanExecute(object parameter) { return true; } #endregion #region 실행하기 - Execute(parameter) /// <summary> /// 실행하기 /// </summary> /// <param name="parameter">매개 변수</param> public void Execute(object parameter) { Podcast podcast = parameter as Podcast; podcast.IsFavorite = !podcast.IsFavorite; } #endregion } |
▶ Podcast.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 |
using System.ComponentModel; namespace TestProject; /// <summary> /// 팟캐스트 /// </summary> public class Podcast : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 즐겨찾기 여부 /// </summary> private bool isFavorite = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get; set; } #endregion #region 설명 - Description /// <summary> /// 설명 /// </summary> public string Description { get; set; } #endregion #region 즐겨찾기 여부 - IsFavorite /// <summary> /// 즐겨찾기 여부 /// </summary> public bool IsFavorite { get { return this.isFavorite; } set { this.isFavorite = value; FirePropertyChangedEvent("IsFavorite"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> private void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } |
▶ PodcastControl.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 64 65 66 67 68 69 |
<UserControl x:Class="TestProject.PodcastControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" IsTabStop="True" UseSystemFocusVisuals="True"> <UserControl.Resources> <SymbolIconSource x:Key="FavoriteSymbolIconSourceKey" Symbol="Favorite" /> <SwipeItems x:Key="RightSwipeItemsKey" Mode="Reveal"> <SwipeItem Background="Yellow" IconSource="{StaticResource FavoriteSymbolIconSourceKey}" Text="Favorite" Invoked="swipeItem_Invoked" /> </SwipeItems> </UserControl.Resources> <UserControl.ContextFlyout> <MenuFlyout> <MenuFlyoutItem Text="Favorite" Command="{StaticResource FavoriteCommandKey}" CommandParameter="{x:Bind Podcast, Mode=OneWay}" /> </MenuFlyout> </UserControl.ContextFlyout> <SwipeControl RightItems="{StaticResource RightSwipeItemsKey}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="HoveringStates"> <VisualState x:Name="HoverButtonsShown"> <VisualState.Setters> <Setter Target="hoverAreaGrid.Visibility" Value="Visible" /> </VisualState.Setters> </VisualState> <VisualState x:Name="HoverButtonsHidden" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid Margin="10 0 10 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Margin="5" Spacing="10"> <TextBlock Style="{StaticResource TitleTextBlockStyle}" Text="{x:Bind Podcast.Title, Mode=OneWay}" /> <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="{x:Bind Podcast.Description, Mode=OneWay}" /> <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="{x:Bind Podcast.IsFavorite, Mode=OneWay}" /> </StackPanel> <Grid Name="hoverAreaGrid" Grid.Column="1" VerticalAlignment="Stretch" Visibility="Collapsed"> <AppBarButton VerticalAlignment="Center" IsTabStop="False" Icon="OutlineStar" Label="Favorite" Command="{StaticResource FavoriteCommandKey}" CommandParameter="{x:Bind Podcast, Mode=OneWay}" /> </Grid> </Grid> </SwipeControl> </UserControl> |
▶ PodcastControl.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 |
using System.Windows.Input; using Windows.System; using Microsoft.UI.Input; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; namespace TestProject; /// <summary> /// 팟캐스트 컨트롤 /// </summary> public sealed partial class PodcastControl : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 팟캐스트 속성 - PodcastProperty /// <summary> /// 팟캐스트 속성 /// </summary> public static readonly DependencyProperty PodcastProperty = DependencyProperty.Register ( "Podcast", typeof(Podcast), typeof(PodcastControl), new PropertyMetadata(null) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 팟캐스트 - Podcast /// <summary> /// 팟캐스트 /// </summary> public Podcast Podcast { get { return (Podcast)GetValue(PodcastProperty); } set { SetValue(PodcastProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PodcastControl() /// <summary> /// 생성자 /// </summary> public PodcastControl() { InitializeComponent(); KeyboardAccelerator favoriteKeyboardAccelerator1 = new KeyboardAccelerator { Key = VirtualKey.F }; KeyboardAccelerator favoriteKeyboardAccelerator2 = new KeyboardAccelerator { Key = VirtualKey.S, Modifiers = VirtualKeyModifiers.Control }; KeyboardAccelerators.Add(favoriteKeyboardAccelerator1); KeyboardAccelerators.Add(favoriteKeyboardAccelerator2); favoriteKeyboardAccelerator1.Invoked += favoriteKeyboardAccelerator_Invoked; favoriteKeyboardAccelerator2.Invoked += favoriteKeyboardAccelerator_Invoked; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 포인터 진입시 처리하기 - OnPointerEntered(e) /// <summary> /// 포인터 진입시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPointerEntered(PointerRoutedEventArgs e) { base.OnPointerEntered(e); if(e.Pointer.PointerDeviceType == PointerDeviceType.Mouse || e.Pointer.PointerDeviceType == PointerDeviceType.Pen) { VisualStateManager.GoToState(this, "HoverButtonsShown", true); } } #endregion #region 포인터 이탈시 처리하기 - OnPointerExited(e) /// <summary> /// 포인터 이탈시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPointerExited(PointerRoutedEventArgs e) { base.OnPointerExited(e); VisualStateManager.GoToState(this, "HoverButtonsHidden", true); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스와이프 항목 호출시 처리하기 - swipeItem_Invoked(sender, e) /// <summary> /// 스와이프 항목 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void swipeItem_Invoked(SwipeItem sender, SwipeItemInvokedEventArgs e) { ICommand favoriteCommand = Application.Current.Resources["FavoriteCommandKey"] as ICommand; favoriteCommand.Execute(Podcast); } #endregion #region 즐겨찾기 키보드 가속키 호출시 처리하기 - favoriteKeyboardAccelerator_Invoked(sender, e) /// <summary> /// 즐겨찾기 키보드 가속키 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void favoriteKeyboardAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e) { if(Application.Current.Resources.TryGetValue("FavoriteCommandKey", out object command) && command is ICommand favoriteCommand) { favoriteCommand.Execute(Podcast); } e.Handled = true; } #endregion } |
▶ App.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <Application x:Class="TestProject.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> </ResourceDictionary.MergedDictionaries> <local:FavoriteCommand x:Key="FavoriteCommandKey" /> </ResourceDictionary> </Application.Resources> </Application> |
▶ 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 |
<?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" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <ListView Margin="10" BorderThickness="1" BorderBrush="Black" ItemsSource="{x:Bind PodcastCollection}"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem" BasedOn="{StaticResource ListViewItemRevealStyle}"> <Setter Property="Padding" Value="0" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate x:DataType="local:Podcast"> <local:PodcastControl Podcast="{x:Bind Mode=OneWay}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </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 |
using System.Collections.ObjectModel; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 팟캐스트 컬렉션 - PodcastCollection /// <summary> /// 팟캐스트 컬렉션 /// </summary> public ObservableCollection<Podcast> PodcastCollection { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); PodcastCollection = [ new Podcast { Title = "팟캐스트 1", Description = "팟캐스트 1에 대한 설명문입니다.", IsFavorite = false}, new Podcast { Title = "팟캐스트 2", Description = "팟캐스트 2에 대한 설명문입니다.", IsFavorite = true }, new Podcast { Title = "팟캐스트 3", Description = "팟캐스트 3에 대한 설명문입니다.", IsFavorite = false}, new Podcast { Title = "팟캐스트 4", Description = "팟캐스트 4에 대한 설명문입니다.", IsFavorite = false} ]; } #endregion } |