■ DrawingView 클래스를 사용해 DrawingLine 객체를 생성해 선을 추가하는 방법을 보여준다.
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"> <Grid Margin="10" RowDefinitions="*,10,Auto"> <toolkit:DrawingView x:Name="drawingView" Grid.Row="0" Margin="10" Background="Beige" IsMultiLineModeEnabled="true" ShouldClearOnFinish="false" LineWidth="5" LineColor="Blue" /> <Button x:Name="addButton" Grid.Row="2" Margin="10" Text="선 추가하기" /> </Grid> </ContentPage> |
▶ 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 |
using System.Collections.ObjectModel; using CommunityToolkit.Maui.Core; using CommunityToolkit.Maui.Core.Views; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 선 컬렉션 /// </summary> private ObservableCollection<IDrawingLine> lineCollection = new ObservableCollection<IDrawingLine>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.drawingView.Lines = this.lineCollection; this.addButton.Clicked += addButton_Clicked; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 선 추가하기 버튼 클릭시 처리하기 - addButton_Clicked(sender, e) /// <summary> /// 선 추가하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addButton_Clicked(object sender, EventArgs e) { double viewWidth = double.IsNaN(this.drawingView.Width ) || this.drawingView.Width <= 1 ? 200 : this.drawingView.Width; double viewHeight = double.IsNaN(this.drawingView.Height) || this.drawingView.Height <= 1 ? 200 : this.drawingView.Height; this.lineCollection.Add ( new DrawingLine() { Granularity = 5, ShouldSmoothPathWhenDrawn = true, LineWidth = 10, LineColor = Color.FromRgb(Random.Shared.Next(255), Random.Shared.Next(255), Random.Shared.Next(255)), Points = new(GetPointEnumerable(10, viewWidth, viewHeight)) } ); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 포인트 열거 가능형 구하기 - GetPointEnumerable(pointCount, viewWidth, viewHeight) /// <summary> /// 포인트 열거 가능형 구하기 /// </summary> /// <param name="pointCount">포인트 수</param> /// <param name="viewWidth">뷰 너비</param> /// <param name="viewHeight">뷰 높이</param> /// <returns>포인트 열거 가능형</returns> private IEnumerable<PointF> GetPointEnumerable(int pointCount, double viewWidth, double viewHeight) { double paddedViewWidth = Math.Clamp(viewWidth - 10, 1, viewWidth ); double paddedViewHeight = Math.Clamp(viewHeight - 10, 1, viewHeight); int maximumWidth = (int)Math.Round(paddedViewWidth , MidpointRounding.AwayFromZero); int maximumHeight = (int)Math.Round(paddedViewHeight, MidpointRounding.AwayFromZero); for(int i = 0; i < pointCount; i++) { yield return new PointF(Random.Shared.Next(1, maximumWidth), Random.Shared.Next(1, maximumHeight)); } } #endregion } |
▶ MauiProgram.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 |
using CommunityToolkit.Maui; namespace TestProject; /// <summary> /// MAUI 프로그램 /// </summary> public static class MauiProgram { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region MAUI 앱 생성하기 - CreateMauiApp() /// <summary> /// MAUI 앱 생성하기 /// </summary> /// <returns>MAUI 앱</returns> public static MauiApp CreateMauiApp() { MauiAppBuilder builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UseMauiCommunityToolkit() .ConfigureFonts ( fontCollection => { fontCollection.AddFont("OpenSans-Regular.ttf" , "OpenSansRegular" ); fontCollection.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); } ); return builder.Build(); } #endregion } |