첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
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">
    <Grid
        Margin="10"
        RowDefinitions="Auto,10,*">
        <Frame Grid.Row="0"
            Padding="10">
            <Grid RowDefinitions="Auto,Auto,Auto">
                <Label Grid.Row="0"
                    HorizontalOptions="Start"
                    Text="보낼 메시지" />
                <Entry x:Name="sendEntry" Grid.Row="1"
                    HorizontalOptions="StartAndExpand"
                    Margin="0,10,0,0"
                    Placeholder="보낼 메시지를 입력해 주시기 바랍니다."
                    MaxLength="20" />
                <Button x:Name="sendButton" Grid.Row="2"
                    HorizontalOptions="Center"
                    Margin="0,10,0,0"
                    Text="메시지 보내기" />
            </Grid>
        </Frame>
        <Frame Grid.Row="2"
            Padding="10">
            <Grid RowDefinitions="Auto,10,*">
                <StackLayout Grid.Row="0"
                    HorizontalOptions="Center"
                    Orientation="Horizontal"
                    Spacing="10">
                    <Button x:Name="subscribeButton"
                        VerticalOptions="Center"
                        Text="메시지 구독하기" />
                    <Button x:Name="unsubscribeButton"
                        VerticalOptions="Center"
                        Text="구독 취소하기" />
                </StackLayout>
                <Frame Grid.Row="2"
                    BorderColor="LightGray"
                    HasShadow="True"
                    Padding="10">
                    <ScrollView
                        HorizontalOptions="Fill"
                        VerticalOptions="Fill">
                        <CollectionView x:Name="receiveCollectionView" />
                    </ScrollView>
                </Frame>
            </Grid>
        </Frame>
    </Grid>
</ContentPage>

 

728x90

 

▶ MainPage.xaml.cs

using System.Collections.ObjectModel;

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 메시지 주제
    /// </summary>
    private readonly string MESSAGE_SUBJECT = "TEST_MESSAGE";

    /// <summary>
    /// 수신 컬렉션
    /// </summary>
    private ObservableCollection<string> receiveCollection = new ObservableCollection<string>();

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        this.receiveCollectionView.ItemsSource = this.receiveCollection;

        this.sendButton.Clicked        += sendButton_Clicked;
        this.subscribeButton.Clicked   += subscribeButton_Clicked;
        this.unsubscribeButton.Clicked += unsubscribeButton_Clicked;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 메시지 보내기 버튼 클릭시 처리하기 - sendButton_Clicked(sender, e)

    /// <summary>
    /// 메시지 보내기 버튼 클릭시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private async void sendButton_Clicked(object sender, EventArgs e)
    {
        string message = this.sendEntry.Text;

        if(string.IsNullOrEmpty(message))
        {
            await DisplayAlert("INFORMATION", "보낼 메시지를 입력해 주시기 바랍니다.", "확인");

            return;
        }

        MessagingCenter.Send<MainPage, string>(this, MESSAGE_SUBJECT, $"[{DateTime.Now:HH:mm:ss}] {message}");
    }

    #endregion
    #region 메시지 구독하기 버튼 클릭시 처리하기 - subscribeButton_Clicked(sender, e)

    /// <summary>
    /// 메시지 구독하기 버튼 클릭시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void subscribeButton_Clicked(object sender, EventArgs e)
    {
        MessagingCenter.Subscribe<MainPage, string>
        (
            this.subscribeButton,
            MESSAGE_SUBJECT,
            (sender, args) => { this.receiveCollection.Add(args); }
        );
    }

    #endregion
    #region 구독 취소하기 버튼 클릭시 처리하기 - unsubscribeButton_Clicked(sender, e)

    /// <summary>
    /// 구독 취소하기 버튼 클릭시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void unsubscribeButton_Clicked(object sender, EventArgs e)
    {
        MessagingCenter.Unsubscribe<MainPage, string>(this.subscribeButton, MESSAGE_SUBJECT);
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요