728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] 누겟 설치 : ZXing.Net.Maui (0) | 2022.08.21 |
---|---|
[C#/MAUI/.NET6] Border 엘리먼트 : 제목 장식하기 (0) | 2022.08.21 |
[C#/MAUI/.NET6] Label 엘리먼트 : Shadow 속성 사용하기 (0) | 2022.08.21 |
[C#/MAUI/.NET6] EnumToBoolConverter 클래스 : 열거형 값 → 진리 값 변환자 사용하기 (0) | 2022.08.01 |
[C#/MAUI/.NET6] Popup 엘리먼트 : 팝업 표시하기 (0) | 2022.08.01 |
[C#/MAUI/.NET6] DrawingView 클래스 : GetImageStream 메소드를 사용해 이미지 구하기 (0) | 2022.07.31 |
[C#/MAUI/.NET6] DrawingView 클래스 : DrawingLineCompleted 이벤트를 사용해 선 그리기 완료시 처리하기 (0) | 2022.07.31 |
[C#/MAUI/.NET6] DrawingView 클래스 : IsMultiLineModeEnabled/ShouldClearOnFinish 속성을 사용해 2개 이상의 선 그리기 (0) | 2022.07.31 |
[C#/MAUI/.NET6] DrawingView 엘리먼트 : IsMultiLineModeEnabled/ShouldClearOnFinish 속성을 사용해 2개 이상의 선 그리기 (0) | 2022.07.31 |
[C#/MAUI/.NET6] DrawingView 클래스 : LineWidth/LineColor/Lines 속성 사용하기 (0) | 2022.07.31 |
댓글을 달아 주세요