RenderTargetBitmap.RenderAsync 参数异常

RenderTargetBitmap.RenderAsync Argument exception

我使用以下代码来呈现 UIElement

var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(elementToRender); //this line throws the exception

var pixelBuffer = await bitmap.GetPixelsAsync();

var wb = new WriteableBitmap(1, 1);
wb = await wb.FromPixelBuffer(pixelBuffer, bitmap.PixelWidth, bitmap.PixelHeight);

var stream = await wb.GetStream();

这是我的 xaml:

<Grid x:Name="elementToRender" Background="White" Width="400" Height="400">
    <Ellipse Fill="Black" />
</Grid>

我在页面加载后调用此方法,但如果我尝试使用此代码,则会出现异常,这是堆栈跟踪:

  System.ArgumentException 
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=mscorlib
  StackTrace:
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

我该如何解决这个问题?

问题是包含 "elementToRender" 的 xaml 元素将其自己的可见性设置为 Collapsed