■ FileOpenPicker 클래스의 PickSingleFileAsync 메소드를 사용해 이미지 파일을 선택하는 방법을 보여준다.
▶ WindowHelper.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 |
using System.Collections.Generic; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; namespace TestProject { /// <summary> /// 윈도우 헬퍼 /// </summary> public class WindowHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 활성 윈도우 리스트 /// </summary> private static List<Window> _activeWindowList = new List<Window>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 루트 테마 - RootTheme /// <summary> /// 루트 테마 /// </summary> public static ElementTheme RootTheme { get { foreach(Window window in _activeWindowList) { if(window.Content is FrameworkElement rootElement) { return rootElement.RequestedTheme; } } return ElementTheme.Default; } } #endregion #region 활성 윈도우 리스트 - ActiveWindowList /// <summary> /// 활성 윈도우 리스트 /// </summary> public static List<Window> ActiveWindowList { get { return _activeWindowList; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 윈도우 추적하기 - TrackWindow(window) /// <summary> /// 윈도우 추적하기 /// </summary> /// <param name="window">윈도우</param> public static void TrackWindow(Window window) { window.Closed += (sender, e) => { _activeWindowList.Remove(window); }; _activeWindowList.Add(window); } #endregion #region 윈도우 생성하기 - CreateWindow() /// <summary> /// 윈도우 생성하기 /// </summary> /// <returns>윈도우</returns> public static Window CreateWindow() { Window window = new Window { SystemBackdrop = new MicaBackdrop() }; TrackWindow(window); return window; } #endregion #region 엘리먼트를 위한 윈도우 구하기 - GetWindowForElement(element) /// <summary> /// 엘리먼트를 위한 윈도우 구하기 /// </summary> /// <param name="element">엘리먼트</param> /// <returns>윈도우</returns> public static Window GetWindowForElement(UIElement element) { if(element.XamlRoot != null) { foreach(Window window in _activeWindowList) { if(element.XamlRoot == window.Content.XamlRoot) { return window; } } } return null; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Frame Name="frame" /> </Window> |
▶ MainWindow.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 |
using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); WindowHelper.TrackWindow(this); } #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 |
<?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" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Button Name="openFileButton" HorizontalAlignment="Center" Padding="10" Content="Open File" /> <TextBlock Name="outputTextBlock" VerticalAlignment="Center" Margin="10 0 0 0" TextWrapping="Wrap" /> </StackPanel> </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 |
using System; using Windows.Storage; using Windows.Storage.Pickers; using WinRT.Interop; using Microsoft.UI.Text; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.openFileButton.Click += openFileButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Open File 버튼 클릭시 처리하기 - openFileButton_Click(sender, e) /// <summary> /// Open File 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void openFileButton_Click(object sender, RoutedEventArgs e) { this.outputTextBlock.Text = ""; FileOpenPicker fileOpenPicker = new FileOpenPicker(); Window window = WindowHelper.GetWindowForElement(this); IntPtr windowHandle = WindowNative.GetWindowHandle(window); InitializeWithWindow.Initialize(fileOpenPicker, windowHandle); fileOpenPicker.ViewMode = PickerViewMode.Thumbnail; fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg" ); fileOpenPicker.FileTypeFilter.Add(".jpeg"); fileOpenPicker.FileTypeFilter.Add(".png" ); StorageFile storageFile = await fileOpenPicker.PickSingleFileAsync(); if(storageFile != null) { Span span = new Span(); Run run1 = new Run(); run1.Text = "Picked photo : "; Run run2 = new Run(); run2.FontWeight = FontWeights.Bold; run2.Text = storageFile.Name; span.Inlines.Add(run1); span.Inlines.Add(run2); this.outputTextBlock.Inlines.Add(span); } else { this.outputTextBlock.Text = "Operation cancelled."; } } #endregion } } |