Skip to content

Commit c4e31cf

Browse files
Local Test for adding PeoplePicker
Needs artifacts from unoplatform/Uno.WindowsCommunityToolkit#59 Don't forget to add ClientID :)
1 parent dd0aeb4 commit c4e31cf

File tree

10 files changed

+231
-9
lines changed

10 files changed

+231
-9
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.ObjectModel;
6+
using Microsoft.Graph;
7+
using Microsoft.Toolkit.Uwp.UI.Controls;
8+
using Windows.UI.Xaml;
9+
10+
namespace Microsoft.Toolkit.Graph.Controls
11+
{
12+
/// <summary>
13+
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>.
14+
/// </summary>
15+
public partial class PeoplePicker
16+
{
17+
/// <summary>
18+
/// Gets the set of <see cref="Person"/> objects chosen by the user.
19+
/// </summary>
20+
public ObservableCollection<Person> PickedPeople
21+
{
22+
get { return (ObservableCollection<Person>)GetValue(SelectedPeopleProperty); }
23+
internal set { SetValue(SelectedPeopleProperty, value); }
24+
}
25+
26+
/// <summary>
27+
/// Identifies the <see cref="PickedPeople"/> dependency property.
28+
/// </summary>
29+
/// <returns>
30+
/// The identifier for the <see cref="PickedPeople"/> dependency property.
31+
/// </returns>
32+
public static readonly DependencyProperty SelectedPeopleProperty =
33+
DependencyProperty.Register(nameof(PickedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>()));
34+
35+
/// <summary>
36+
/// Gets or sets collection of people suggested by the graph from the user's query.
37+
/// </summary>
38+
public ObservableCollection<Person> SuggestedPeople
39+
{
40+
get { return (ObservableCollection<Person>)GetValue(SuggestedPeopleProperty); }
41+
set { SetValue(SuggestedPeopleProperty, value); }
42+
}
43+
44+
/// <summary>
45+
/// Identifies the <see cref="SuggestedPeople"/> dependency property.
46+
/// </summary>
47+
/// <returns>
48+
/// The identifier for the <see cref="SuggestedPeople"/> dependency property.
49+
/// </returns>
50+
public static readonly DependencyProperty SuggestedPeopleProperty =
51+
DependencyProperty.Register(nameof(SuggestedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>()));
52+
}
53+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Microsoft.Graph;
7+
using Microsoft.Toolkit.Graph.Extensions;
8+
using Microsoft.Toolkit.Graph.Providers;
9+
using Microsoft.Toolkit.Uwp.UI.Controls;
10+
using Microsoft.Toolkit.Uwp.UI.Extensions;
11+
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
13+
14+
namespace Microsoft.Toolkit.Graph.Controls
15+
{
16+
/// <summary>
17+
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>.
18+
/// </summary>
19+
[TemplatePart(Name = PeoplePickerTokenizingTextBoxName, Type = typeof(TokenizingTextBox))]
20+
public partial class PeoplePicker : Control
21+
{
22+
private const string PeoplePickerTokenizingTextBoxName = "PART_TokenizingTextBox";
23+
24+
private TokenizingTextBox _tokenBox;
25+
26+
private DispatcherTimer _typeTimer = new DispatcherTimer();
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="PeoplePicker"/> class.
30+
/// </summary>
31+
public PeoplePicker()
32+
{
33+
this.DefaultStyleKey = typeof(PeoplePicker);
34+
}
35+
36+
/// <inheritdoc/>
37+
protected override void OnApplyTemplate()
38+
{
39+
base.OnApplyTemplate();
40+
41+
if (_tokenBox != null)
42+
{
43+
_tokenBox.QueryTextChanged -= TokenBox_QueryTextChanged;
44+
_tokenBox.TokenItemAdded -= TokenBox_TokenItemAdded;
45+
_tokenBox.TokenItemRemoved -= TokenBox_TokenItemRemoved;
46+
}
47+
48+
_tokenBox = GetTemplateChild(PeoplePickerTokenizingTextBoxName) as TokenizingTextBox;
49+
50+
if (_tokenBox != null)
51+
{
52+
_tokenBox.QueryTextChanged += TokenBox_QueryTextChanged;
53+
_tokenBox.TokenItemAdded += TokenBox_TokenItemAdded;
54+
_tokenBox.TokenItemRemoved += TokenBox_TokenItemRemoved;
55+
}
56+
}
57+
58+
private void TokenBox_TokenItemAdded(TokenizingTextBox sender, TokenizingTextBoxItem args)
59+
{
60+
if (args?.Content is Person person)
61+
{
62+
PickedPeople.Add(person);
63+
}
64+
}
65+
66+
private void TokenBox_TokenItemRemoved(TokenizingTextBox sender, TokenItemRemovedEventArgs args)
67+
{
68+
PickedPeople.Remove(args.Item as Person);
69+
}
70+
71+
private string _previousQuery;
72+
73+
private void TokenBox_QueryTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
74+
{
75+
#if !HAS_UNO
76+
if (!args.CheckCurrent())
77+
{
78+
return;
79+
}
80+
#endif
81+
82+
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
83+
{
84+
var text = sender.Text;
85+
_typeTimer.Debounce(
86+
async () =>
87+
{
88+
var graph = ProviderManager.Instance.GlobalProvider.Graph;
89+
if (graph != null)
90+
{
91+
// If empty, clear out
92+
if (string.IsNullOrWhiteSpace(text))
93+
{
94+
SuggestedPeople.Clear();
95+
}
96+
else
97+
{
98+
SuggestedPeople.Clear();
99+
100+
foreach (var contact in (await graph.FindPersonAsync(text)).CurrentPage)
101+
{
102+
SuggestedPeople.Add(contact);
103+
}
104+
}
105+
106+
_previousQuery = text;
107+
}
108+
109+
// TODO: If we don't have Graph connection and just list of Person should we auto-filter here?
110+
}, TimeSpan.FromSeconds(0.3));
111+
}
112+
}
113+
}
114+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:Microsoft.Toolkit.Graph.Controls"
5+
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls">
6+
7+
<Style TargetType="local:PeoplePicker">
8+
<Setter Property="Template">
9+
<Setter.Value>
10+
<ControlTemplate TargetType="local:PeoplePicker">
11+
<Border
12+
Background="{TemplateBinding Background}"
13+
BorderBrush="{TemplateBinding BorderBrush}"
14+
BorderThickness="{TemplateBinding BorderThickness}">
15+
<controls:TokenizingTextBox
16+
x:Name="PART_TokenizingTextBox"
17+
QueryIcon="Find"
18+
PlaceholderText="Start typing a name"
19+
TokenDelimiter=" "
20+
SuggestedItemsSource="{TemplateBinding SuggestedPeople}">
21+
<controls:TokenizingTextBox.SuggestedItemTemplate>
22+
<DataTemplate>
23+
<local:PersonView PersonDetails="{Binding}" ShowName="True" ShowEmail="True"/>
24+
</DataTemplate>
25+
</controls:TokenizingTextBox.SuggestedItemTemplate>
26+
<controls:TokenizingTextBox.TokenItemTemplate>
27+
<DataTemplate>
28+
<local:PersonView PersonDetails="{Binding}" ShowName="True"/>
29+
</DataTemplate>
30+
</controls:TokenizingTextBox.TokenItemTemplate>
31+
</controls:TokenizingTextBox>
32+
</Border>
33+
</ControlTemplate>
34+
</Setter.Value>
35+
</Setter>
36+
</Style>
37+
</ResourceDictionary>

Microsoft.Toolkit.Graph.Controls/Microsoft.Toolkit.Graph.Controls.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
</ItemGroup>
3232

3333
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == 'MonoAndroid' or '$(TargetFrameworkIdentifier)' == 'Xamarin.iOS' or '$(TargetFramework)' == 'netstandard2.0'">
34-
<PackageReference Include="Uno.UI" Version="1.46.0-dev.1991" />
35-
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI" Version="5.1.0-build.200.gf9c311b069" />
34+
<PackageReference Include="System.Buffers" Version="4.5.0.0" />
35+
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
36+
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.4.g02d22c3964" />
37+
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls" Version="6.0.0-build.4.g02d22c3964" />
3638
</ItemGroup>
3739

3840
<ItemGroup Condition="'$(TargetFramework)' == 'uap10.0' or '$(TargetFramework)' == 'uap10.0.16299' ">
39-
<PackageReference Include="Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.12" />
41+
<PackageReference Include="Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.26" />
42+
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls" Version="6.0.0-build.26" />
4043
</ItemGroup>
4144

4245
<ItemGroup>

Microsoft.Toolkit.Graph.Controls/Themes/Generic.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
33
<ResourceDictionary.MergedDictionaries>
44
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.xaml" />
5+
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml" />
56
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.xaml" />
67
</ResourceDictionary.MergedDictionaries>
78
</ResourceDictionary>

Microsoft.Toolkit.Graph.Controls/VisualStudioToolsManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<File Reference="Microsoft.Toolkit.Graph.Controls.dll">
33
<ToolboxItems VSCategory="Windows Community Toolkit Graph Controls" BlendCategory="Windows Community Toolkit Graph Controls">
44
<Item Type="Microsoft.Toolkit.Graph.Controls.LoginButton" />
5+
<Item Type="Microsoft.Toolkit.Graph.Controls.PeoplePicker" />
56
<Item Type="Microsoft.Toolkit.Graph.Controls.PersonView" />
67
</ToolboxItems>
78
</File>

Microsoft.Toolkit.Graph/Microsoft.Toolkit.Graph.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(TargetFrameworksOverride)</TargetFrameworks>
5-
<Title>Windows Community Toolkit .NET Standard Services</Title>
5+
<Title>Windows Community Toolkit .NET Standard Graph Services</Title>
6+
<PackageId>Uno.Microsoft.Toolkit.Graph</PackageId>
67
<Description>
78
This package includes .NET Standard code helpers such as:
89
- GraphExtensions: Helpers for common tasks related to the Microsoft Graph used by the Microsoft.Toolkit.Graph.Controls.
@@ -26,6 +27,6 @@
2627
<ItemGroup>
2728
<PackageReference Include="Microsoft.Graph.Beta" Version="0.7.0-preview" />
2829
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.1" />
29-
<PackageReference Include="Microsoft.Toolkit" Version="6.0.0-build.12" />
30+
<PackageReference Include="Uno.Microsoft.Toolkit" Version="6.0.0-build.4.g02d22c3964" />
3031
</ItemGroup>
3132
</Project>

SampleGraphApp/SampleGraphApp.Droid/SampleGraphApp.Droid.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<Reference Include="System.Xml" />
7272
</ItemGroup>
7373
<ItemGroup>
74-
<PackageReference Include="Uno.UI" Version="1.46.0-dev.1991" />
74+
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
7575
<PackageReference Include="Uno.UniversalImageLoader" Version="1.9.32" />
7676
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
7777
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />

SampleGraphApp/SampleGraphApp.Shared/MainPage.xaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:wgt="using:Microsoft.Toolkit.Graph.Controls"
9+
xmlns:graph="using:Microsoft.Graph"
910
mc:Ignorable="d">
1011

1112
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
12-
<wgt:LoginButton VerticalAlignment="Top" HorizontalAlignment="Right"/>
13-
<wgt:PersonView PersonQuery="Me" ShowName="True" ShowEmail="True"/>
13+
<wgt:LoginButton VerticalAlignment="Top" HorizontalAlignment="Right"/>
14+
<StackPanel Margin="16,48">
15+
<wgt:PeoplePicker x:Name="PeopleChooser"/>
16+
<TextBlock Margin="0,8,0,0" FontWeight="Bold">Picked People:</TextBlock>
17+
<ItemsControl ItemsSource="{x:Bind PeopleChooser.PickedPeople}"
18+
Margin="8,0,0,0">
19+
<ItemsControl.ItemTemplate>
20+
<DataTemplate x:DataType="graph:Person">
21+
<TextBlock Text="{x:Bind DisplayName}"/>
22+
</DataTemplate>
23+
</ItemsControl.ItemTemplate>
24+
</ItemsControl>
25+
</StackPanel>
1426
</Grid>
1527
</Page>

SampleGraphApp/SampleGraphApp.iOS/SampleGraphApp.iOS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<BundleResource Include="Resources\Fonts\winjs-symbols.ttf" />
128128
</ItemGroup>
129129
<ItemGroup>
130-
<PackageReference Include="Uno.UI" Version="1.45.0" />
130+
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
131131
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
132132
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
133133
</ItemGroup>

0 commit comments

Comments
 (0)