Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CancellationToken Parameter to Methods Returning Task and ValueTask #335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/maui/TOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ items:
- name: "MediaElement"
href: views/MediaElement.md
- name: "Popup"
href: views/popup.md
href: views/Popup.md
- name: SemanticOrderView
href: views/semantic-order-view.md
- name: C# Markup
Expand Down
22 changes: 16 additions & 6 deletions docs/maui/views/DrawingView.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ var drawingView = new DrawingView
Lines = new ObservableCollection<IDrawingLine>(),
DrawingLineCompletedCommand = new Command<IDrawingLine>(async (line) =>
{
var stream = await line.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint());
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var stream = await line.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint(), cts.Token);
gestureImage.Source = ImageSource.FromStream(() => stream);
})
};
drawingView.OnDrawingLineCompleted += async (s, e) =>
{
var stream = await e.LastDrawingLine.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint());
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var stream = await e.LastDrawingLine.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint(), cts.Token);
gestureImage.Source = ImageSource.FromStream(() => stream);
};
```
Expand Down Expand Up @@ -153,7 +157,9 @@ var drawingView = new DrawingView
ShouldClearOnFinish = false,
DrawingLineCompletedCommand = new Command<IDrawingLine>(async (line) =>
{
var stream = await line.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint());
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var stream = await line.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint(), cts.Token);
gestureImage.Source = ImageSource.FromStream(() => stream);
}),
LineColor = Colors.Red,
Expand All @@ -162,19 +168,23 @@ var drawingView = new DrawingView
};
drawingView.OnDrawingLineCompleted += async (s, e) =>
{
var stream = await e.LastDrawingLine.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint());
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var stream = await e.LastDrawingLine.GetImageStream(gestureImage.Width, gestureImage.Height, Colors.Gray.AsPaint(), cts.Token);
gestureImage.Source = ImageSource.FromStream(() => stream);
};

// get stream from lines collection
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var lines = new List<IDrawingLine>();
var stream1 = await DrawingView.GetImageStream(
lines,
new Size(gestureImage.Width, gestureImage.Height),
Colors.Black);
Colors.Black.
cts.Token);

// get steam from the current DrawingView
var stream2 = await drawingView.GetImageStream(gestureImage.Width, gestureImage.Height);
var stream2 = await drawingView.GetImageStream(gestureImage.Width, gestureImage.Height, cts.Token);
```

## Properties
Expand Down
2 changes: 1 addition & 1 deletion docs/maui/views/MediaElement.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ To read more about handlers, please see the .NET MAUI documentation on [Handlers
| Play | Starts playing the loaded media. |
| Pause | Pauses playback of the current media. |
| Stop | Stops playback and resets the position of the current media. |
| SeekTo | Takes a `TimeSpan` value to set the `Position` property to. |
| SeekTo | Takes a `TimeSpan` value to set the `Position` property to and takes a `CancellationToken` to cancel the `Task`. |

## Examples

Expand Down
22 changes: 16 additions & 6 deletions docs/maui/views/popup.md → docs/maui/views/Popup.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class MyViewModel : INotifyPropertyChanged

For a more concrete example please refer to our sample application and the example in [`MultiplePopupViewModel`](https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/MultiplePopupViewModel.cs)

The `IPopupService` also provides methods to handle a result being returned from a Popup as covered in [Returning a result](./popup.md#returning-a-result).
The `IPopupService` also provides methods to handle a result being returned from a Popup as covered in [Returning a result](./Popup.md#returning-a-result).

#### Passing data to a Popup view model

Expand Down Expand Up @@ -165,7 +165,7 @@ There are 2 different ways that a `Popup` can be closed; programmatically or by

In order to close a `Popup` a developer must call `Close` or `CloseAsync` on the `Popup` itself. This is typically performed by responding to a button press from a user.

If we enhance the previous XAML example by adding an ok `Button`:
We can enhance the previous XAML example by adding an **OK** `Button`:

```xml
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
Expand Down Expand Up @@ -205,7 +205,9 @@ public partial class MySimplePopup : Popup

async void OnOKButtonClicked(object? sender, EventArgs e)
{
await CloseAsync();
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

await CloseAsync(token: cts.Token);
await Toast.Make("Popup Dismissed By Button").Show();
}
}
Expand Down Expand Up @@ -245,9 +247,17 @@ By adding 2 new buttons to the XAML:
Then adding the following event handlers in the C#:

```csharp
async void OnYesButtonClicked(object? sender, EventArgs e) => await CloseAsync(true);
async void OnYesButtonClicked(object? sender, EventArgs e)
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await CloseAsync(true, cts.Token);
}

async void OnNoButtonClicked(object? sender, EventArgs e) => await CloseAsync(false);
async void OnNoButtonClicked(object? sender, EventArgs e)
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await CloseAsync(false, cts.Token);
}
```

The `Close` method allows for an `object` value to be supplied, this will be the resulting return value. In order to await the result the `ShowPopupAsync` method must be used as follows:
Expand All @@ -261,7 +271,7 @@ public class MyPage : ContentPage
{
var popup = new SimplePopup();

var result = await this.ShowPopupAsync(popup);
var result = await this.ShowPopupAsync(popup, CancellationToken.None);

if (result is bool boolResult)
{
Expand Down
2 changes: 1 addition & 1 deletion docs/maui/views/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ The .NET MAUI Community Toolkit provides a collection of pre-built, reusable vie
| [`LazyView`](LazyView.md) | The `LazyView` control allows you to delay the initialization of a View.|
| [`Map (Windows)`](Map.md) | The `Map` control is a cross-platform view for displaying and annotating maps. The Windows implementation is available through the .NET MAUI Community Toolkit. |
| [`MediaElement`](MediaElement.md) | The `MediaElement` is a view for playing multimedia such as audio and video. |
| [`Popup`](popup.md) | The `Popup` view allows developers to build their own custom UI and present it to their users. |
| [`Popup`](Popup.md) | The `Popup` view allows developers to build their own custom UI and present it to their users. |
| [`SemanticOrderView`](semantic-order-view.md) | The `SemanticOrderView` provides the ability to control the order of VisualElements for screen readers and improve the Accessibility of an application. |