■ Snackbar 클래스에서 스낵바 표시시 애플리케이션 새 인스턴스 실행을 방지하는 방법을 보여준다. (UWP)
▶ Platforms/Windows/App.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.Diagnostics; using System.Threading; using Microsoft.UI.Xaml; namespace TestProject.WinUI; /// <summary> /// 앱 /// </summary> public partial class App : MauiWinUIApplication { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 뮤텍스 /// </summary> private static Mutex _mutex = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - App() /// <summary> /// 생성자 /// </summary> public App() { this.InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 단일 인스턴스 여부 구하기 - IsSingleInstance() /// <summary> /// 단일 인스턴스 여부 구하기 /// </summary> /// <returns>단일 인스턴스 여부</returns> private static bool IsSingleInstance() { const string applicationID = "AF57FC2A-CAEF-466F-8D15-651A418258BE"; _mutex = new Mutex(false, applicationID); GC.KeepAlive(_mutex); try { return _mutex.WaitOne(0, false); } catch(AbandonedMutexException) { _mutex.ReleaseMutex(); return _mutex.WaitOne(0, false); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region MAUI 앱 생성하기 - CreateMauiApp() /// <summary> /// MAUI 앱 생성하기 /// </summary> /// <returns>MAUI 앱</returns> protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); #endregion #region 시작시 처리하기 - OnLaunched(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { if(!IsSingleInstance()) { Process.GetCurrentProcess().Kill(); } else { base.OnLaunched(e); } } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 |
<?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"> <Button x:Name="showButton" HorizontalOptions="Center" VerticalOptions="Center" Text="스낵바 표시하기" /> </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 |
using CommunityToolkit.Maui.Alerts; using CommunityToolkit.Maui.Core; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.showButton.Clicked += showButton_Clicked; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스낵바 표시 버튼 클릭시 처리하기 - showButton_Clicked(sender, e) /// <summary> /// 스낵바 표시 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void showButton_Clicked(object sender, EventArgs e) { SnackbarOptions options = new SnackbarOptions { CornerRadius = new CornerRadius(10), BackgroundColor = Colors.Blue, CharacterSpacing = 0, Font = Microsoft.Maui.Font.SystemFontOfSize(16), ActionButtonFont = Microsoft.Maui.Font.SystemFontOfSize(14), TextColor = Colors.White, ActionButtonTextColor = Colors.White }; Action action = async () => await DisplayAlert ( "INFORMATION", "사용자가 스낵바 액션 버튼을 탭했습니다.", "OK" ); ISnackbar snackbar = Snackbar.Make("스낵바 입니다.", action, "종료", TimeSpan.FromSeconds(3), options); await snackbar.Show(); } #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 } |