■ MessageCenter 클래스의 Subscribe/Unsubscribe/Send 메소드를 사용해 메시지를 송신/수신하는 방법을 보여준다.
▶ 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 |
<?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> |
▶ 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 |
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 } |