■ XamlUICommand 클래스의 ExecuteRequested 이벤트를 사용해 명령 실행 호출시 처리하는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ Item.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 |
using System.Windows.Input; namespace TestProject; /// <summary> /// 항목 /// </summary> public class Item { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get; set; } #endregion #region 명령 - Command /// <summary> /// 명령 /// </summary> public ICommand Command { get; set; } #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 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 |
<?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" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page.Resources> <XamlUICommand x:Name="deleteCommand" Label="Delete" Description="Delete item"> <XamlUICommand.IconSource> <FontIconSource FontFamily="Wingdings" Glyph="M" /> </XamlUICommand.IconSource> <XamlUICommand.KeyboardAccelerators> <KeyboardAccelerator Modifiers="Control" Key="D" /> </XamlUICommand.KeyboardAccelerators> </XamlUICommand> <Style x:Key="ListViewItemStyleKey" TargetType="ListViewItem" BasedOn="{StaticResource ListViewItemRevealStyle}"> <Setter Property="Height" Value="60" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Padding" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> </Style> </Page.Resources> <Grid Name="grid"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <muxc:MenuBar Grid.Row="0" Padding="10"> <muxc:MenuBarItem Title="File"> </muxc:MenuBarItem> <muxc:MenuBarItem Title="Edit"> <MenuFlyoutItem Command="{StaticResource deleteCommand}" /> </muxc:MenuBarItem> <muxc:MenuBarItem Title="Help"> </muxc:MenuBarItem> </muxc:MenuBar> <ListView Name="listView" Grid.Row="1" ItemContainerStyle="{StaticResource ListViewItemStyleKey}" SelectionMode="Single" IsItemClickEnabled="True"> <ListView.ItemTemplate> <DataTemplate x:DataType="local:Item"> <UserControl PointerEntered="ItemUserControl_PointerEntered" PointerExited="ItemUserControl_PointerExited"> <UserControl.ContextFlyout> <MenuFlyout> <MenuFlyoutItem Command="{x:Bind Command}" CommandParameter="{x:Bind Text}" /> </MenuFlyout> </UserControl.ContextFlyout> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState x:Name="AppBarButtonHidden" /> <VisualState x:Name="AppBarButtonShown"> <VisualState.Setters> <Setter Target="appBarButton.Visibility" Value="Visible" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <SwipeControl> <SwipeControl.RightItems> <SwipeItems Mode="Execute"> <SwipeItem Background="Red" Command="{x:Bind Command}" CommandParameter="{x:Bind Text}"/> </SwipeItems> </SwipeControl.RightItems> <Grid VerticalAlignment="Center"> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10" FontSize="18" Text="{x:Bind Text}" /> <AppBarButton Name="appBarButton" HorizontalAlignment="Right" IsTabStop="False" Visibility="Collapsed" Command="{x:Bind Command}" CommandParameter="{x:Bind Text}"/> </Grid> </SwipeControl> </Grid> </UserControl> </DataTemplate> </ListView.ItemTemplate> </ListView> </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 |
using System.Collections.ObjectModel; 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 MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컬렉션 /// </summary> private ObservableCollection<Item> collection = new ObservableCollection<Item>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.deleteCommand.ExecuteRequested += deleteCommand_ExecuteRequested; this.grid.Loaded += grid_Loaded; this.listView.Loaded += listView_Loaded; this.listView.SelectionChanged += listView_SelectionChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 그리드 로드시 처리하기 - grid_Loaded(sender, e) /// <summary> /// 그리드 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void grid_Loaded(object sender, RoutedEventArgs e) { for(int i = 0; i < 5; i++) { collection.Add(new Item { Text = "항목 " + i.ToString(), Command = deleteCommand }); } } #endregion #region 리스트 뷰 로드시 처리하기 - listView_Loaded(sender, e) /// <summary> /// 리스트 뷰 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listView_Loaded(object sender, RoutedEventArgs e) { ListView listView = sender as ListView; listView.ItemsSource = this.collection; } #endregion #region 리스트 뷰 선택 변경시 처리하기 - listView_SelectionChanged(sender, e) /// <summary> /// 리스트 뷰 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.listView.SelectedIndex != -1) { Item item = collection[this.listView.SelectedIndex]; } } #endregion #region 항목 사용자 컨트롤 포인터 진입시 처리하기 - ItemUserControl_PointerEntered(sender, e) /// <summary> /// 항목 사용자 컨트롤 포인터 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ItemUserControl_PointerEntered(object sender, PointerRoutedEventArgs e) { if(e.Pointer.PointerDeviceType == PointerDeviceType.Mouse || e.Pointer.PointerDeviceType == PointerDeviceType.Pen) { VisualStateManager.GoToState(sender as Control, "AppBarButtonShown", true); } } #endregion #region 항목 사용자 컨트롤 포인터 이탈시 처리하기 - ItemUserControl_PointerExited(sender, e) /// <summary> /// 항목 사용자 컨트롤 포인터 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ItemUserControl_PointerExited(object sender, PointerRoutedEventArgs e) { VisualStateManager.GoToState(sender as Control, "AppBarButtonHidden", true); } #endregion #region 삭제 명령 실행 요청시 처리하기 - deleteCommand_ExecuteRequested(sender, e) /// <summary> /// 삭제 명령 실행 요청시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void deleteCommand_ExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs e) { if(e.Parameter != null) { foreach(Item item in this.collection) { if(item.Text == (e.Parameter as string)) { this.collection.Remove(item); return; } } } if(this.listView.SelectedIndex != -1) { this.collection.RemoveAt(this.listView.SelectedIndex); } } #endregion } |