Skip to content

Commit 2120f62

Browse files
authored
Merge branch 'main' into tests/diagnostics
2 parents b588832 + c7c0f94 commit 2120f62

23 files changed

+93
-132
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ contact_links:
2020
about: "The Windows Community Toolkit uses Uno Platform for cross-platform WinUI. See the Uno Platform repo for issues specific to running on non-Windows platforms:"
2121
- name: Discord
2222
url: https://aka.ms/wct/discord
23-
about: "Join the UWP Discord Server and talk to us in the #community-toolkit channel."
23+
about: "Join the Windows App Community Discord Server and talk to us in the #community-toolkit channel."

components/Behaviors/src/CommunityToolkit.WinUI.Behaviors.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 -->
99
<RootNamespace>CommunityToolkit.WinUI.BehaviorsRns</RootNamespace>
1010
<PackageReadmeFile>ReadMe.md</PackageReadmeFile>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1112
</PropertyGroup>
1213

1314
<!-- Sets this up as a toolkit component's source project -->

components/Collections/samples/AdvancedCollectionView.md

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: AdvancedCollectionView
33
author: nmetulev
4-
description: The AdvancedCollectionView is a collection view implementation that support filtering, sorting and incremental loading. It's meant to be used in a viewmodel.
5-
keywords: AdvancedCollectionView, data, sorting, filtering, Collections
4+
description: The AdvancedCollectionView is a collection view implementation that support filtering, sorting and incremental loading. It's meant to be used in a view or viewmodel.
5+
keywords: AdvancedCollectionView, CollectionViewSource, data, sorting, filtering, Collections
66
dev_langs:
77
- csharp
88
category: Helpers
@@ -12,70 +12,23 @@ issue-id: 0
1212
icon: Assets/AdvancedCollectionView.png
1313
---
1414

15-
> [!Sample AdvancedCollectionViewSample]
16-
1715
## Usage
1816

19-
In your viewmodel instead of having a public [IEnumerable](https://learn.microsoft.com/dotnet/core/api/system.collections.generic.ienumerable-1) of some sort to be bound to an eg. [Listview](https://learn.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.ListView), create a public AdvancedCollectionView and pass your list in the constructor to it. If you've done that you can use the many useful features it provides:
17+
In your view or viewmodel instead of having a public [IEnumerable](https://learn.microsoft.com/dotnet/core/api/system.collections.generic.ienumerable-1) of some sort to be bound to an eg. [Listview](https://learn.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.ListView), create a public AdvancedCollectionView and pass your list in the constructor to it. If you've done that you can use the many useful features it provides:
2018

2119
* sorting your list using the `SortDirection` helper: specify any number of property names to sort on with the direction desired
2220
* filtering your list using a [Predicate](https://learn.microsoft.com/dotnet/core/api/system.predicate-1): this will automatically filter your list only to the items that pass the check by the predicate provided
2321
* deferring notifications using the `NotificationDeferrer` helper: with a convenient _using_ pattern you can increase performance while doing large-scale modifications in your list by waiting with updates until you've completed your work
2422
* incremental loading: if your source collection supports the feature then AdvancedCollectionView will do as well (it simply forwards the calls)
2523
* live shaping: when constructing the `AdvancedCollectionView` you may specify that the collection use live shaping. This means that the collection will re-filter or re-sort if there are changes to the sort properties or filter properties that are specified using `ObserveFilterProperty`
2624

27-
## Example
28-
29-
```csharp
30-
using CommunityToolkit.WinUI.Collections;
31-
32-
// Grab a sample type
33-
public class Person
34-
{
35-
public string Name { get; set; }
36-
}
25+
The `AdvancedCollectionView` is a good replacement for WPF's `CollectionViewSource`.
3726

38-
// Set up the original list with a few sample items
39-
var oc = new ObservableCollection<Person>
40-
{
41-
new Person { Name = "Staff" },
42-
new Person { Name = "42" },
43-
new Person { Name = "Swan" },
44-
new Person { Name = "Orchid" },
45-
new Person { Name = "15" },
46-
new Person { Name = "Flame" },
47-
new Person { Name = "16" },
48-
new Person { Name = "Arrow" },
49-
new Person { Name = "Tempest" },
50-
new Person { Name = "23" },
51-
new Person { Name = "Pearl" },
52-
new Person { Name = "Hydra" },
53-
new Person { Name = "Lamp Post" },
54-
new Person { Name = "4" },
55-
new Person { Name = "Looking Glass" },
56-
new Person { Name = "8" },
57-
};
58-
59-
// Set up the AdvancedCollectionView with live shaping enabled to filter and sort the original list
60-
var acv = new AdvancedCollectionView(oc, true);
61-
62-
// Let's filter out the integers
63-
int nul;
64-
acv.Filter = x => !int.TryParse(((Person)x).Name, out nul);
65-
66-
// And sort ascending by the property "Name"
67-
acv.SortDescriptions.Add(new SortDescription("Name", SortDirection.Ascending));
68-
69-
// Let's add a Person to the observable collection
70-
var person = new Person { Name = "Aardvark" };
71-
oc.Add(person);
27+
## Example
7228

73-
// Our added person is now at the top of the list, but if we rename this person, we can trigger a re-sort
74-
person.Name = "Zaphod"; // Now a re-sort is triggered and person will be last in the list
29+
The following is a complete example of how to perform ...
7530

76-
// AdvancedCollectionView can be bound to anything that uses collections.
77-
YourListView.ItemsSource = acv;
78-
```
31+
> [!Sample AdvancedCollectionViewSample]
7932
8033
## Remarks
8134

components/Collections/samples/AdvancedCollectionViewSample.xaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="CollectionsExperiment.Samples.AdvancedCollectionViewSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="using:AdvancedCollectionViewExperiment.Samples"
6+
xmlns:local="using:CollectionsExperiment.Samples"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
mc:Ignorable="d">
99

@@ -17,8 +17,9 @@
1717
<Setter Property="CornerRadius" Value="{StaticResource ControlCornerRadius}" />
1818
</Style.Setters>
1919
</Style>
20-
<DataTemplate x:Key="PersonDataTemplate">
21-
<TextBlock Text="{Binding Name}" />
20+
<DataTemplate x:Key="EmployeeDataTemplate"
21+
x:DataType="local:Employee">
22+
<TextBlock Text="{x:Bind Name}" />
2223
</DataTemplate>
2324
</Page.Resources>
2425
<Grid ColumnSpacing="12"
@@ -51,7 +52,8 @@
5152
<Grid Grid.Row="2"
5253
Style="{StaticResource CardStyle}">
5354
<ListView x:Name="LeftList"
54-
ItemTemplate="{StaticResource PersonDataTemplate}" />
55+
ItemTemplate="{StaticResource EmployeeDataTemplate}"
56+
ItemsSource="{x:Bind EmployeeCollection}" />
5557
</Grid>
5658
<TextBlock Grid.Row="1"
5759
Grid.Column="1"
@@ -60,7 +62,8 @@
6062
Grid.Column="1"
6163
Style="{StaticResource CardStyle}">
6264
<ListView x:Name="RightList"
63-
ItemTemplate="{StaticResource PersonDataTemplate}" />
65+
ItemTemplate="{StaticResource EmployeeDataTemplate}"
66+
ItemsSource="{x:Bind CollectionView}" />
6467
</Grid>
6568
</Grid>
6669
</Page>

components/Collections/samples/AdvancedCollectionViewSample.xaml.cs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,73 @@
33
// See the LICENSE file in the project root for more information.
44

55
using CommunityToolkit.WinUI.Collections;
6+
using System.Diagnostics.CodeAnalysis;
67

78
namespace CollectionsExperiment.Samples;
89

9-
[ToolkitSample(id: nameof(AdvancedCollectionViewSample), "AdvancedCollectionView", description: $"A sample for showing how to create and use a {nameof(AdvancedCollectionView)}.")]
10+
[ToolkitSample(id: nameof(AdvancedCollectionViewSample), "AdvancedCollectionView", description: $"A sample for showing how to create and use a {nameof(AdvancedCollectionView)} for sorting and filtering.")]
1011
public sealed partial class AdvancedCollectionViewSample : Page
1112
{
12-
public ObservableCollection<Person>? oc;
13+
public ObservableCollection<Employee> EmployeeCollection { get; private set; }
14+
15+
public AdvancedCollectionView CollectionView { get; private set; }
1316

1417
public AdvancedCollectionViewSample()
1518
{
1619
this.InitializeComponent();
1720
Setup();
1821
}
1922

23+
[MemberNotNull(nameof(EmployeeCollection))]
24+
[MemberNotNull(nameof(CollectionView))]
2025
private void Setup()
2126
{
2227
// left list
23-
oc = new ObservableCollection<Person>
28+
EmployeeCollection = new()
2429
{
25-
new Person { Name = "Staff" },
26-
new Person { Name = "42" },
27-
new Person { Name = "Swan" },
28-
new Person { Name = "Orchid" },
29-
new Person { Name = "15" },
30-
new Person { Name = "Flame" },
31-
new Person { Name = "16" },
32-
new Person { Name = "Arrow" },
33-
new Person { Name = "Tempest" },
34-
new Person { Name = "23" },
35-
new Person { Name = "Pearl" },
36-
new Person { Name = "Hydra" },
37-
new Person { Name = "Lamp Post" },
38-
new Person { Name = "4" },
39-
new Person { Name = "Looking Glass" },
40-
new Person { Name = "8" },
30+
new() { Name = "Staff" },
31+
new() { Name = "42" },
32+
new() { Name = "Swan" },
33+
new() { Name = "Orchid" },
34+
new() { Name = "15" },
35+
new() { Name = "Flame" },
36+
new() { Name = "16" },
37+
new() { Name = "Arrow" },
38+
new() { Name = "Tempest" },
39+
new() { Name = "23" },
40+
new() { Name = "Pearl" },
41+
new() { Name = "Hydra" },
42+
new() { Name = "Lamp Post" },
43+
new() { Name = "4" },
44+
new() { Name = "Looking Glass" },
45+
new() { Name = "8" },
4146
};
4247

43-
LeftList.ItemsSource = oc;
44-
4548
// right list
46-
var acv = new AdvancedCollectionView(oc);
47-
int nul;
48-
acv.Filter = x => !int.TryParse(((Person)x).Name, out nul);
49-
acv.SortDescriptions.Add(new SortDescription("Name", SortDirection.Ascending));
49+
AdvancedCollectionView acv = new(EmployeeCollection);
50+
acv.Filter = x => !int.TryParse(((Employee)x).Name, out _);
51+
acv.SortDescriptions.Add(new(nameof(Employee.Name), SortDirection.Ascending));
5052

51-
RightList.ItemsSource = acv;
53+
CollectionView = acv;
5254
}
5355

5456
private void Add_Click(object sender, RoutedEventArgs e)
5557
{
5658
if (!string.IsNullOrWhiteSpace(NewItemBox.Text))
5759
{
58-
oc!.Insert(0, new Person { Name = NewItemBox.Text });
60+
EmployeeCollection.Insert(0, new Employee { Name = NewItemBox.Text });
5961
NewItemBox.Text = "";
6062
}
6163
}
64+
}
6265

66+
/// <summary>
67+
/// A sample class used to show how to use the <see cref="AdvancedCollectionView"/> class.
68+
/// </summary>
69+
public partial class Employee
70+
{
6371
/// <summary>
64-
/// A sample class used to show how to use the <see cref="IIncrementalSource{TSource}"/> interface.
72+
/// Gets or sets the name of the person.
6573
/// </summary>
66-
public class Person
67-
{
68-
/// <summary>
69-
/// Gets or sets the name of the person.
70-
/// </summary>
71-
public string? Name { get; set; }
72-
}
74+
public string? Name { get; set; }
7375
}

components/Collections/samples/Collections.Samples.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2424
</Content>
2525
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Include="PolySharp" Version="1.14.1">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
31+
</ItemGroup>
2632
</Project>

components/Collections/samples/IncrementalLoadingCollectionSample.xaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="CollectionsExperiment.Samples.IncrementalLoadingCollectionSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="using:IncrementalLoadingCollectionExperiment.Samples"
6+
xmlns:local="using:CollectionsExperiment.Samples"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
mc:Ignorable="d">
99

@@ -19,18 +19,18 @@
1919
<TextBlock Text="Items are loaded incrementally when the view needs to show them (i.e., when the user scrolls the ListView)"
2020
TextWrapping="Wrap" />
2121
<Button Margin="0,12,0,12"
22-
Click="RefreshCollection"
22+
Click="{x:Bind PeopleSource.RefreshAsync}"
2323
Content="Refresh collection"
2424
Style="{StaticResource AccentButtonStyle}" />
2525
<TextBlock>
2626
<Run Text="Is loading:" />
2727
<Run FontWeight="SemiBold"
28-
Text="{Binding IsLoading, Mode=OneWay}" />
28+
Text="{x:Bind PeopleSource.IsLoading, Mode=OneWay}" />
2929
</TextBlock>
3030
<TextBlock>
3131
<Run Text="Has more items:" />
3232
<Run FontWeight="SemiBold"
33-
Text="{Binding HasMoreItems, Mode=OneWay}" />
33+
Text="{x:Bind PeopleSource.HasMoreItems, Mode=OneWay}" />
3434
</TextBlock>
3535

3636
</StackPanel>
@@ -43,9 +43,10 @@
4343
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
4444
BorderThickness="1"
4545
CornerRadius="4">
46-
<ListView x:Name="PeopleListView">
46+
<ListView x:Name="PeopleListView"
47+
ItemsSource="{x:Bind PeopleSource, Mode=OneWay}">
4748
<ListView.ItemTemplate>
48-
<DataTemplate>
49+
<DataTemplate x:DataType="local:Person">
4950
<Grid>
5051
<Grid.ColumnDefinitions>
5152
<ColumnDefinition Width="Auto" />
@@ -58,7 +59,7 @@
5859
<TextBlock Grid.Column="1"
5960
Margin="12"
6061
VerticalAlignment="Center"
61-
Text="{Binding Name}" />
62+
Text="{x:Bind Name}" />
6263
</Grid>
6364
</DataTemplate>
6465
</ListView.ItemTemplate>

components/Collections/samples/IncrementalLoadingCollectionSample.xaml.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,20 @@ namespace CollectionsExperiment.Samples;
99
[ToolkitSample(id: nameof(IncrementalLoadingCollectionSample), "Incremental Loading Collection", description: $"A sample for showing how to create and use a IncrementalLoadingCollection.")]
1010
public sealed partial class IncrementalLoadingCollectionSample : Page
1111
{
12+
// IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.
13+
public IncrementalLoadingCollection<PeopleSource, Person> PeopleSource { get; set; } = new(new PeopleSource());
14+
1215
public IncrementalLoadingCollectionSample()
1316
{
1417
this.InitializeComponent();
15-
Load();
16-
}
17-
private void Load()
18-
{
19-
// IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.
20-
var collection = new IncrementalLoadingCollection<PeopleSource, Person>(new PeopleSource());
21-
PeopleListView.ItemsSource = collection;
22-
23-
// Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
24-
DataContext = collection;
25-
}
26-
27-
private async void RefreshCollection(object sender, RoutedEventArgs e)
28-
{
29-
var collection = (IncrementalLoadingCollection<PeopleSource, Person>)PeopleListView.ItemsSource;
30-
await collection.RefreshAsync();
3118
}
3219
}
3320

3421
/// <summary>
3522
/// A sample implementation of the <see cref="IIncrementalSource{TSource}"/> interface.
3623
/// </summary>
3724
/// <seealso cref="IIncrementalSource{TSource}"/>
38-
public class PeopleSource : IIncrementalSource<Person>
25+
public partial class PeopleSource : IIncrementalSource<Person>
3926
{
4027
private readonly List<Person> _people;
4128

@@ -94,7 +81,7 @@ public PeopleSource()
9481
/// <summary>
9582
/// A sample class used to show how to use the <see cref="IIncrementalSource{TSource}"/> interface.
9683
/// </summary>
97-
public class Person
84+
public partial class Person
9885
{
9986
/// <summary>
10087
/// Gets or sets the name of the person.

components/Collections/src/CommunityToolkit.WinUI.Collections.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 -->
99
<RootNamespace>CommunityToolkit.WinUI.CollectionsRns</RootNamespace>
1010
<PackageReadmeFile>ReadMe.md</PackageReadmeFile>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

components/ColorPicker/src/CommunityToolkit.WinUI.Controls.ColorPicker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 -->
88
<RootNamespace>CommunityToolkit.WinUI.Controls.ColorPickerRns</RootNamespace>
9+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
910
</PropertyGroup>
1011

1112
<ItemGroup>

0 commit comments

Comments
 (0)