[英]Why does this simple C# UWP Win2D program occasionally fail to render an image
Hi, my first post so please be gentle :) 嗨,我的第一篇文章,所以请保持温柔:)
I have written this simple C# UWP Win2D program that on occasions randomly fails to render the image on screen. 我已经编写了这个简单的C#UWP Win2D程序,有时会随机地无法在屏幕上渲染图像。 The program runs without problems roughly 90% of the time, and as it should, it renders the image, oddly though every now and then it fails to render it.
该程序大约90%的时间运行都没有问题,并且它应该渲染图像,但奇怪的是,尽管有时它无法渲染图像。
When it fails, it still clears the canvas to the correct colour but it does not render anything else. 失败时,它仍将画布清除为正确的颜色,但不会渲染其他任何内容。 The program seems to be running properly but when I place a break point on either the draw method or the update method a break does not occur.
该程序似乎运行正常,但是当我在draw方法或update方法上放置一个断点时,不会发生断点。
'Break all' does break the program but it informs me "Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)" 'Break all'确实破坏了程序,但是它通知我“您的应用已进入中断状态,但是没有代码可显示,因为所有线程都在执行外部代码(通常是系统或框架代码)”
I hope this explains the situation that I am currently facing, I have included the code that is causing this problem, hopefully someone more knowledgeable than me can help me find a remedy. 我希望这能解释我当前所面临的情况,我已经包含了导致此问题的代码,希望比我更有知识的人可以帮助我找到解决办法。
Here is my XAML code ... 这是我的XAML代码...
<Page
x:Class="TheGrid.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TheGrid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d"
Unloaded="Page_Unloaded">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<canvas:CanvasAnimatedControl
x:Name="canvas"
ClearColor="Bisque" Update="canvas_Update" Draw="canvas_Draw" CreateResources="canvas_CreateResources"
/>
</Grid>
</Page>
And here is the C# code behind ... 这是后面的C#代码...
public sealed partial class MainPage : Page
{
CanvasBitmap bitmap1;
public MainPage()
{
this.InitializeComponent();
}
private void canvas_CreateResources(CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(Canvas_CreateResourcesAsync(sender).AsAsyncAction());
}
async Task Canvas_CreateResourcesAsync(CanvasAnimatedControl sender)
{
bitmap1 = await CanvasBitmap.LoadAsync(sender, "Assets/Images/testimage.png");
}
private void canvas_Update(ICanvasAnimatedControl sender, CanvasAnimatedUpdateEventArgs args)
{
// Do stuff
}
private void canvas_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
args.DrawingSession.DrawImage(bitmap1, 256, 256);
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
this.canvas.RemoveFromVisualTree();
this.canvas = null;
}
}
Thank you in advance for any help you may be able to offer, please let me know if more information is required. 在此先感谢您提供的任何帮助,如果需要更多信息,请告诉我。
Edit 1.) I have tried to simplify the problem by removing 编辑1。)我试图通过删除来简化问题
args.TrackAsyncAction(Canvas_CreateResourcesAsync(sender).AsAsyncAction());
and 和
async Task Canvas_CreateResourcesAsync(CanvasAnimatedControl sender) { bitmap1 = await CanvasBitmap.LoadAsync(sender, "Assets/Images/testimage.png"); }
and replacing 并替换
args.DrawingSession.DrawImage(bitmap1, 256, 256);
with 与
args.DrawingSession.DrawCircle(256, 256, 128, Colors.Black, 16);
This seems to prevent the problem from occurring, so I can only presume that it is a problem with the async pattern that I am using. 这似乎可以防止问题的发生,因此我只能假定这与我使用的异步模式有关。
I think the problem is in here 我认为问题出在这里
private void canvas_CreateResources(CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(Canvas_CreateResourcesAsync(sender).AsAsyncAction());
}
When using async, my teacher told me to always use await and async function. 使用异步时,我的老师告诉我要始终使用等待和异步功能。 Because this will cause problem.
因为这会引起问题。
Some reading source : https://msdn.microsoft.com/en-us/library/hh191443.aspx 一些阅读源: https : //msdn.microsoft.com/en-us/library/hh191443.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.