Skip to content

Commit

Permalink
Merge pull request #54 from zxbmmmmmmmmm/gamebar
Browse files Browse the repository at this point in the history
GameBar widget
  • Loading branch information
zxbmmmmmmmmm authored Jul 14, 2024
2 parents 9a364cf + c865dd3 commit b34b062
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 1 deletion.
45 changes: 45 additions & 0 deletions FluentWeather.Uwp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using FluentWeather.Uwp.Shared.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Gaming.XboxGameBar;

namespace FluentWeather.Uwp;

Expand Down Expand Up @@ -65,6 +66,8 @@ public App()
#endif

}
private XboxGameBarWidget _widget = null;

public static string ActiveArguments { get; private set; }
public static async void RegisterBackgroundTask()
{
Expand Down Expand Up @@ -105,6 +108,42 @@ private void OnUnhandledException(object sender, Windows.UI.Xaml.UnhandledExcept
}
protected override void OnActivated(IActivatedEventArgs e)
{
XboxGameBarWidgetActivatedEventArgs widgetArgs = null;
if (e.Kind == ActivationKind.Protocol)
{
var protocolArgs = e as IProtocolActivatedEventArgs;
string scheme = protocolArgs.Uri.Scheme;
if (scheme.Equals("ms-gamebarwidget"))
{
widgetArgs = e as XboxGameBarWidgetActivatedEventArgs;
}
if (scheme.Equals("weather")){

}
}
if (widgetArgs != null)
{
if (widgetArgs.IsLaunchActivation)
{
var widgetFrame = new Frame();
widgetFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = widgetFrame;

_widget = new XboxGameBarWidget(
widgetArgs,
Window.Current.CoreWindow,
widgetFrame);
widgetFrame.Navigate(typeof(WidgetPage),_widget);

Window.Current.Closed += OnWigdetClosed; ;
Window.Current.Activate();
}
else
{
// You can perform whatever behavior you need based on the URI payload.
}
}

Frame rootFrame = Window.Current.Content as Frame;
RegisterBackgroundTask();
if (rootFrame == null)
Expand All @@ -131,6 +170,12 @@ protected override void OnActivated(IActivatedEventArgs e)
// 确保当前窗口处于活动状态
Window.Current.Activate();
}

private void OnWigdetClosed(object sender, Windows.UI.Core.CoreWindowEventArgs e)
{
throw new NotImplementedException();
}

/// <summary>
/// 在应用程序由最终用户正常启动时进行调用。
/// 将在启动应用程序以打开特定文件等情况下使用。
Expand Down
10 changes: 10 additions & 0 deletions FluentWeather.Uwp/FluentWeather.Uwp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@
<Compile Include="Pages\WelcomePage.xaml.cs">
<DependentUpon>WelcomePage.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\WidgetPage.xaml.cs">
<DependentUpon>WidgetPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Themes\Generic.xaml.cs">
<DependentUpon>Generic.xaml</DependentUpon>
Expand Down Expand Up @@ -580,6 +583,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Pages\WidgetPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\ContentDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -643,6 +650,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Gaming.XboxGameBar">
<Version>6.1.240122001</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.14</Version>
</PackageReference>
Expand Down
63 changes: 63 additions & 0 deletions FluentWeather.Uwp/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
IgnorableNamespaces="uap mp">

<Identity
Expand Down Expand Up @@ -58,10 +59,72 @@
<Task Type="timer"/>
</BackgroundTasks>
</Extension>
<uap3:Extension Category="windows.appExtension">
<uap3:AppExtension Name="microsoft.gameBarUIExtension"
Id="WeatherWidget"
DisplayName="Skyline"
Description="Skyline weather widget"
PublicFolder="GameBar">
<uap3:Properties>
<GameBarWidget Type="Standard">
<HomeMenuVisible>true</HomeMenuVisible>
<PinningSupported>true</PinningSupported>
<Window>
<Size>
<Height>300</Height>
<Width>400</Width>
</Size>
<ResizeSupported>
<Horizontal>true</Horizontal>
<Vertical>true</Vertical>
</ResizeSupported>
</Window>
</GameBarWidget>
</uap3:Properties>
</uap3:AppExtension>
</uap3:Extension>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="weather">
<uap:Logo>Assets\Logo.png</uap:Logo>
<uap:DisplayName>ms-resource:AppDisplayName</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
</Application>
</Applications>

<Extensions>
<!-- Enlighten COM on where to find Metadata Based Marshaling (MBM) data for the Game Bar private types
<Path> is a required element (by VS) and has to point to a binary in the package, but it's not used when the class id is
00000355-0000-0000-C000-000000000046 (MBM). Due to that we just put the Microsoft.Gaming.XboxGameBar.winmd here. -->
<Extension Category="windows.activatableClass.proxyStub">
<ProxyStub ClassId="00000355-0000-0000-C000-000000000046">
<Path>Microsoft.Gaming.XboxGameBar.winmd</Path>
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarAppTargetHost" InterfaceId="38CDC43C-0A0E-4B3B-BBD3-A581AE220D53" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarAppTargetInfo" InterfaceId="D7689E93-5587-47D1-A42E-78D16B2FA807" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarActivityHost" InterfaceId="2B113C9B-E370-49B2-A20B-83E0F5737577" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarHotkeyManagerHost" InterfaceId="F6225A53-B34C-4833-9511-AA377B43316F" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetAuthHost" InterfaceId="DC263529-B12F-469E-BB35-B94069F5B15A" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetControlHost" InterfaceId="C309CAC7-8435-4082-8F37-784523747047" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetForegroundWorkerHost" InterfaceId="DDB52B57-FA83-420C-AFDE-6FA556E18B83" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetForegroundWorkerPrivate" InterfaceId="42BACDFC-BB28-4E71-99B4-24C034C7B7E0" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarNavigationKeyCombo" InterfaceId="5EEA3DBF-09BB-42A5-B491-CF561E33C172" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetActivatedEventArgsPrivate" InterfaceId="782535A7-9407-4572-BFCB-316B4086F102" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost" InterfaceId="5D12BC93-212B-4B9F-9091-76B73BF56525" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost2" InterfaceId="28717C8B-D8E8-47A8-AF47-A1D5263BAE9B" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost3" InterfaceId="3F5A3F12-C1E4-4942-B80D-3117BC948E29" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost4" InterfaceId="FA696D9E-2501-4B01-B26F-4BB85344740F" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost5" InterfaceId="A6C878CC-2B08-4B94-B1C3-222C6A913F3C" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetHost6" InterfaceId="CE6F0D73-C44F-4BBD-9652-A0FC52C37A34" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetNotificationHost" InterfaceId="6F68D392-E4A9-46F7-A024-5275BC2FE7BA" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetNotificationPrivate" InterfaceId="C94C8DC8-C8B5-4560-AF6E-A588B558213A" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetPrivate" InterfaceId="22ABA97F-FB0F-4439-9BDD-2C67B2D5AA8F" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetPrivate2" InterfaceId="B2F7DB8C-7540-48DA-9B46-4E60CE0D9DEB" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetPrivate3" InterfaceId="4FB89FB6-7CB8-489D-8408-2269E6C733A1" />
<Interface Name="Microsoft.Gaming.XboxGameBar.Private.IXboxGameBarWidgetPrivate4" InterfaceId="5638D65A-3733-48CC-90E5-984688D62786" />
</ProxyStub>
</Extension>
</Extensions>
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="location"/>
Expand Down
8 changes: 7 additions & 1 deletion FluentWeather.Uwp/Pages/CitiesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public CitiesPage()
this.InitializeComponent();
this.DataContext = this;
this.NavigationCacheMode = NavigationCacheMode.Required;

}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
CurrentCityView.SelectionChanged += CurrentCityView_SelectionChanged;
CitiesView.SelectionChanged += CitiesView_SelectionChanged;
if (!App.ActiveArguments.Contains("City_"))
if (App.ActiveArguments is null)
{
SetSelectedLocation(Common.Settings.DefaultGeolocation?.Name);
return;
Expand Down
52 changes: 52 additions & 0 deletions FluentWeather.Uwp/Pages/WidgetPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<Page
x:Class="FluentWeather.Uwp.Pages.WidgetPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:FluentWeather.Uwp.Behaviors"
xmlns:behaviors1="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
xmlns:chart="using:Telerik.UI.Xaml.Controls.Chart"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:controls1="using:FluentWeather.Uwp.Controls"
xmlns:converters="using:ValueConverters"
xmlns:converters1="using:FluentWeather.Uwp.Shared.Helpers.ValueConverters"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="using:Microsoft.Xaml.Interactivity"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True"
xmlns:local="using:FluentWeather.Uwp.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d">
<Grid Padding="12,2,0,3" x:DefaultBindMode="OneWay">
<Button x:Name="WeatherButton" Click="WeatherButton_Click" IsEnabled="{x:Bind ViewModel.GetWeatherCommand.IsRunning,Converter={StaticResource BoolNegationConverter}}" VerticalAlignment="Bottom" Background="Transparent" BorderBrush="Transparent">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="DoubleTapped">
<core:InvokeCommandAction Command="{x:Bind ViewModel.RefreshCommand}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<StackPanel

Orientation="Horizontal" Margin="0,0,0,0"
Spacing="8">
<Image
Width="28"
Height="28"
Source="{x:Bind converters1:ConverterMethods.GetIconByWeather(ViewModel.WeatherNow.WeatherType)}" />
<StackPanel Spacing="0" VerticalAlignment="Center">
<TextBlock Margin="0,0,0,0" >
<Run Text="{x:Bind converters1:ConverterMethods.ConvertTemperatureUnit(ViewModel.WeatherNow.Temperature)}" /><Run Text="°" />
<Run Text="{x:Bind ViewModel.WeatherNow.Description}"/>
</TextBlock>
</StackPanel>

</StackPanel>
<Button.ContextFlyout>
<Flyout x:Name="DetailFlyout">
<StackPanel>
<TextBlock Text="{x:Bind ViewModel.WeatherNow.Description}"/>
</StackPanel>
</Flyout>
</Button.ContextFlyout>
</Button>
</Grid>
</Page>
50 changes: 50 additions & 0 deletions FluentWeather.Uwp/Pages/WidgetPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentWeather.Uwp.Shared;
using FluentWeather.Uwp.ViewModels;
using Microsoft.Gaming.XboxGameBar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


namespace FluentWeather.Uwp.Pages;

public sealed partial class WidgetPage : Page
{
public MainPageViewModel ViewModel { get; set; } = new();
private readonly DispatcherTimer _timer = new (){ Interval = TimeSpan.FromMinutes(20)};
private XboxGameBarWidget _widget;

public WidgetPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ViewModel.CurrentGeolocation = Common.Settings.DefaultGeolocation!;
_widget = e.Parameter as XboxGameBarWidget;
_timer.Tick += OnTimerTicked;
_timer.Start();
}

private async void OnTimerTicked(object sender, object e)
{
await ViewModel.RefreshCommand.ExecuteAsync(null);
}

private async void WeatherButton_Click(object sender, RoutedEventArgs e)
{
await _widget.LaunchUriAsync(new Uri("weather:"));
}
}

0 comments on commit b34b062

Please sign in to comment.