[C#/MAUI/.NET6] Application 클래스 : RequestedThemeChanged 이벤트를 사용해 현재 테마 변경시 처리하기
C#/MAUI 2022. 6. 5. 22:50728x90
728x170
▶ Platforms/Android/MainActivity.cs
using Android.App;
using Android.Content.PM;
using Android.OS;
namespace TestProject;
/// <summary>
/// 메인 액티비티
/// </summary>
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
728x90
▶ 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">
<Label x:Name="label"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
FontSize="48" />
</ContentPage>
300x250
▶ MainPage.xaml.cs
namespace TestProject;
/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainPage()
/// <summary>
/// 생성자
/// </summary>
public MainPage()
{
InitializeComponent();
Application application = Application.Current;
application.RequestedThemeChanged += application_RequestedThemeChanged;
Loaded += ContentPage_Loaded;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 애플리케이션 요청 테마 변경시 처리하기 - application_RequestedThemeChanged(sender, e)
/// <summary>
/// 애플리케이션 요청 테마 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void application_RequestedThemeChanged(object sender, AppThemeChangedEventArgs e)
{
ChangeTheme(e.RequestedTheme);
}
#endregion
#region 컨텐트 페이지 로드시 처리하기 - ContentPage_Loaded(sender, e)
/// <summary>
/// 컨텐트 페이지 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void ContentPage_Loaded(object sender, EventArgs e)
{
ChangeTheme(Application.Current.RequestedTheme);
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 테마 변경하기 - ChangeTheme(theme)
/// <summary>
/// 테마 변경하기
/// </summary>
/// <param name="theme">테마</param>
private void ChangeTheme(AppTheme theme)
{
if(theme == AppTheme.Light)
{
BackgroundColor = Colors.White;
this.label.TextColor = Colors.Black;
}
else
{
BackgroundColor = Colors.Black;
this.label.TextColor = Colors.White;
}
this.label.Text = theme.ToString();
}
#endregion
}
728x90
그리드형(광고전용)