Skip to content

Commit 0e9a854

Browse files
committed
merged master
2 parents 70c08e6 + 881155d commit 0e9a854

File tree

8 files changed

+466
-0
lines changed

8 files changed

+466
-0
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
</Content>
464464
<Content Include="SamplePages\HeaderedItemsControl\HeaderedItemsControlXaml.bind" />
465465
<Content Include="SamplePages\HeaderedContentControl\HeaderedContentControlXaml.bind" />
466+
<Content Include="SamplePages\AppPinManager\AppPinManagerHelperCode.bind" />
466467
<Content Include="SamplePages\Mouse\MouseCursorPage.bind" />
467468
</ItemGroup>
468469
<ItemGroup>
@@ -494,6 +495,9 @@
494495
<Compile Include="SamplePages\AdvancedCollectionView\AdvancedCollectionViewPage.xaml.cs">
495496
<DependentUpon>AdvancedCollectionViewPage.xaml</DependentUpon>
496497
</Compile>
498+
<Compile Include="SamplePages\AppPinManager\AppPinManagerHelperPage.xaml.cs">
499+
<DependentUpon>AppPinManagerHelperPage.xaml</DependentUpon>
500+
</Compile>
497501
<Compile Include="SamplePages\BluetoothLEHelper\BluetoothLEHelperPage.xaml.cs">
498502
<DependentUpon>BluetoothLEHelperPage.xaml</DependentUpon>
499503
</Compile>
@@ -773,6 +777,10 @@
773777
<SubType>Designer</SubType>
774778
<Generator>MSBuild:Compile</Generator>
775779
</Page>
780+
<Page Include="SamplePages\AppPinManager\AppPinManagerHelperPage.xaml">
781+
<SubType>Designer</SubType>
782+
<Generator>MSBuild:Compile</Generator>
783+
</Page>
776784
<Page Include="SamplePages\Carousel\CarouselPage.xaml">
777785
<Generator>MSBuild:Compile</Generator>
778786
<SubType>Designer</SubType>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Specific app pin to StartMenu
2+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
3+
var pinResult = await AppPinManager.PinSpecificAppToStartMenuAsync(appList);
4+
5+
//User Specific app in StartMenu
6+
var userInfo = await User.FindAllAsync();
7+
if (userInfo.Count > 0)
8+
{
9+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
10+
var pinResult = await AppPinManager.PinUserSpecificAppToStartMenuAsync(userInfo[0], appList);
11+
}
12+
13+
//Current app pin to TaskBar
14+
var pinResult = await AppPinManager.PinCurrentAppToTaskBarAsync();
15+
16+
17+
//Specific app pin to TaskBar
18+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
19+
var pinResult = await AppPinManager.PinSpecificAppToTaskBarAsync(appList);
20+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<Page
2+
x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.AppPinManagerHelperPage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
9+
mc:Ignorable="d">
10+
11+
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
12+
<Grid Background="{StaticResource Brush-Grey-05}">
13+
14+
<StackPanel VerticalAlignment="Top">
15+
<StackPanel Margin="0 5 0 0">
16+
<TextBlock Text="AppPinManager" Margin="5" FontSize="30"/>
17+
<StackPanel Margin="10">
18+
<TextBlock Text="Pin to StartMenu" Margin="5" FontSize="20"/>
19+
20+
<Button x:Name="SpecificAppSMenuButton"
21+
Margin="5"
22+
Click="SpecificAppSMenuButton_OnClick"
23+
Content="Pin Specific App To StartMenu" />
24+
25+
<Button x:Name="UserSpecificAppSMenuButton"
26+
Margin="5"
27+
Click="UserSpecificAppSMenuButton_OnClick"
28+
Content="Pin User Specific App To StartMenu" />
29+
</StackPanel>
30+
<StackPanel Margin="10">
31+
<TextBlock Text="Pin to TaskBar" Margin="5" FontSize="20"/>
32+
33+
<Button x:Name="CurrentAppTBarButton"
34+
Margin="5"
35+
Click="CurrentAppTBarButton_OnClick"
36+
Content="Pin Current App To TaskBar" />
37+
38+
<Button x:Name="SpecificAppTBarButton"
39+
Margin="5"
40+
Click="SpecificAppTBarButton_OnClick"
41+
Content="Pin Specific App To TaskBar" />
42+
</StackPanel>
43+
<StackPanel Margin="5"
44+
Orientation="Horizontal">
45+
<TextBlock x:Name="StatusMessage" FontSize="25" />
46+
</StackPanel>
47+
</StackPanel>
48+
</StackPanel>
49+
50+
51+
<VisualStateManager.VisualStateGroups>
52+
<VisualStateGroup>
53+
<VisualState x:Name="Full">
54+
<VisualState.StateTriggers>
55+
<AdaptiveTrigger MinWindowWidth="600" />
56+
</VisualState.StateTriggers>
57+
</VisualState>
58+
<VisualState x:Name="Small">
59+
<VisualState.StateTriggers>
60+
<AdaptiveTrigger MinWindowWidth="0" />
61+
</VisualState.StateTriggers>
62+
63+
<VisualState.Setters>
64+
<Setter Target="ButtonsPanel.Orientation" Value="Vertical" />
65+
</VisualState.Setters>
66+
</VisualState>
67+
</VisualStateGroup>
68+
</VisualStateManager.VisualStateGroups>
69+
</Grid>
70+
</Grid>
71+
</Page>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ******************************************************************
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
5+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
7+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
8+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
9+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
10+
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
11+
// ******************************************************************
12+
13+
using System;
14+
using Microsoft.Toolkit.Uwp.Helpers;
15+
using Windows.ApplicationModel;
16+
using Windows.System;
17+
using Windows.UI.Xaml;
18+
using Windows.UI.Xaml.Controls;
19+
20+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
21+
{
22+
/// <summary>
23+
/// An empty page that can be used on its own or navigated to within a Frame.
24+
/// </summary>
25+
public sealed partial class AppPinManagerHelperPage : Page
26+
{
27+
public AppPinManagerHelperPage()
28+
{
29+
this.InitializeComponent();
30+
}
31+
32+
private async void SpecificAppSMenuButton_OnClick(object sender, RoutedEventArgs e)
33+
{
34+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
35+
var pinResult = await AppPinManager.PinSpecificAppToStartMenuAsync(appList);
36+
StatusMessage.Text = "SpecificApp in StartMenu : " + pinResult.ToString();
37+
}
38+
39+
private async void UserSpecificAppSMenuButton_OnClick(object sender, RoutedEventArgs e)
40+
{
41+
var userInfo = await User.FindAllAsync();
42+
if (userInfo.Count > 0)
43+
{
44+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
45+
var pinResult = await AppPinManager.PinUserSpecificAppToStartMenuAsync(userInfo[0], appList);
46+
StatusMessage.Text = "User SpecificApp in StartMenu : " + pinResult.ToString();
47+
}
48+
}
49+
50+
private async void CurrentAppTBarButton_OnClick(object sender, RoutedEventArgs e)
51+
{
52+
var pinResult = await AppPinManager.PinCurrentAppToTaskBarAsync();
53+
StatusMessage.Text = "Current App in TaskBar : " + pinResult.ToString();
54+
}
55+
56+
private async void SpecificAppTBarButton_OnClick(object sender, RoutedEventArgs e)
57+
{
58+
var appList = (await Package.Current.GetAppListEntriesAsync())[0];
59+
var pinResult = await AppPinManager.PinSpecificAppToTaskBarAsync(appList);
60+
StatusMessage.Text = "Specific App in TaskBar : " + pinResult.ToString();
61+
}
62+
}
63+
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@
541541
"Icon": "/Assets/Helpers.png",
542542
"DocumentationUrl": "https://raw.githubusercontent.com/Microsoft/UWPCommunityToolkit/master/docs/helpers/BackgroundTaskHelper.md"
543543
},
544+
{
545+
"About": "The AppPinManager helps easily add app shortcuts to the Start Menu or the Taskbar",
546+
"CodeFile": "AppPinManagerHelperCode.bind",
547+
"CodeUrl": "https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/AppPinManager/AppPinManager.cs",
548+
"DocumentationUrl": "https://raw.githubusercontent.com/Microsoft/UWPCommunityToolkit/master/docs/helpers/AppPinManager.md",
549+
"Icon": "/Assets/Helpers.png",
550+
"Name": "AppPinManager",
551+
"Type": "AppPinManagerHelperPage",
552+
"BadgeUpdateVersionRequired": "Creators Update required"
553+
},
544554
{
545555
"Name": "NetworkHelper",
546556
"Type": "NetworkHelperPage",
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// ******************************************************************
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
5+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
7+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
8+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
9+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
10+
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
11+
// ******************************************************************
12+
13+
using System;
14+
using System.Threading.Tasks;
15+
using Windows.ApplicationModel.Core;
16+
using Windows.Foundation.Metadata;
17+
using Windows.System;
18+
using Windows.UI.Shell;
19+
using Windows.UI.StartScreen;
20+
21+
namespace Microsoft.Toolkit.Uwp.Helpers
22+
{
23+
/// <summary>
24+
/// This class provides static helper methods help to add the app in startmenu or TaskBar.
25+
/// </summary>
26+
public static class AppPinManager
27+
{
28+
/// <summary>
29+
/// Pin the current app in Windows TaskBar
30+
/// </summary>
31+
/// <returns>PinResult</returns>
32+
public static async Task<PinResult> PinCurrentAppToTaskBarAsync()
33+
{
34+
var resultPinResult = PinResult.UnsupportedOs;
35+
36+
if (!ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager"))
37+
{
38+
return resultPinResult;
39+
}
40+
41+
if (TaskbarManager.GetDefault().IsSupported)
42+
{
43+
if (TaskbarManager.GetDefault().IsPinningAllowed)
44+
{
45+
if (await TaskbarManager.GetDefault().IsCurrentAppPinnedAsync())
46+
{
47+
resultPinResult = PinResult.PinAlreadyPresent;
48+
}
49+
else
50+
{
51+
var result = await TaskbarManager.GetDefault().RequestPinCurrentAppAsync();
52+
resultPinResult = result ? PinResult.PinPresent : PinResult.PinOperationFailed;
53+
}
54+
}
55+
else
56+
{
57+
resultPinResult = PinResult.PinNotAllowed;
58+
}
59+
}
60+
else
61+
{
62+
resultPinResult = PinResult.UnsupportedDevice;
63+
}
64+
65+
return resultPinResult;
66+
}
67+
68+
/// <summary>
69+
/// Pin Specific App in Windows TaskBar
70+
/// </summary>
71+
/// <param name="appListEntry">AppListEntry</param>
72+
/// <returns>PinResult</returns>
73+
public static async Task<PinResult> PinSpecificAppToTaskBarAsync(AppListEntry appListEntry)
74+
{
75+
var resultPinResult = PinResult.UnsupportedOs;
76+
77+
if (!ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager"))
78+
{
79+
return resultPinResult;
80+
}
81+
82+
if (TaskbarManager.GetDefault().IsSupported)
83+
{
84+
if (TaskbarManager.GetDefault().IsPinningAllowed)
85+
{
86+
if (await TaskbarManager.GetDefault().IsAppListEntryPinnedAsync(appListEntry))
87+
{
88+
resultPinResult = PinResult.PinAlreadyPresent;
89+
}
90+
else
91+
{
92+
var result = await TaskbarManager.GetDefault().RequestPinAppListEntryAsync(appListEntry);
93+
resultPinResult = result ? PinResult.PinPresent : PinResult.PinOperationFailed;
94+
}
95+
}
96+
else
97+
{
98+
resultPinResult = PinResult.PinNotAllowed;
99+
}
100+
}
101+
else
102+
{
103+
resultPinResult = PinResult.UnsupportedDevice;
104+
}
105+
106+
return resultPinResult;
107+
}
108+
109+
/// <summary>
110+
/// Pin Specific App in Windows StartMenu
111+
/// </summary>
112+
/// <param name="entry">AppListEntry</param>
113+
/// <returns>PinResult</returns>
114+
public static async Task<PinResult> PinSpecificAppToStartMenuAsync(AppListEntry entry)
115+
{
116+
var resultPinResult = PinResult.UnsupportedOs;
117+
118+
if (!ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager"))
119+
{
120+
return resultPinResult;
121+
}
122+
123+
if (StartScreenManager.GetDefault().SupportsAppListEntry(entry))
124+
{
125+
if (await StartScreenManager.GetDefault().ContainsAppListEntryAsync(entry))
126+
{
127+
resultPinResult = PinResult.PinAlreadyPresent;
128+
}
129+
else
130+
{
131+
var result = await StartScreenManager.GetDefault().RequestAddAppListEntryAsync(entry);
132+
resultPinResult = result ? PinResult.PinPresent : PinResult.PinOperationFailed;
133+
}
134+
}
135+
else
136+
{
137+
resultPinResult = PinResult.UnsupportedDevice;
138+
}
139+
140+
return resultPinResult;
141+
}
142+
143+
/// <summary>
144+
/// Pin Specific App in Windows StartMenu based on User
145+
/// </summary>
146+
/// <param name="user">User</param>
147+
/// <param name="entry">AppListEntry</param>
148+
/// <returns>PinResult</returns>
149+
public static async Task<PinResult> PinUserSpecificAppToStartMenuAsync(User user, AppListEntry entry)
150+
{
151+
var resultPinResult = PinResult.UnsupportedOs;
152+
153+
if (!ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager"))
154+
{
155+
return resultPinResult;
156+
}
157+
158+
if (StartScreenManager.GetForUser(user).SupportsAppListEntry(entry))
159+
{
160+
if (await StartScreenManager.GetForUser(user).ContainsAppListEntryAsync(entry))
161+
{
162+
resultPinResult = PinResult.PinAlreadyPresent;
163+
}
164+
else
165+
{
166+
var result = await StartScreenManager.GetForUser(user).RequestAddAppListEntryAsync(entry);
167+
resultPinResult = result ? PinResult.PinPresent : PinResult.PinOperationFailed;
168+
}
169+
}
170+
else
171+
{
172+
resultPinResult = PinResult.UnsupportedDevice;
173+
}
174+
175+
return resultPinResult;
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)