Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF推送和弹出_C#_.net_Wpf_User Interface_Render - Fatal编程技术网

C# WPF推送和弹出

C# WPF推送和弹出,c#,.net,wpf,user-interface,render,C#,.net,Wpf,User Interface,Render,我有一些渲染代码,它依赖于和相应的Pop方法 protected override void OnRender(DrawingContext drawingContext) { drawingContext.PushClip(whatever); OnRenderInternal(...); drawingContext.Pop(); } 现在,让我们假设在OnRenderInternal方法中发生了一些非常糟糕的事情,它抛出了一个异常(在这种情况下,Pop将永远不会

我有一些渲染代码,它依赖于和相应的
Pop
方法

protected override void OnRender(DrawingContext drawingContext)
{
    drawingContext.PushClip(whatever);
    OnRenderInternal(...);
    drawingContext.Pop();
}
  • 现在,让我们假设在
    OnRenderInternal
    方法中发生了一些非常糟糕的事情,它抛出了一个异常(在这种情况下,
    Pop
    将永远不会被调用)。这是否会中断整个渲染过程,或者在执行其他操作之前,图形上下文将自身恢复到某种“安全”状态(并且不会对其他可渲染项进行剪裁)

  • 基本上,在执行渲染操作时,我是否应该始终关心将图形上下文恢复到其初始状态

  • 我意识到在这个简单的例子中,我可以通过使用
    try finally
    语句来避免麻烦,但是如果我忘记保存正确的
    Push/Pop
    堆栈会发生什么呢

    • 您可以:

      protected override void OnRender(DrawingContext drawingContext)
      {
          try
          {
              drawingContext.PushClip(whatever);
              OnRenderInternal(...);        
          }
          finally
          {
              drawingContext.Pop();
          }
      }
      

      我认为通常不需要将DrawingContext恢复到其初始状态,原因很简单,每次调用OnRender都会得到一个新的DrawingContext。每次调用OnRender时都会重新创建整个图形,无论您在哪个状态下离开DrawingContext都无关紧要

      但也有例外。派生的UIElement也可以重写OnRender,并可以在执行自己的呈现代码之前首先调用base.OnRender()。在这种情况下,派生的UIElement将从基类的OnRender方法获取“损坏的”DrawingContext


      因此,除非您能确保您的UIElement不能从中派生,否则最好不要让DrawingContext处于未定义的“损坏”状态。

      谢谢。然而,我真的对我忘记调用
      Pop
      的情况很感兴趣,我想知道这有多糟糕(见我问题的最后一句)。好吧,如果任何东西不能被渲染,并且每次都抛出一个错误,那么在它被弹出之前,任何东西都不会再次渲染。