Files
RedLintu16andClaude Opus 5.5 63b784bfd1 Log unhandled exceptions to widget.log
Write the crash synchronously from App.UnhandledException so a startup
crash leaves its exception in LocalState\widget.log.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 20:36:26 -07:00

95 lines
3.7 KiB
C#

using System;
using Microsoft.Gaming.XboxGameBar;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace CrossSoundWidget
{
sealed partial class App : Application
{
/// <summary>Must equal the AppExtension Id in Package.appxmanifest.</summary>
private const string WidgetExtensionId = "CrossSoundMedia";
private XboxGameBarWidget _widget;
public App()
{
InitializeComponent();
Suspending += OnSuspending;
UnhandledException += (s, e) => Services.Log.WriteNow("CRASH " + e.Exception);
}
/// <summary>Game Bar activates the widget through the ms-gamebarwidget protocol.</summary>
protected override void OnActivated(IActivatedEventArgs args)
{
XboxGameBarWidgetActivatedEventArgs widgetArgs = null;
if (args.Kind == ActivationKind.Protocol)
{
var protocolArgs = args as IProtocolActivatedEventArgs;
if (protocolArgs != null && protocolArgs.Uri.Scheme.Equals("ms-gamebarwidget", StringComparison.OrdinalIgnoreCase))
{
widgetArgs = args as XboxGameBarWidgetActivatedEventArgs;
}
}
if (widgetArgs == null || !widgetArgs.IsLaunchActivation) return;
if (!string.Equals(widgetArgs.AppExtensionId, WidgetExtensionId, StringComparison.Ordinal)) return;
var rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
_widget = new XboxGameBarWidget(widgetArgs, Window.Current.CoreWindow, rootFrame);
// The page receives the widget as its navigation parameter (OnNavigatedTo).
rootFrame.Navigate(typeof(MediaWidget), _widget);
Window.Current.Closed += OnWidgetWindowClosed;
Window.Current.Activate();
}
/// <summary>
/// Started from the Start menu instead of Game Bar: show the same page in a normal window
/// (no widget object), which is handy for testing.
/// </summary>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated) return;
if (rootFrame.Content == null) rootFrame.Navigate(typeof(MediaWidget), null);
// Roughly the widget's own size.
var widgetSize = new Size(300, 480);
ApplicationView.PreferredLaunchViewSize = widgetSize;
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
Window.Current.Activate();
ApplicationView.GetForCurrentView().TryResizeView(widgetSize);
}
private void OnWidgetWindowClosed(object sender, Windows.UI.Core.CoreWindowEventArgs e)
{
_widget = null;
Window.Current.Closed -= OnWidgetWindowClosed;
}
private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
deferral.Complete();
}
}
}