■ DataGrid 엘리먼트를 사용하는 방법을 보여준다.
▶ 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 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Page.Resources> <local:ByteToMegabyteConverter x:Key="ByteToMegabyteConverterKey" /> <local:CPUTimeConverter x:Key="CPUTimeConverterKey" /> </Page.Resources> <Grid> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <controls:DataGrid Name="dataGrid" Grid.Row="0" BorderBrush="Black" BorderThickness="1" AutoGenerateColumns="False"> <controls:DataGrid.Columns> <controls:DataGridTextColumn Header="명칭" Width="Auto" Binding="{Binding ExecutableFileName}" /> <controls:DataGridTextColumn Header="PID" Width="Auto" Binding="{Binding ProcessID}" /> <controls:DataGridTextColumn Header="커널 시간" Width="Auto" Binding="{Binding KernelTime, Converter={StaticResource CPUTimeConverterKey}}" /> <controls:DataGridTextColumn Header="사용자 시간" Width="Auto" Binding="{Binding UserTime, Converter={StaticResource CPUTimeConverterKey}}" /> <controls:DataGridTextColumn Header="페이지 파일" Width="Auto" Binding="{Binding PageFileSize, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="작업 세트" Width="Auto" Binding="{Binding WorkingSetSize, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="디스크 읽기" Width="Auto" Binding="{Binding ReadByteCount, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="디스크 쓰기" Width="Auto" Binding="{Binding WriteByteCount, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="타입" Width="Auto" Binding="{Binding AppType}" /> </controls:DataGrid.Columns> </controls:DataGrid> <Button Name="refreshButton" Grid.Row="2" HorizontalAlignment="Right" Width="100" Height="30" Content="갱신" /> </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 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using Windows.System; using Windows.System.Diagnostics; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디폴트 프로세스 이미지 /// </summary> private BitmapImage defaultProcessImage; /// <summary> /// 디폴트 앱 이미지 /// </summary> private BitmapImage defaultAppImage; /// <summary> /// 프로세스 컬렉션 /// </summary> private ObservableCollection<ProcessModel> processCollection = new ObservableCollection<ProcessModel>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.defaultProcessImage = new BitmapImage(new Uri("ms-appx:/Assets/default-process-icon.png", UriKind.Absolute)); this.defaultAppImage = new BitmapImage(new Uri("ms-appx:/Assets/default-app-icon.png" , UriKind.Absolute)); this.dataGrid.ItemsSource = this.processCollection; this.refreshButton.Click += refreshButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected async override void OnNavigatedTo(NavigationEventArgs e) { await AppDiagnosticInfo.RequestAccessAsync(); SetProcessData(); base.OnNavigatedTo(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 갱신 버튼 클릭시 처리하기 - refreshButton_Click(sender, e) /// <summary> /// 갱신 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void refreshButton_Click(object sender, RoutedEventArgs e) { SetProcessData(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 프로세스 데이터 설정하기 - SetProcessData() /// <summary> /// 프로세스 데이터 설정하기 /// </summary> private void SetProcessData() { this.processCollection.Clear(); IReadOnlyList<ProcessDiagnosticInfo> processDiagnosticInfoList = ProcessDiagnosticInfo.GetForProcesses(); if(processDiagnosticInfoList != null) { foreach(ProcessDiagnosticInfo processDiagnosticInfo in processDiagnosticInfoList) { BitmapImage bitmapImage = null; if(processDiagnosticInfo.IsPackaged) { bitmapImage = this.defaultAppImage; } else { bitmapImage = this.defaultProcessImage; } ProcessModel process = new ProcessModel(processDiagnosticInfo, bitmapImage); this.processCollection.Add(process); } } } #endregion } } |