첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.15MB

▶ MainPage.xaml

<?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>

 

300x250

 

▶ MainPage.xaml.cs

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
}

 

728x90

 

▶ MauiProgram.cs

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
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요