Skip to content

Commit 057099f

Browse files
Merge pull request #1 from SyncfusionExamples/applyTheme
How to apply OS level theme in .NET MAUI Applications for .NET MAUI DataGrid.
2 parents 091ee5a + 213940c commit 057099f

40 files changed

Lines changed: 1615 additions & 2 deletions

README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
# How-to-Apply-OS-Level-Theme-in-.NET-MAUI-Applications-with-.NET-MAUI-DataGrid
2-
This demo shows how to Apply OS-Level Theme in .NET MAUI Applications with .NET MAUI DataGrid
1+
# How to apply OS level theme in .NET MAUI Applications for .NET MAUI DataGrid?
2+
In this article, we will show you how to apply OS level theme in .NET MAUI Applications for [.Net Maui DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid).
3+
4+
## C#
5+
The below code sets the visual theme of the application based on the system's light or dark mode. It listens for theme changes and applies the appropriate Syncfusion visual theme accordingly when the app appears or the system theme changes.
6+
```
7+
public partial class MainPage : ContentPage
8+
{
9+
10+
public MainPage()
11+
{
12+
InitializeComponent();
13+
}
14+
15+
protected override void OnAppearing()
16+
{
17+
if (Application.Current != null)
18+
{
19+
this.ApplyTheme(Application.Current.RequestedTheme);
20+
Application.Current.RequestedThemeChanged += OnRequestedThemeChanged;
21+
}
22+
base.OnAppearing();
23+
}
24+
25+
private void OnRequestedThemeChanged(object? sender, AppThemeChangedEventArgs e)
26+
{
27+
this.ApplyTheme(e.RequestedTheme);
28+
}
29+
30+
public void ApplyTheme(AppTheme appTheme)
31+
{
32+
if (Application.Current != null)
33+
{
34+
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
35+
if (mergedDictionaries != null)
36+
{
37+
var syncTheme = mergedDictionaries.OfType<SyncfusionThemeResourceDictionary>().FirstOrDefault();
38+
if (syncTheme != null)
39+
{
40+
if (appTheme is AppTheme.Light)
41+
{
42+
syncTheme.VisualTheme = SfVisuals.MaterialLight;
43+
}
44+
else
45+
{
46+
syncTheme.VisualTheme = SfVisuals.MaterialDark;
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
```
54+
![ThemeDemo.gif](https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI5Njg0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.7Q7ibX2r45DjahI9XMnWYKqotSPPui6vL9l8LJ0nSU8)
55+
56+
[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-Apply-OS-Level-Theme-in-.NET-MAUI-Applications-with-.NET-MAUI-DataGrid)
57+
58+
Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
59+
60+
##### Conclusion
61+
62+
I hope you enjoyed learning about how to apply OS level theme in .NET MAUI Applications for .NET MAUI DataGrid (SfDataGrid).
63+
64+
You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data.
65+
For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components.
66+
67+
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you!

SfDataGridSample/App.xaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
xmlns:syncTheme="clr-namespace:Syncfusion.Maui.Themes;assembly=Syncfusion.Maui.Core"
6+
x:Class="SfDataGridSample.App" >
7+
<Application.Resources>
8+
<ResourceDictionary>
9+
<ResourceDictionary.MergedDictionaries>
10+
<syncTheme:SyncfusionThemeResourceDictionary />
11+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
12+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
13+
</ResourceDictionary.MergedDictionaries>
14+
</ResourceDictionary>
15+
</Application.Resources>
16+
</Application>

SfDataGridSample/App.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
12+
}

SfDataGridSample/AppShell.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDataGridSample.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDataGridSample"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="SfDataGridSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>

SfDataGridSample/AppShell.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}

SfDataGridSample/MainPage.xaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
5+
xmlns:local="clr-namespace:SfDataGridSample"
6+
x:Class="SfDataGridSample.MainPage">
7+
8+
<ContentPage.BindingContext>
9+
<local:EmployeeViewModel x:Name="viewModel"/>
10+
</ContentPage.BindingContext>
11+
12+
13+
<syncfusion:SfDataGrid x:Name="sfGrid"
14+
GridLinesVisibility="Both"
15+
ColumnWidthMode="Auto"
16+
SelectionMode="Single"
17+
NavigationMode="Cell"
18+
HeaderGridLinesVisibility="Both"
19+
AutoGenerateColumnsMode="None"
20+
ItemsSource="{Binding Employees}">
21+
22+
<syncfusion:SfDataGrid.Columns>
23+
<syncfusion:DataGridTextColumn MappingName="EmployeeID" Format="#" />
24+
<syncfusion:DataGridTextColumn MappingName="Name" />
25+
<syncfusion:DataGridTextColumn MappingName="Title" />
26+
<syncfusion:DataGridDateColumn MappingName="HireDate" />
27+
</syncfusion:SfDataGrid.Columns>
28+
</syncfusion:SfDataGrid>
29+
30+
31+
32+
</ContentPage>

SfDataGridSample/MainPage.xaml.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Syncfusion.Maui.Themes;
2+
3+
namespace SfDataGridSample
4+
{
5+
public partial class MainPage : ContentPage
6+
{
7+
8+
public MainPage()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
protected override void OnAppearing()
14+
{
15+
if (Application.Current != null)
16+
{
17+
this.ApplyTheme(Application.Current.RequestedTheme);
18+
Application.Current.RequestedThemeChanged += OnRequestedThemeChanged;
19+
}
20+
base.OnAppearing();
21+
}
22+
23+
private void OnRequestedThemeChanged(object? sender, AppThemeChangedEventArgs e)
24+
{
25+
this.ApplyTheme(e.RequestedTheme);
26+
}
27+
28+
public void ApplyTheme(AppTheme appTheme)
29+
{
30+
if (Application.Current != null)
31+
{
32+
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
33+
if (mergedDictionaries != null)
34+
{
35+
var syncTheme = mergedDictionaries.OfType<SyncfusionThemeResourceDictionary>().FirstOrDefault();
36+
if (syncTheme != null)
37+
{
38+
if (appTheme is AppTheme.Light)
39+
{
40+
syncTheme.VisualTheme = SfVisuals.MaterialLight;
41+
}
42+
else
43+
{
44+
syncTheme.VisualTheme = SfVisuals.MaterialDark;
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
}

SfDataGridSample/MauiProgram.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core;
3+
using Syncfusion.Maui.Core.Hosting;
4+
namespace SfDataGridSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)