-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from SyncfusionExamples/Sample
Added the sample for Tab View KB.
- Loading branch information
Showing
37 changed files
with
1,233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,83 @@ | ||
# How-to-Center-Align-Tabs-in-.NET-MAUI-TabView-for-Windows-and-Mac-platforms | ||
This repository contains a sample explaining how to center align tabs in .NET MAUI TabView for Windows and Mac platforms. Tokens: maui, maui-tab-view, tab-view, tab-alignment | ||
This article guides you on how to center align tabs in [.NET MAUI TabView](https://www.syncfusion.com/maui-controls/maui-tab-view) for Windows and Mac platforms. | ||
|
||
To customize the placement of tabs to the center of the Windows and Mac screens, set [TabWidthMode](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_TabWidthMode) to [SizeToContent](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.TabWidthMode.html#Syncfusion_Maui_TabView_TabWidthMode_SizeToContent) and adjust [TabHeaderPadding](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_TabHeaderPadding) dynamically based on the screen's width. Below is a code snippet demonstrating this approach. | ||
|
||
**XAML Code** | ||
|
||
```xaml | ||
<tabView:SfTabView TabWidthMode="SizeToContent"> | ||
… | ||
<\tabView:SfTabView> | ||
``` | ||
|
||
**C#** | ||
|
||
```csharp | ||
public partial class MainPage : ContentPage | ||
{ | ||
public MainPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
protected override void OnAppearing() | ||
{ | ||
base.OnAppearing(); | ||
|
||
#if WINDOWS | ||
var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow; | ||
if (window != null) | ||
{ | ||
UpdateTabHeaderPadding(window.Bounds.Width); | ||
} | ||
#elif MACCATALYST | ||
var macWindow = GetParentWindow().Handler.PlatformView as UIWindow; | ||
if (macWindow != null) | ||
{ | ||
UpdateTabHeaderPadding(macWindow.Bounds.Width); | ||
} | ||
#endif | ||
} | ||
|
||
#if WINDOWS || MACCATALYST | ||
protected override void OnSizeAllocated(double width, double height) | ||
{ | ||
base.OnSizeAllocated(width, height); | ||
UpdateTabHeaderPadding(width); | ||
} | ||
private void UpdateTabHeaderPadding(double width) | ||
{ | ||
double totalTabWidth = 0; | ||
foreach (var tabItem in tabView.Items) | ||
{ | ||
if (tabItem is Syncfusion.Maui.TabView.SfTabItem tabItemView) | ||
{ | ||
totalTabWidth += tabItemView.Measure(double.PositiveInfinity, double.PositiveInfinity).Request.Width; | ||
} | ||
} | ||
double remainingSpace = ((width - totalTabWidth)); | ||
|
||
double padding = (remainingSpace / 2) ; | ||
|
||
tabView.TabHeaderPadding = new Thickness(padding, 0, 0, 0); | ||
} | ||
#endif | ||
} | ||
``` | ||
|
||
- The TabWidthMode is set to SizeToContent so that each tab's width is adjusted based on its content. | ||
- The method `UpdateTabHeaderPadding` calculates the total width of all the tab items and dynamically adjusts the left padding of the tab headers to center them based on the screen width. | ||
|
||
**Output** | ||
|
||
![TabView_TabAlignment.gif](https://support.syncfusion.com/kb/agent/attachment/article/17398/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI5MzYwIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.99mAjs80Wwjabr15ABNoC5JX8l7RrCo36DsNBR5P4is) | ||
|
||
**Requirements to run the demo** | ||
|
||
To run the demo, refer to [System Requirements for .NET MAUI](https://help.syncfusion.com/maui/system-requirements) | ||
|
||
**Troubleshooting:** | ||
|
||
**Path too long exception** | ||
|
||
If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.8.34309.116 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TabViewSample", "TabViewSample\TabViewSample.csproj", "{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
Release-Xml|Any CPU = Release-Xml|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.Deploy.0 = Debug|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.Deploy.0 = Release|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.Build.0 = Release|Any CPU | ||
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.Deploy.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {B69CA809-EB64-4829-B9A8-BA7F393E582F} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version = "1.0" encoding = "UTF-8" ?> | ||
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:local="clr-namespace:TabViewSample" | ||
x:Class="TabViewSample.App"> | ||
<Application.Resources> | ||
<ResourceDictionary> | ||
<ResourceDictionary.MergedDictionaries> | ||
<ResourceDictionary Source="Resources/Styles/Colors.xaml" /> | ||
<ResourceDictionary Source="Resources/Styles/Styles.xaml" /> | ||
</ResourceDictionary.MergedDictionaries> | ||
</ResourceDictionary> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace TabViewSample | ||
{ | ||
public partial class App : Application | ||
{ | ||
public App() | ||
{ | ||
InitializeComponent(); | ||
|
||
MainPage = new AppShell(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<Shell | ||
x:Class="TabViewSample.AppShell" | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:local="clr-namespace:TabViewSample" | ||
Shell.FlyoutBehavior="Disabled" | ||
Title="TabViewSample"> | ||
|
||
<ShellContent | ||
Title="Home" | ||
ContentTemplate="{DataTemplate local:MainPage}" | ||
Route="MainPage" /> | ||
|
||
</Shell> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace TabViewSample | ||
{ | ||
public partial class AppShell : Shell | ||
{ | ||
public AppShell() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#if WINDOWS | ||
using Microsoft.UI.Windowing; | ||
#elif MACCATALYST | ||
using UIKit; | ||
#endif | ||
|
||
namespace TabViewSample | ||
{ | ||
public partial class MainPage : ContentPage | ||
{ | ||
public MainPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
protected override void OnAppearing() | ||
{ | ||
base.OnAppearing(); | ||
|
||
#if WINDOWS | ||
var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow; | ||
if (window != null) | ||
{ | ||
UpdateTabHeaderPadding(window.Bounds.Width); | ||
} | ||
#elif MACCATALYST | ||
var macWindow = GetParentWindow().Handler.PlatformView as UIWindow; | ||
if (macWindow != null) | ||
{ | ||
UpdateTabHeaderPadding(macWindow.Bounds.Width); | ||
} | ||
#endif | ||
} | ||
|
||
#if WINDOWS || MACCATALYST | ||
protected override void OnSizeAllocated(double width, double height) | ||
{ | ||
base.OnSizeAllocated(width, height); | ||
UpdateTabHeaderPadding(width); | ||
} | ||
private void UpdateTabHeaderPadding(double width) | ||
{ | ||
double totalTabWidth = 0; | ||
foreach (var tabItem in tabView.Items) | ||
{ | ||
if (tabItem is Syncfusion.Maui.TabView.SfTabItem tabItemView) | ||
{ | ||
totalTabWidth += tabItemView.Measure(double.PositiveInfinity, double.PositiveInfinity).Request.Width; | ||
} | ||
} | ||
double remainingSpace = ((width - totalTabWidth)); | ||
|
||
double padding = (remainingSpace / 2) ; | ||
|
||
tabView.TabHeaderPadding = new Thickness(padding, 0, 0, 0); | ||
} | ||
#endif | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Syncfusion.Maui.Core.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace TabViewSample | ||
{ | ||
public static class MauiProgram | ||
{ | ||
public static MauiApp CreateMauiApp() | ||
{ | ||
var builder = MauiApp.CreateBuilder(); | ||
builder | ||
.UseMauiApp<App>() | ||
.ConfigureSyncfusionCore() | ||
.ConfigureFonts(fonts => | ||
{ | ||
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); | ||
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); | ||
}); | ||
|
||
#if DEBUG | ||
builder.Logging.AddDebug(); | ||
#endif | ||
|
||
return builder.Build(); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
TabViewSample/TabViewSample/Platforms/Android/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
11 changes: 11 additions & 0 deletions
11
TabViewSample/TabViewSample/Platforms/Android/MainActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Android.App; | ||
using Android.Content.PM; | ||
using Android.OS; | ||
|
||
namespace TabViewSample | ||
{ | ||
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] | ||
public class MainActivity : MauiAppCompatActivity | ||
{ | ||
} | ||
} |
Oops, something went wrong.