[C#/WPF/NODENETWORK] NodeNetworkToolkit 누겟 설치하기
■ NodeNetworkToolkit 누겟을 설치하는 방법을 보여준다. 1. Visual Studio를 실행한다. 2. [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 실행한다.
■ NodeNetworkToolkit 누겟을 설치하는 방법을 보여준다. 1. Visual Studio를 실행한다. 2. [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 실행한다.
■ NetworkView 엘리먼트를 사용해 노드를 만드는 방법을 보여준다. ▶ MainApplication.xaml
1 2 3 4 5 6 7 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> </Application> |
▶ MainApplication.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 |
using System.Windows; using NodeNetwork; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStartup(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); NNViewRegistrar.RegisterSplat(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:nn="clr-namespace:NodeNetwork.Views;assembly=NodeNetwork" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <nn:NetworkView Name="networkView" Margin="10" /> </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 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 |
using System.Windows; using DynamicData; using NodeNetwork.ViewModels; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); NodeInputViewModel nodeInputViewModel1 = new NodeInputViewModel(); nodeInputViewModel1.Name = "Node 1 input"; NodeViewModel nodeViewModel1 = new NodeViewModel(); nodeViewModel1.Name = "Node 1"; nodeViewModel1.Position = new Point(200, 200); nodeViewModel1.Inputs.Add(nodeInputViewModel1); NodeOutputViewModel nodeOutputViewModel2 = new NodeOutputViewModel(); nodeOutputViewModel2.Name = "Node 2 output"; NodeViewModel nodeViewModel2 = new NodeViewModel(); nodeViewModel2.Name = "Node 2"; nodeViewModel2.Position = new Point(400, 200); nodeViewModel2.Outputs.Add(nodeOutputViewModel2); NetworkViewModel networkViewModel = new NetworkViewModel(); networkViewModel.Nodes.Add(nodeViewModel1); networkViewModel.Nodes.Add(nodeViewModel2); this.networkView.ViewModel = networkViewModel; } #endregion } } |
TestProject.zip
■ NodeNetwork 누겟을 설치하는 방법을 보여준다. 1. Visual Studio를 실행한다. 2. [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 실행한다.
■ FontIcon 엘리먼트의 전체 폰트 아이콘을 나열하는 방법을 보여준다. ▶ NativeHelper.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 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 네이티브 헬퍼 /// </summary> public class NativeHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 현재 패키지 ID 구하기 - GetCurrentPackageId(bufferLength, buffer) /// <summary> /// 현재 패키지 ID 구하기 /// </summary> /// <param name="bufferLength">버퍼 길이</param> /// <param name="buffer">버퍼</param> /// <returns>처리 결과</returns> [DllImport("api-ms-win-appmodel-runtime-l1-1-1", SetLastError = true)] [return : MarshalAs(UnmanagedType.U4)] private static extern uint GetCurrentPackageId(ref int bufferLength, out byte buffer); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// ERROR_SUCCESS /// </summary> public const int ERROR_SUCCESS = 0; /// <summary> /// ERROR_INSUFFICIENT_BUFFER /// </summary> public const int ERROR_INSUFFICIENT_BUFFER = 122; /// <summary> /// APPMODEL_ERROR_NO_PACKAGE /// </summary> public const int APPMODEL_ERROR_NO_PACKAGE = 15700; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 애플리케이션 패키지 여부 - IsAppPackaged /// <summary> /// 애플리케이션 패키지 여부 /// </summary> public static bool IsAppPackaged { get { int bufferSize = 0; uint lastError = GetCurrentPackageId(ref bufferSize, out _); bool isPackaged = true; if(lastError == APPMODEL_ERROR_NO_PACKAGE) { isPackaged = false; } return isPackaged; } } #endregion } } |
▶ FileHelper.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 |
using System; using System.IO; using System.Reflection; using System.Threading.Tasks; using Windows.Storage; namespace TestProject { /// <summary> /// 파일 헬퍼 /// </summary> public static class FileHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 파일 로드하기 (비동기) - LoadTextFileAsync(relativeFilePath) /// <summary> /// 텍스트 파일 로드하기 (비동기) /// </summary> /// <param name="relativeFilePath">상대적인 파일 경로</param> /// <returns>텍스트</returns> public static async Task<string> LoadTextFileAsync(string relativeFilePath) { StorageFile storageFile = null; if(!NativeHelper.IsAppPackaged) { string entryAssemblyFilePath = Assembly.GetEntryAssembly().Location; string entryAssemblyDirectorPath = Path.GetDirectoryName(entryAssemblyFilePath); string filePath = Path.Combine(entryAssemblyDirectorPath, relativeFilePath); string finalFilePath = Path.GetFullPath(filePath); storageFile = await StorageFile.GetFileFromPathAsync(finalFilePath); } else { Uri sourceURI = new Uri($"ms-appx:///{relativeFilePath}"); storageFile = await StorageFile.GetFileFromApplicationUriAsync(sourceURI); } return await FileIO.ReadTextAsync(storageFile); } #endregion } } |
▶ IconInfo.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 |
using System; namespace TestProject { /// <summary> /// 아이콘 정보 /// </summary> public class IconInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 코드 - Code /// <summary> /// 코드 /// </summary> public string Code { get; set; } #endregion #region 문자 - Character /// <summary> /// 문자 /// </summary> public string Character => char.ConvertFromUtf32(Convert.ToInt32(Code, 16)); #endregion #region 코드 글리프 - CodeGlyph /// <summary> /// 코드 글리프 /// </summary> public string CodeGlyph => "\\u" + Code; #endregion #region 텍스트 글리프 - TextGlyph /// <summary> /// 텍스트 글리프 /// </summary> public string TextGlyph => "&#x" + Code + ";"; #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 |
<?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"> <Page.Resources> <DataTemplate x:Key="IconDataTemplateKey"> <ItemContainer Margin="5" Width="96" Height="96" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"> <Grid> <Viewbox Margin="0 0 0 25" Width="28" Height="28"> <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="{Binding Character}" /> </Viewbox> <TextBlock Style="{StaticResource CaptionTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5 0 5 25" Foreground="{ThemeResource TextFillColorSecondaryBrush}" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" Text="{Binding Name}" /> <TextBlock Style="{StaticResource CaptionTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5 0 5 10" Foreground="{ThemeResource TextFillColorSecondaryBrush}" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" Text="{Binding Code}" /> </Grid> </ItemContainer> </DataTemplate> </Page.Resources> <ScrollViewer Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="5"> <ItemsView Name="itemsView" ItemTemplate="{StaticResource IconDataTemplateKey}" TabFocusNavigation="Once"> <ItemsView.Layout> <UniformGridLayout Orientation="Horizontal" /> </ItemsView.Layout> </ItemsView> </ScrollViewer> </Page> |
▶
■ TextBlock 엘리먼트에서 기본 제공 타이포그래피를 사용하는 방법을 보여준다. ▶ 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 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 |
<?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"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="20" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="20" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="20" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Example" /> <TextBlock Grid.Row="0" Grid.Column="2" VerticalAlignment="Center" Text="Variable Font" /> <TextBlock Grid.Row="0" Grid.Column="4" VerticalAlignment="Center" Text="Siz/Line height" /> <TextBlock Grid.Row="0" Grid.Column="6" VerticalAlignment="Center" Text="Style" /> <TextBlock Grid.Row="2" Grid.Column="0" Style="{StaticResource CaptionTextBlockStyle}" Text="Caption" /> <TextBlock Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" Text="Small, Regular" /> <TextBlock Grid.Row="2" Grid.Column="4" VerticalAlignment="Center" Text="12/16 epx" /> <TextBlock Grid.Row="2" Grid.Column="6" VerticalAlignment="Center" Text="CaptionTextBlockStyle" /> <TextBlock Grid.Row="4" Grid.Column="0" Style="{StaticResource BodyTextBlockStyle}" Text="Body" /> <TextBlock Grid.Row="4" Grid.Column="2" VerticalAlignment="Center" Text="Text, Regular" /> <TextBlock Grid.Row="4" Grid.Column="4" VerticalAlignment="Center" Text="14/20 epx" /> <TextBlock Grid.Row="4" Grid.Column="6" VerticalAlignment="Center" Text="BodyTextBlockStyle" /> <TextBlock Grid.Row="6" Grid.Column="0" Style="{StaticResource BodyStrongTextBlockStyle}" Text="Body Strong" /> <TextBlock Grid.Row="6" Grid.Column="2" VerticalAlignment="Center" Text="Text, SemiBold" /> <TextBlock Grid.Row="6" Grid.Column="4" VerticalAlignment="Center" Text="14/20 epx" /> <TextBlock Grid.Row="6" Grid.Column="6" VerticalAlignment="Center" Text="BodyStrongTextBlockStyle" /> <TextBlock Grid.Row="8" Grid.Column="0" Style="{StaticResource SubtitleTextBlockStyle}" Text="Subtitle" /> <TextBlock Grid.Row="8" Grid.Column="2" VerticalAlignment="Center" Text="Display, SemiBold" /> <TextBlock Grid.Row="8" Grid.Column="4" VerticalAlignment="Center" Text="20/28 epx" /> <TextBlock Grid.Row="8" Grid.Column="6" VerticalAlignment="Center" Text="SubtitleTextBlockStyle" /> <TextBlock Grid.Row="10" Grid.Column="0" Style="{StaticResource TitleTextBlockStyle}" Text="Title" /> <TextBlock Grid.Row="10" Grid.Column="2" VerticalAlignment="Center" Text="Display, SemiBold" /> <TextBlock Grid.Row="10" Grid.Column="4" VerticalAlignment="Center" Text="28/36 epx" /> <TextBlock Grid.Row="10" Grid.Column="6" VerticalAlignment="Center" Text="TitleTextBlockStyle" /> <TextBlock Grid.Row="12" Grid.Column="0" Style="{StaticResource TitleLargeTextBlockStyle}" Text="Title Large" /> <TextBlock Grid.Row="12" Grid.Column="2" VerticalAlignment="Center" Text="Display, SemiBold" /> <TextBlock Grid.Row="12" Grid.Column="4" VerticalAlignment="Center" Text="40/52 epx" /> <TextBlock Grid.Row="12" Grid.Column="6" VerticalAlignment="Center" Text="TitleLargeTextBlockStyle" /> <TextBlock Grid.Row="14" Grid.Column="0" Style="{StaticResource DisplayTextBlockStyle}" Text="Display" /> <TextBlock Grid.Row="14" Grid.Column="2" VerticalAlignment="Center" Text="Display, SemiBold" /> <TextBlock Grid.Row="14" Grid.Column="4" VerticalAlignment="Center" Text="68/92 epx" /> <TextBlock Grid.Row="14" Grid.Column="6" VerticalAlignment="Center" Text="DisplayTextBlockStyle" /> </Grid> </Page> |
TestProject.zip
■ TextBox 엘리먼트를 사용하는 방법을 보여준다. ▶ 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 |
<?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"> <TextBox /> <TextBox Margin="0 10 0 0" Header="Enter your name : " PlaceholderText="Name" /> <TextBox Margin="0 10 0 0" Foreground="#5178be" FontFamily="Arial" FontSize="24" FontStyle="Italic" CharacterSpacing="200" Text="I am super excited to be here!" IsReadOnly="True" /> <TextBox Margin="0 10 0 0" MinWidth="400" AcceptsReturn="True" IsSpellCheckEnabled="True" SelectionHighlightColor="Green" TextWrapping="Wrap" /> </StackPanel> </Page> |
TestProject.zip
■ TextBlock 엘리먼트를 사용하는 방법을 보여준다. 각 예제는 단순 텍스트 블럭, 스타일 적용 텍스트 블럭, 다양한 속성 설정 텍스트 블럭, 인라인 텍스트
■ RichTextBlock 클래스의 TextHighlighters 속성을 사용해 하이라이트를 설정하는 방법을 보여준다. ▶ 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 |
<?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"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <RichTextBlock Name="richTextBlock" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"> <Paragraph> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua </Paragraph> </RichTextBlock> <ComboBox Name="highlightColorCombobox" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Header="Text highlighting color"> <ComboBoxItem Content="Yellow" /> <ComboBoxItem Content="Red" /> <ComboBoxItem Content="Blue" /> </ComboBox> </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 |
using Microsoft.UI; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.highlightColorCombobox.SelectionChanged += highlightColorCombobox_SelectionChanged; this.highlightColorCombobox.SelectedIndex = 0; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 하이라이트 색상 콤보 박스 선택 변경시 처리하기 - highlightColorCombobox_SelectionChanged(sender, e) /// <summary> /// 하이라이트 색상 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void highlightColorCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem comboBoxItem = (sender as ComboBox).SelectedItem as ComboBoxItem; Windows.UI.Color color = Colors.Yellow; switch(comboBoxItem.Content as string) { case "Yellow" : color = Colors.Yellow; break; case "Red" : color = Colors.Red; break; case "Blue" : color = Colors.Blue; break; } TextRange textRange = new TextRange() { StartIndex = 28, Length = 11 }; TextHighlighter textHighlighter = new TextHighlighter() { Background = new SolidColorBrush(color), Ranges = { textRange } }; this.richTextBlock.TextHighlighters.Clear(); this.richTextBlock.TextHighlighters.Add(textHighlighter); } #endregion } } |
TestProject.zip
■ RichTextBlock 엘리먼트의 OverflowContentTarget 속성을 사용하는 방법을 보여준다. ▶ 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 |
<?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"> <Grid Width="600" Height="400"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <RichTextBlock Grid.Column="0" OverflowContentTarget="{x:Bind richTextBlockOverflow1}" TextAlignment="Justify"> <Paragraph>Linked text containers allow text which does not fit in one element to overflow into a different element on the page. Creative use of linked text containers enables basic multicolumn support and other advanced page layouts.</Paragraph> <Paragraph>Duis sed nulla metus, id hendrerit velit. Curabitur dolor purus, bibendum eu cursus lacinia, interdum vel augue. Aenean euismod eros et sapien vehicula dictum. Duis ullamcorper, turpis nec feugiat tincidunt, dui erat luctus risus, aliquam accumsan lacus est vel quam. Nunc lacus massa, varius eget accumsan id, congue sed orci. Duis dignissim hendrerit egestas. Proin ut turpis magna, sit amet porta erat. Nunc semper metus nec magna imperdiet nec vestibulum dui fringilla. Sed sed ante libero, nec porttitor mi. Ut luctus, neque vitae placerat egestas, urna leo auctor magna, sit amet ultricies ipsum felis quis sapien. Proin eleifend varius dui, at vestibulum nunc consectetur nec. Mauris nulla elit, ultrices a sodales non, aliquam ac est. Quisque sit amet risus nulla. Quisque vestibulum posuere velit, vitae vestibulum eros scelerisque sit amet. In in risus est, at laoreet dolor. Nullam aliquet pellentesque convallis. Ut vel tincidunt nulla. Mauris auctor tincidunt auctor.Aenean orci ante, vulputate ac sagittis sit amet, consequat at mi. Morbi elementum purus consectetur nisi adipiscing vitae blandit sapien placerat. Aliquam adipiscing tortor non sem lobortis consectetur mattis felis rhoncus. Nunc eu nunc rhoncus arcu sollicitudin ultrices. In vulputate eros in mauris aliquam id dignissim nisl laoreet.</Paragraph> </RichTextBlock> <RichTextBlockOverflow Name="richTextBlockOverflow1" Grid.Column="1" OverflowContentTarget="{x:Bind richTextBlockOverflow2}" Margin="10 0 0 0" /> <RichTextBlockOverflow Name="richTextBlockOverflow2" Grid.Column="2" Margin="10 0 0 0" /> </Grid> </Page> |
TestProject.zip
■ RichTextBlock 엘리먼트의 SelectionHighlightColor 속성을 사용하는 방법을 보여준다. ▶ 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 |
<?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"> <RichTextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" SelectionHighlightColor="Green"> <Paragraph> RichTextBlock provides a rich text display container that supports <Run FontStyle="Italic" FontWeight="Bold">formatted text</Run>, <Hyperlink NavigateUri="https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Documents.Hyperlink"> hyperlinks </Hyperlink>, inline images, and other rich content. </Paragraph> <Paragraph>RichTextBlock also supports a built-in overflow model.</Paragraph> </RichTextBlock> </Page> |
TestProject.zip
■ RichEditBox 클래스를 사용해 커스텀 에디터를 만드는 방법을 보여준다. ▶ 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 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 |
<?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"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" HorizontalAlignment="Left" Orientation="Horizontal"> <Button Name="openFileButton" ToolTipService.ToolTip="Open file"> <Button.Content> <FontIcon Glyph=""/> </Button.Content> </Button> <Button Name="saveFileButton" Margin="5 0 0 0" ToolTipService.ToolTip="Save file"> <Button.Content> <FontIcon Glyph=""/> </Button.Content> </Button> </StackPanel> <StackPanel Grid.Row="0" HorizontalAlignment="Right" Orientation="Horizontal"> <Button Name="boldButton" ToolTipService.ToolTip="Bold"> <Button.Content> <FontIcon Glyph=""/> </Button.Content> </Button> <Button Name="italicButton" Margin="5 0 0 0" ToolTipService.ToolTip="Italic"> <Button.Content> <FontIcon Glyph=""/> </Button.Content> </Button> <DropDownButton Name="fontColorButton" Margin="5 0 0 0" BorderThickness="0" ToolTipService.ToolTip="Font color" Background="Transparent"> <SymbolIcon Symbol="FontColor" /> <DropDownButton.Flyout> <Flyout Placement="Bottom"> <VariableSizedWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3"> <VariableSizedWrapGrid.Resources> <Style TargetType="Rectangle"> <Setter Property="Width" Value="32" /> <Setter Property="Height" Value="32" /> </Style> <Style TargetType="Button"> <Setter Property="Margin" Value="5" /> <Setter Property="MinWidth" Value="0" /> <Setter Property="MinHeight" Value="0" /> <Setter Property="Padding" Value="0" /> </Style> </VariableSizedWrapGrid.Resources> <Button Name="redColorButton"> <Button.Content> <Rectangle Fill="Red" /> </Button.Content> </Button> <Button Name="orangeColorButton"> <Button.Content> <Rectangle Fill="Orange" /> </Button.Content> </Button> <Button Name="yellowColorButton"> <Button.Content> <Rectangle Fill="Yellow" /> </Button.Content> </Button> <Button Name="greenColorButton"> <Button.Content> <Rectangle Fill="Green" /> </Button.Content> </Button> <Button Name="blueColorButton"> <Button.Content> <Rectangle Fill="Blue" /> </Button.Content> </Button> <Button Name="indigoColorButton"> <Button.Content> <Rectangle Fill="Indigo" /> </Button.Content> </Button> <Button Name="violetColorButton"> <Button.Content> <Rectangle Fill="Violet" /> </Button.Content> </Button> <Button Name="grayColorButton"> <Button.Content> <Rectangle Fill="Gray" /> </Button.Content> </Button> </VariableSizedWrapGrid> </Flyout> </DropDownButton.Flyout> </DropDownButton> </StackPanel> <RichEditBox Name="richEditBox" Grid.Row="2" /> <StackPanel Name="findStackPanel" Grid.Row="4" Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="Find" /> <TextBox Name="findTextBox" Margin="10 0 0 0" Width="150" PlaceholderText="Enter search text" GotFocus="{x:Bind SetFindTextBoxHighlight}" TextChanged="{x:Bind SetFindTextBoxHighlight}" LostFocus="{x:Bind ClearFindTextBoxHighlight}" /> </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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Provider; using Windows.Storage.Streams; using Windows.UI; using Windows.UI.Popups; using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Text; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Shapes; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 활성 윈도우 구하기 - GetActiveWindow() /// <summary> /// 활성 윈도우 구하기 /// </summary> /// <returns></returns> [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Auto, PreserveSig = true, SetLastError = false)] private static extern IntPtr GetActiveWindow(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 현재 색상 /// </summary> private Color currentColor = Colors.Green; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.openFileButton.Click += openFileButton_Click; this.saveFileButton.Click += saveFileButton_Click; this.boldButton.Click += boldButton_Click; this.italicButton.Click += italicButton_Click; this.redColorButton.Click += colorButton_Click; this.orangeColorButton.Click += colorButton_Click; this.yellowColorButton.Click += colorButton_Click; this.greenColorButton.Click += colorButton_Click; this.blueColorButton.Click += colorButton_Click; this.indigoColorButton.Click += colorButton_Click; this.violetColorButton.Click += colorButton_Click; this.grayColorButton.Click += colorButton_Click; this.richEditBox.GotFocus += richEditBox_GotFocus; this.richEditBox.TextChanged += richEditBox_TextChanged; this.richEditBox.Document.GetDefaultCharacterFormat().ForegroundColor = Colors.Green; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #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) { FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; fileOpenPicker.FileTypeFilter.Add(".rtf"); if(Window.Current == null) { IntPtr windowHandle = GetActiveWindow(); InitializeWithWindow.Initialize(fileOpenPicker, windowHandle); } StorageFile storageFile = await fileOpenPicker.PickSingleFileAsync(); if(storageFile != null) { using(IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read)) { this.richEditBox.Document.LoadFromStream(TextSetOptions.FormatRtf, stream); } } } #endregion #region Save file 버튼 클릭시 처리하기 - saveFileButton_Click(sender, e) /// <summary> /// Save file 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void saveFileButton_Click(object sender, RoutedEventArgs e) { FileSavePicker fileSavePicker = new FileSavePicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; fileSavePicker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" }); fileSavePicker.SuggestedFileName = "New Document"; if(Window.Current == null) { IntPtr windowHandle = GetActiveWindow(); InitializeWithWindow.Initialize(fileSavePicker, windowHandle); } StorageFile storageFile = await fileSavePicker.PickSaveFileAsync(); if(storageFile != null) { CachedFileManager.DeferUpdates(storageFile); using(IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { this.richEditBox.Document.SaveToStream(TextGetOptions.FormatRtf, stream); } FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(storageFile); if(status != FileUpdateStatus.Complete) { MessageDialog messageDialog = new MessageDialog("File " + storageFile.Name + " couldn't be saved."); await messageDialog.ShowAsync(); } } } #endregion #region Bold 버튼 클릭시 처리하기 - boldButton_Click(sender, e) /// <summary> /// Bold 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void boldButton_Click(object sender, RoutedEventArgs e) { this.richEditBox.Document.Selection.CharacterFormat.Bold = FormatEffect.Toggle; } #endregion #region Italic 버튼 클릭시 처리하기 - italicButton_Click(sender, e) /// <summary> /// Italic 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void italicButton_Click(object sender, RoutedEventArgs e) { this.richEditBox.Document.Selection.CharacterFormat.Italic = FormatEffect.Toggle; } #endregion #region 색상 버튼 클릭시 처리하기 - colorButton_Click(sender, e) /// <summary> /// 색상 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorButton_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; Rectangle rectangle = button.Content as Rectangle; Color color = (rectangle.Fill as SolidColorBrush).Color; this.richEditBox.Document.Selection.CharacterFormat.ForegroundColor = color; this.fontColorButton.Flyout.Hide(); this.richEditBox.Focus(FocusState.Keyboard); this.currentColor = color; } #endregion #region 리치 에디트 박스 포커스 획득시 처리하기 - richEditBox_GotFocus(sender, e) /// <summary> /// 리치 에디트 박스 포커스 획득시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richEditBox_GotFocus(object sender, RoutedEventArgs e) { this.richEditBox.Document.GetText(TextGetOptions.UseCrlf, out _); ITextRange textRange = richEditBox.Document.GetRange(0, TextConstants.MaxUnitCount); SolidColorBrush backgroundBrush = App.Current.Resources["TextControlBackgroundFocused"] as SolidColorBrush; if(backgroundBrush != null) { textRange.CharacterFormat.BackgroundColor = backgroundBrush.Color; } } #endregion #region 리치 에디트 박스 텍스트 변경시 처리하기 - richEditBox_TextChanged(sender, e) /// <summary> /// 리치 에디트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richEditBox_TextChanged(object sender, RoutedEventArgs e) { if(this.richEditBox.Document.Selection.CharacterFormat.ForegroundColor != this.currentColor) { this.richEditBox.Document.Selection.CharacterFormat.ForegroundColor = this.currentColor; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region Find 텍스트 박스 하이라이트 설정하기 - SetFindTextBoxHighlight() /// <summary> /// Find 텍스트 박스 하이라이트 설정하기 /// </summary> private void SetFindTextBoxHighlight() { ClearFindTextBoxHighlight(); Color highlightBackgroundColor = (Color)App.Current.Resources["SystemColorHighlightColor" ]; Color highlightForegroundColor = (Color)App.Current.Resources["SystemColorHighlightTextColor"]; string textToFind = this.findTextBox.Text; if(textToFind != null) { ITextRange textRange = this.richEditBox.Document.GetRange(0, 0); while(textRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.None) > 0) { textRange.CharacterFormat.BackgroundColor = highlightBackgroundColor; textRange.CharacterFormat.ForegroundColor = highlightForegroundColor; } } } #endregion #region Find 텍스트 박스 하이라이트 지우기 - ClearFindTextBoxHighlight() /// <summary> /// Find 텍스트 박스 하이라이트 지우기 /// </summary> private void ClearFindTextBoxHighlight() { ITextRange textRange = this.richEditBox.Document.GetRange(0, TextConstants.MaxUnitCount); SolidColorBrush defaultBackgroundBrush = this.richEditBox.Background as SolidColorBrush; SolidColorBrush defaultForegroundBrush = this.richEditBox.Foreground as SolidColorBrush; textRange.CharacterFormat.BackgroundColor = defaultBackgroundBrush.Color; textRange.CharacterFormat.ForegroundColor = defaultForegroundBrush.Color; } #endregion } } |
TestProject.zip
■ RichEditBox 클래스의 SelectionFlyout/ContextFlyout 속성을 사용해 플라이아웃 메뉴를 추가하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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"> <RichEditBox Name="richEditBox" Margin="10" /> </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 |
using Windows.Foundation.Metadata; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.richEditBox.Loaded += richEditBox_Loaded; this.richEditBox.Unloaded += richEditBox_Unloaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 리치 에디트 박스 로드시 처리하기 - richEditBox_Loaded(sender, e) /// <summary> /// 리치 에디트 박스 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richEditBox_Loaded(object sender, RoutedEventArgs e) { // UniversalApiContract 7 이전에는 RichEditBox에 기본 ContextFlyout 세트가 없었다. if(ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7)) { this.richEditBox.SelectionFlyout.Opening += flyout_Opening; this.richEditBox.ContextFlyout.Opening += flyout_Opening; } } #endregion #region 리치 에디트 박스 언로드시 처리하기 - richEditBox_Unloaded(sender, e) /// <summary> /// 리치 에디트 박스 언로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richEditBox_Unloaded(object sender, RoutedEventArgs e) { // UniversalApiContract 7 이전에는 RichEditBox에 기본 ContextFlyout 세트가 없었다. if(ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7)) { this.richEditBox.SelectionFlyout.Opening -= flyout_Opening; this.richEditBox.ContextFlyout.Opening -= flyout_Opening; } } #endregion #region 플라이아웃 오픈시 처리하기 - flyout_Opening(sender, e) /// <summary> /// 플라이아웃 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void flyout_Opening(object sender, object e) { CommandBarFlyout commandBarFlyout = sender as CommandBarFlyout; if(commandBarFlyout != null && commandBarFlyout.Target == this.richEditBox) { AppBarButton appBarButton = new AppBarButton { Command = new StandardUICommand(StandardUICommandKind.Share) }; commandBarFlyout.PrimaryCommands.Add(appBarButton); } } #endregion } } |
TestProject.zip
■ PassworBox 엘리먼트의 PasswordRevealMode 속성을 사용하는 방법을 보여준다. ▶ 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 |
<?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"> <PasswordBox Name="passworBox" VerticalAlignment="Center" Width="250" PasswordRevealMode="Hidden" /> <CheckBox Name="revealModeCheckBox" VerticalAlignment="Center" Margin="10 0 0 0" Content="Show password" IsChecked="False" /> </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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.revealModeCheckBox.Checked += revealModeCheckBox_CheckedChanged; this.revealModeCheckBox.Unchecked += revealModeCheckBox_CheckedChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region Reveal Mode 체크 박스 체크 변경시 처리하기 - revealModeCheckBox_CheckedChanged(sender, e) /// <summary> /// Reveal Mode 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void revealModeCheckBox_CheckedChanged(object sender, RoutedEventArgs e) { if(this.revealModeCheckBox.IsChecked == true) { this.passworBox.PasswordRevealMode = PasswordRevealMode.Visible; } else { this.passworBox.PasswordRevealMode = PasswordRevealMode.Hidden; } } #endregion } } |
TestProject.zip
■ PasswordBox 엘리먼트의 Header/PlaceholderText/PasswordChar 속성을 사용하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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"> <PasswordBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Header="Password" PlaceholderText="Enter your password" PasswordChar="#" /> </Page> |
TestProject.zip
■ PasswordBox 클래스의 PasswordChanged 이벤트를 사용하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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"> <PasswordBox Name="passwordBox" HorizontalAlignment="Center" Width="300" /> <TextBlock Name="outputTextBlock" Margin="0 10 0 0" FontFamily="Global User Interface" Text="Please input password." /> </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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.passwordBox.PasswordChanged += passwordBox_PasswordChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 패스워드 박스 패스워드 변경시 처리하기 - passwordBox_PasswordChanged(sender, e) /// <summary> /// 패스워드 박스 패스워드 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void passwordBox_PasswordChanged(object sender, RoutedEventArgs e) { if(sender is PasswordBox passwordBox) { if(string.IsNullOrEmpty(passwordBox.Password)) { this.outputTextBlock.Visibility = Visibility.Visible; this.outputTextBlock.Text = "Please input password."; this.passwordBox.Password = string.Empty; } else { this.outputTextBlock.Visibility = Visibility.Collapsed; this.outputTextBlock.Text = string.Empty; } } } #endregion } } |
TestProject.zip
■ NumberBox 클래스의 NumberFormatter 속성을 사용해 특정 값 단위로 반올림하는 포맷을 설정하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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"> <NumberBox Name="numberBox" HorizontalAlignment="Center" VerticalAlignment="Center" Header="Enter a dollar amount : " PlaceholderText="0.00" /> </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 |
using Windows.Globalization.NumberFormatting; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); IncrementNumberRounder incrementNumberRounder = new IncrementNumberRounder { Increment = 0.25, RoundingAlgorithm = RoundingAlgorithm.RoundHalfUp }; DecimalFormatter decimalFormatter = new DecimalFormatter { IntegerDigits = 1, FractionDigits = 2, NumberRounder = incrementNumberRounder }; this.numberBox.NumberFormatter = decimalFormatter; } #endregion } } |
TestProject.zip
■ Directory 클래스의 EnumerateFiles 정적 메소드에서 특정 파일 확장자를 갖는 파일 목록을 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 |
IEnumerable<string> fileEnumerable = Directory.EnumerateFiles("D:\\temp", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".png")); |
■ NumberBox 엘리먼트의 SpinButtonPlacementMode 속성을 사용하는 방법을 보여준다. ▶ 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 |
<?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"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <NumberBox Name="numberBox" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Header="Enter an integer :" SpinButtonPlacementMode="Compact" SmallChange="10" LargeChange="100" Value="10" /> <RadioButtons Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Header="SpinButton placement"> <RadioButton Name="inlineRadioButton" Tag="Inline" Content="Inline" /> <RadioButton Name="compactRadioButton" Tag="Compact" Content="Compact" Checked="radioButton_Checked" /> </RadioButtons> </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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.inlineRadioButton.Checked += radioButton_Checked; this.compactRadioButton.Checked += radioButton_Checked; this.compactRadioButton.IsChecked = true; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 라디오 버튼 체크시 처리하기 - radioButton_Checked(sender, e) /// <summary> /// 라디오 버튼 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void radioButton_Checked(object sender, RoutedEventArgs e) { if(sender is RadioButton radioButton && this.numberBox != null) { string spinButtonPlacementModeName = radioButton.Tag.ToString(); switch(spinButtonPlacementModeName) { case "Inline" : this.numberBox.SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Inline; break; case "Compact" : this.numberBox.SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Compact; break; } } } #endregion } } |
TestProject.zip
■ NumberBox 엘리먼트의 AcceptsExpression 속성을 사용해 수식을 평가하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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"> <NumberBox HorizontalAlignment="Center" VerticalAlignment="Center" Header="Enter an expression :" PlaceholderText="1 + 2^2" AcceptsExpression="True" Value="NaN" /> </Page> |
TestProject.zip
■ AutoSuggestBox 클래스의 QueryIcon 속성과 QuerySubmitted 이벤트를 사용하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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"> <AutoSuggestBox Name="autoSuggestBox" Width="300" PlaceholderText="Type cat name" QueryIcon="Find" /> <TextBlock Name="outputTextBlock" Margin="0 10 0 0" FontFamily="Global User Interface" /> </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 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
using System.Collections.Generic; using System.Linq; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 리스트 /// </summary> private List<string> sourceList = new List<string>() { "Abyssinian", "Aegean", "American Bobtail", "American Curl", "American Ringtail", "American Shorthair", "American Wirehair", "Aphrodite Giant", "Arabian Mau", "Asian cat", "Asian Semi-longhair", "Australian Mist", "Balinese", "Bambino", "Bengal", "Birman", "Brazilian Shorthair", "British Longhair", "British Shorthair", "Burmese", "Burmilla", "California Spangled", "Chantilly-Tiffany", "Chartreux", "Chausie", "Colorpoint Shorthair", "Cornish Rex", "Cymric", "Cyprus", "Devon Rex", "Donskoy", "Dragon Li", "Dwelf", "Egyptian Mau", "European Shorthair", "Exotic Shorthair", "Foldex", "German Rex", "Havana Brown", "Highlander", "Himalayan", "Japanese Bobtail", "Javanese", "Kanaani", "Khao Manee", "Kinkalow", "Korat", "Korean Bobtail", "Korn Ja", "Kurilian Bobtail", "Lambkin", "LaPerm", "Lykoi", "Maine Coon", "Manx", "Mekong Bobtail", "Minskin", "Napoleon", "Munchkin", "Nebelung", "Norwegian Forest Cat", "Ocicat", "Ojos Azules", "Oregon Rex", "Persian (modern)", "Persian (traditional)", "Peterbald", "Pixie-bob", "Ragamuffin", "Ragdoll", "Raas", "Russian Blue", "Russian White", "Sam Sawet", "Savannah", "Scottish Fold", "Selkirk Rex", "Serengeti", "Serrade Petit", "Siamese", "Siberian or´Siberian Forest Cat", "Singapura", "Snowshoe", "Sokoke", "Somali", "Sphynx", "Suphalak", "Thai", "Thai Lilac", "Tonkinese", "Toyger", "Turkish Angora", "Turkish Van", "Turkish Vankedisi", "Ukrainian Levkoy", "Wila Krungthep", "York Chocolate" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.autoSuggestBox.TextChanged += autoSuggestBox_TextChanged; this.autoSuggestBox.QuerySubmitted += autoSuggestBox_QuerySubmitted; this.autoSuggestBox.SuggestionChosen += autoSuggestBox_SuggestionChosen; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 자동 제안 박스 텍스트 변경시 처리하기 - autoSuggestBox_TextChanged(sender, e) /// <summary> /// 자동 제안 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void autoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e) { if(e.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { List<string> searchList = GetSearchList(sender.Text); if(searchList.Count > 0) { sender.ItemsSource = searchList; } else { sender.ItemsSource = new string[] { "No results found" }; } } } #endregion #region 자동 제안 박스 쿼리 제출시 처리하기 - autoSuggestBox_QuerySubmitted(sender, e) /// <summary> /// 자동 제안 박스 쿼리 제출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void autoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs e) { if(e.ChosenSuggestion != null && e.ChosenSuggestion is string) { Select(e.ChosenSuggestion as string); } else if(!string.IsNullOrEmpty(e.QueryText)) { List<string> searchList = GetSearchList(sender.Text); if(searchList.Count > 0) { Select(searchList.FirstOrDefault()); } } } #endregion #region 자동 제안 박스 제안 선택시 처리하기 - autoSuggestBox_SuggestionChosen(sender, e) /// <summary> /// 자동 제안 박스 제안 선택시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void autoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs e) { this.outputTextBlock.Text = e.SelectedItem.ToString(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 조회 리스트 구하기 - GetSearchList(text) /// <summary> /// 조회 리스트 구하기 /// </summary> /// <param name="text">텍스트</param> /// <returns>조회 리스트</returns> private List<string> GetSearchList(string text) { List<string> searchList = new List<string>(); if(string.IsNullOrWhiteSpace(text)) { return searchList; } string[] tokenArray = text.ToLower().Split(" "); foreach(string source in this.sourceList) { bool found = tokenArray.All ( (key) => { return source.ToLower().Contains(key); } ); if(found) { searchList.Add(source); } } return searchList; } #endregion #region 선택하기 - Select(name) /// <summary> /// 선택하기 /// </summary> /// <param name="name">명칭</param> private void Select(string name) { if(string.IsNullOrWhiteSpace(name)) { this.outputTextBlock.Text = name; } } #endregion } } |
TestProject.zip
■ AutoSuggestBox 클래스의 TextChanged/SuggestionChosen 이벤트를 사용하는 방법을 보여준다. ▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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"> <AutoSuggestBox Name="autoSuggestBox" Width="300" /> <TextBlock Name="outputTextBlock" Margin="0 10 0 0" FontFamily="Global User Interface" /> </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 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 |
using System.Collections.Generic; using System.Linq; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 리스트 /// </summary> private List<string> sourceList = new List<string>() { "Abyssinian", "Aegean", "American Bobtail", "American Curl", "American Ringtail", "American Shorthair", "American Wirehair", "Aphrodite Giant", "Arabian Mau", "Asian cat", "Asian Semi-longhair", "Australian Mist", "Balinese", "Bambino", "Bengal", "Birman", "Brazilian Shorthair", "British Longhair", "British Shorthair", "Burmese", "Burmilla", "California Spangled", "Chantilly-Tiffany", "Chartreux", "Chausie", "Colorpoint Shorthair", "Cornish Rex", "Cymric", "Cyprus", "Devon Rex", "Donskoy", "Dragon Li", "Dwelf", "Egyptian Mau", "European Shorthair", "Exotic Shorthair", "Foldex", "German Rex", "Havana Brown", "Highlander", "Himalayan", "Japanese Bobtail", "Javanese", "Kanaani", "Khao Manee", "Kinkalow", "Korat", "Korean Bobtail", "Korn Ja", "Kurilian Bobtail", "Lambkin", "LaPerm", "Lykoi", "Maine Coon", "Manx", "Mekong Bobtail", "Minskin", "Napoleon", "Munchkin", "Nebelung", "Norwegian Forest Cat", "Ocicat", "Ojos Azules", "Oregon Rex", "Persian (modern)", "Persian (traditional)", "Peterbald", "Pixie-bob", "Ragamuffin", "Ragdoll", "Raas", "Russian Blue", "Russian White", "Sam Sawet", "Savannah", "Scottish Fold", "Selkirk Rex", "Serengeti", "Serrade Petit", "Siamese", "Siberian or´Siberian Forest Cat", "Singapura", "Snowshoe", "Sokoke", "Somali", "Sphynx", "Suphalak", "Thai", "Thai Lilac", "Tonkinese", "Toyger", "Turkish Angora", "Turkish Van", "Turkish Vankedisi", "Ukrainian Levkoy", "Wila Krungthep", "York Chocolate" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.autoSuggestBox.TextChanged += autoSuggestBox_TextChanged; this.autoSuggestBox.SuggestionChosen += autoSuggestBox_SuggestionChosen; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 자동 제안 박스 텍스트 변경시 처리하기 - autoSuggestBox_TextChanged(sender, e) /// <summary> /// 자동 제안 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void autoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e) { if(e.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { List<string> list = new List<string>(); string[] tokenArray = sender.Text.ToLower().Split(" "); foreach(string source in this.sourceList) { bool found = tokenArray.All ( (key) => { return source.ToLower().Contains(key); } ); if(found) { list.Add(source); } } if(list.Count == 0) { list.Add("No results found"); } sender.ItemsSource = list; } } #endregion #region 자동 제안 박스 제안 선택시 처리하기 - autoSuggestBox_SuggestionChosen(sender, e) /// <summary> /// 자동 제안 박스 제안 선택시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void autoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs e) { this.outputTextBlock.Text = e.SelectedItem.ToString(); } #endregion } } |
TestProject.zip
■ FileSavePicker 클래스의 PickSaveFileAsync 메소드를 사용해 파일을 저장하는 방법을 보여준다. ▶ 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="saveFileButton" HorizontalAlignment="Center" Padding="10" Content="Save File" /> <TextBlock Name="outputTextBlock" VerticalAlignment="Center" Margin="10 0 0 0" TextWrapping="Wrap" /> </StackPanel> </Page> |
■ FolderPicker 클래스의 PickSingleFolderAsync 메소드를 사용해 폴더를 선택하는 방법을 보여준다. ▶ 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="openFolderButton" HorizontalAlignment="Center" Padding="10" Content="Open Folder" /> <TextBlock Name="outputTextBlock" VerticalAlignment="Center" Margin="10 0 0 0" TextWrapping="Wrap" /> </StackPanel> </Page> |
■ FileOpenPicker 클래스의 PickMultipleFilesAsync 메소드를 사용해 복수 파일을 선택하는 방법을 보여준다. ▶ 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
■ 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