Click or drag to resize
CanvasCreateResourcesEventArgsTrackAsyncAction Method
Causes the sender to track an action that must completed before the CreateResources operation can be considered finished.

Namespace:  Microsoft.Graphics.Canvas.UI
Assembly:  Microsoft.Graphics.Canvas (in Microsoft.Graphics.Canvas.dll) Version: 0.0.0.0
Syntax
C#
public void TrackAsyncAction(
	IAsyncAction action
)

Parameters

action
Type: Windows.FoundationIAsyncAction
Remarks

Use this when creating resources involves asynchronous method calls, such as CanvasBitmap.LoadAsync.

void Canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
    args.TrackAsyncAction(Canvas_CreateResourcesAsync(sender).AsAsyncAction());
}

async Task Canvas_CreateResourcesAsync(CanvasControl sender)
{
    bitmapTiger = await CanvasBitmap.LoadAsync(sender, "imageTiger.jpg");
}

Using TrackAsyncAction will:

  • Ensure that the Draw event is not invoked until after the IAsyncAction has completed
  • Clear the contents of the control to the ClearColor while resources are being created
  • Redraw the control once resources have been loaded
  • Catch exceptions thrown during CreateResourcesAsync
  • Handle device lost exceptions by Canceling any previous IAsyncAction and waiting until they've completed before raising CreateResources again

For more advanced asynchronous loading scenarios, see this article.

In the current implementation, only one action per control can be tracked at a time. Attempting to track multiple actions simultaneously will cause TrackAsyncAction to throw an exception.

The behavior discussed above pertains to CanvasCreateResourcesEventArgs passed to an app through its CreateResources event handler.

For instances of CanvasCreateResourcesEventArgs which the app has created itself, TrackAsyncAction is a lightweight operation which does not do the above behavior. It holds a reference to the tracked action which can be retrieved using GetTrackedAction.

See Also