2
0
Files
maui-linux-samples/ShellDemo/Pages/HomePage.xaml.cs

155 lines
5.3 KiB
C#
Raw Normal View History

2026-01-11 12:33:48 -05:00
// HomePage - Welcome page for the demo
using Microsoft.Maui.Controls;
2026-01-11 13:34:36 -05:00
using Microsoft.Maui.Controls.Shapes;
2026-01-11 12:33:48 -05:00
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform.Linux.Hosting;
namespace ShellDemo;
public partial class HomePage : ContentPage
{
2026-01-24 03:17:48 +00:00
private bool _isUpdatingThemeSwitch;
2026-01-11 12:33:48 -05:00
public HomePage()
{
InitializeComponent();
CreateFeatureCards();
2026-01-24 03:17:48 +00:00
UpdateThemeSwitchState();
}
protected override void OnAppearing()
{
base.OnAppearing();
UpdateThemeSwitchState();
}
private void UpdateThemeSwitchState()
{
_isUpdatingThemeSwitch = true;
var isDark = Application.Current?.UserAppTheme == AppTheme.Dark ||
(Application.Current?.UserAppTheme == AppTheme.Unspecified &&
Application.Current?.RequestedTheme == AppTheme.Dark);
ThemeSwitch.IsToggled = isDark;
_isUpdatingThemeSwitch = false;
}
private void OnThemeSwitchToggled(object? sender, ToggledEventArgs e)
{
if (_isUpdatingThemeSwitch || Application.Current == null) return;
Application.Current.UserAppTheme = e.Value ? AppTheme.Dark : AppTheme.Light;
2026-01-11 12:33:48 -05:00
}
private void CreateFeatureCards()
{
2026-01-24 03:17:48 +00:00
// Title, Description, Route
2026-01-11 12:33:48 -05:00
var features = new[]
{
2026-01-24 03:17:48 +00:00
("Buttons", "Various button styles and events", "Buttons"),
("Text Input", "Entry, Editor, SearchBar", "TextInput"),
("Selection", "CheckBox, Switch, Slider", "Selection"),
("Pickers", "Picker, DatePicker, TimePicker", "Pickers"),
("Lists", "CollectionView with selection", "Lists"),
("Progress", "ProgressBar, ActivityIndicator", "Progress")
2026-01-11 12:33:48 -05:00
};
for (int i = 0; i < features.Length; i++)
{
2026-01-24 03:17:48 +00:00
var (title, desc, route) = features[i];
var card = CreateFeatureCard(title, desc, route);
2026-01-11 12:33:48 -05:00
Grid.SetRow(card, i / 2);
Grid.SetColumn(card, i % 2);
FeatureGrid.Children.Add(card);
}
}
2026-01-24 03:17:48 +00:00
private Border CreateFeatureCard(string title, string description, string route)
2026-01-11 12:33:48 -05:00
{
// Use AppThemeBinding for card colors
var cardBackground = new AppThemeBindingExtension
{
Light = Application.Current?.Resources["CardBackgroundLight"] as Color ?? Colors.White,
Dark = Application.Current?.Resources["CardBackgroundDark"] as Color ?? Color.FromArgb("#1E1E1E")
};
var textPrimary = new AppThemeBindingExtension
{
Light = Application.Current?.Resources["TextPrimaryLight"] as Color ?? Colors.Black,
Dark = Application.Current?.Resources["TextPrimaryDark"] as Color ?? Colors.White
};
var textSecondary = new AppThemeBindingExtension
{
Light = Application.Current?.Resources["TextSecondaryLight"] as Color ?? Colors.Gray,
Dark = Application.Current?.Resources["TextSecondaryDark"] as Color ?? Color.FromArgb("#B0B0B0")
};
var titleLabel = new Label
{
Text = title,
FontSize = 14,
FontAttributes = FontAttributes.Bold,
TextColor = Color.FromArgb("#2196F3")
};
var descLabel = new Label
{
Text = description,
FontSize = 11,
LineBreakMode = LineBreakMode.WordWrap
};
descLabel.SetBinding(Label.TextColorProperty, new Binding { Source = textSecondary, Path = "." });
descLabel.SetAppThemeColor(Label.TextColorProperty,
Application.Current?.Resources["TextSecondaryLight"] as Color ?? Colors.Gray,
Application.Current?.Resources["TextSecondaryDark"] as Color ?? Color.FromArgb("#B0B0B0"));
var border = new Border
{
Padding = new Thickness(15),
StrokeThickness = 0,
StrokeShape = new RoundRectangle { CornerRadius = 8 },
Shadow = new Shadow
{
Brush = new SolidColorBrush(Colors.Black),
Opacity = 0.1f,
Radius = 4,
Offset = new Point(0, 2)
},
Content = new VerticalStackLayout
{
Spacing = 5,
Children = { titleLabel, descLabel }
}
};
border.SetAppThemeColor(Border.BackgroundColorProperty,
Application.Current?.Resources["CardBackgroundLight"] as Color ?? Colors.White,
Application.Current?.Resources["CardBackgroundDark"] as Color ?? Color.FromArgb("#1E1E1E"));
2026-01-24 03:17:48 +00:00
// Add tap gesture for navigation
var tapGesture = new TapGestureRecognizer();
tapGesture.Tapped += (s, e) => LinuxViewRenderer.NavigateToRoute(route);
border.GestureRecognizers.Add(tapGesture);
2026-01-11 12:33:48 -05:00
return border;
}
private void OnTryButtonsClicked(object? sender, EventArgs e)
{
LinuxViewRenderer.NavigateToRoute("Buttons");
}
private void OnTryListsClicked(object? sender, EventArgs e)
{
LinuxViewRenderer.NavigateToRoute("Lists");
}
private void OnPushDetailClicked(object? sender, EventArgs e)
{
Console.WriteLine("[HomePage] Push button clicked, navigating to detail");
var success = LinuxViewRenderer.PushPage(new DetailPage());
Console.WriteLine($"[HomePage] PushPage result: {success}");
}
}