Files
maui-linux/samples/XamlBrowser/MainPage.xaml.cs
Dave Friedel 8a36a44341 Reconstruct XamlBrowser sample with XAML from decompiled code
Created complete XamlBrowser sample application:
- App.xaml: Colors and styles for light/dark theme support
- App.xaml.cs: BrowserApp with ToggleTheme()
- MainPage.xaml: Toolbar (Back, Forward, Refresh, Stop, Home),
  address bar, Go button, WebView, status bar with theme toggle
- MainPage.xaml.cs: Navigation logic, URL handling, progress animation
- MauiProgram.cs: UseLinuxPlatform() configuration
- Program.cs: LinuxProgramHost entry point
- Resources/Images: 10 SVG icons for toolbar (dark/light variants)

UI matches screenshot provided by user:
- Dark gray toolbar with navigation buttons
- Entry field for URL with rounded corners
- Green "Go" button
- WebView displaying content
- Status bar with theme toggle

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 18:32:08 -05:00

129 lines
3.7 KiB
C#

using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
namespace XamlBrowser;
public partial class MainPage : ContentPage
{
private const string HomeUrl = "https://openmaui.net";
public MainPage()
{
InitializeComponent();
AddressBar.Text = HomeUrl;
}
private void OnBackClicked(object? sender, EventArgs e)
{
Console.WriteLine($"[MainPage] OnBackClicked, CanGoBack={BrowserWebView.CanGoBack}");
if (BrowserWebView.CanGoBack)
{
BrowserWebView.GoBack();
}
}
private void OnForwardClicked(object? sender, EventArgs e)
{
Console.WriteLine($"[MainPage] OnForwardClicked, CanGoForward={BrowserWebView.CanGoForward}");
if (BrowserWebView.CanGoForward)
{
BrowserWebView.GoForward();
}
}
private void OnRefreshClicked(object? sender, EventArgs e)
{
Console.WriteLine("[MainPage] OnRefreshClicked");
BrowserWebView.Reload();
}
private void OnStopClicked(object? sender, EventArgs e)
{
LoadingProgress.IsVisible = false;
StatusLabel.Text = "Stopped";
}
private void OnHomeClicked(object? sender, EventArgs e)
{
NavigateTo(HomeUrl);
}
private void OnAddressBarCompleted(object? sender, EventArgs e)
{
NavigateTo(AddressBar.Text);
}
private void OnGoClicked(object? sender, EventArgs e)
{
NavigateTo(AddressBar.Text);
}
private void NavigateTo(string? url)
{
if (string.IsNullOrWhiteSpace(url))
return;
// Add protocol if missing
if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
// If it looks like a URL (contains dot and no spaces), add https
// Otherwise treat as a search query
url = url.Contains('.') && !url.Contains(' ')
? "https://" + url
: "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
}
AddressBar.Text = url;
BrowserWebView.Source = new UrlWebViewSource { Url = url };
}
private void OnWebViewNavigating(object? sender, WebNavigatingEventArgs e)
{
Console.WriteLine("[MainPage] Navigating to: " + e.Url);
StatusLabel.Text = $"Loading {e.Url}...";
// Reset and show progress bar with animation
LoadingProgress.AbortAnimation("Progress");
LoadingProgress.Progress = 0;
LoadingProgress.IsVisible = true;
// Animate progress from 0 to 90%
LoadingProgress.Animate("Progress",
new Animation(v => LoadingProgress.Progress = v, 0, 0.9),
length: 2000,
easing: Easing.CubicOut);
AddressBar.Text = e.Url;
}
private void OnWebViewNavigated(object? sender, WebNavigatedEventArgs e)
{
Console.WriteLine($"[MainPage] Navigated: {e.Url} - Result: {e.Result}");
StatusLabel.Text = e.Result == WebNavigationResult.Success ? "Done" : $"Error: {e.Result}";
// Complete progress bar
LoadingProgress.AbortAnimation("Progress");
LoadingProgress.Progress = 1;
AddressBar.Text = e.Url;
// Hide progress bar after a short delay
Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(300), () =>
{
LoadingProgress.IsVisible = false;
LoadingProgress.Progress = 0;
});
}
private void OnThemeToggleClicked(object? sender, EventArgs e)
{
if (Application.Current is BrowserApp app)
{
app.ToggleTheme();
Console.WriteLine($"[MainPage] Theme changed to: {Application.Current.UserAppTheme}");
}
}
}