2017-08-11 2 views
0

J'ai mon propre ApplicationModalAdorner. Parfois, je reçois un InvalidOperationException de GetLayoutClip() que je remplace. L'appel à TransformToAncestor provoque l'exception. Je suis incapable de reproduire l'exception (je vois beaucoup l'exception dans la journalisation). Peut-être que la fenêtre n'est pas complètement rendue? Je cherche une reproduction (peut-être dans une application simple) et une cause/solution pour cette exception. Je reçois l'exception plusieurs fois dans la même minute. Je préfère une solution sans Dispatcher.ApplicationModalAdorner GetLayoutClip() InvalidOperationException

partie callstack pertinente:

Wrapped Exception: System.InvalidOperationException: An exception occured while handling another exception; 
    bij System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix& simpleTransform) 
    bij System.Windows.Media.Visual.TransformToAncestor(Visual ancestor) 
    bij x.x.x.ApplicationModalAdorner.WindowRect() 
    bij x.x.x.ApplicationModalAdorner.GetLayoutClip(Size layoutSlotSize) 
    bij System.Windows.UIElement.ensureClip(Size layoutSlotSize) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 
    bij System.Windows.Documents.AdornerLayer.ArrangeOverride(Size finalSize) 
    bij System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 
    bij System.Windows.Documents.AdornerDecorator.ArrangeOverride(Size finalSize) 
    bij System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 
    bij System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds) 
    bij System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 
    bij MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize) 
    bij System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize) 
    bij System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 
    bij System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize) 
    bij System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) 
    bij System.Windows.UIElement.Arrange(Rect finalRect) 

code

public class ApplicationModalAdorner : Adorner 
{ 
    private SolidColorBrush _brush; 

    private XamDialogWindow Window 
    { 
     get 
     { 
      return (XamDialogWindow)AdornedElement; 
     } 
    } 

    private Window MainWindow 
    { 
     get 
     { 
      if (Application.Current != null) 
      { 
       return Application.Current.MainWindow; 
      } 
      return null; 
     } 
    } 

    public ApplicationModalAdorner(UIElement adornedElement) 
     : base(adornedElement) 
    { 
     _brush = (SolidColorBrush)FindResource("ModalBackgroundBrush"); 

     Window.Moved += WindowMoved; 
     Window.WindowStateChanged += WindowStateChanged; 
     MainWindow.SizeChanged += MainWindowSizeChanged; 
    } 

    // There is no WindowClosed event. Infragistics states that when a window is closed, 
    // the WindowStateChanged event gets triggerd and the value will be Hidden. Because 
    // the ApplicationModal windows cannot be hidden, this is a nice way to clean events. 
    private void WindowStateChanged(object sender, WindowStateChangedEventArgs e) 
    { 
     if (e.NewWindowState == Infragistics.Controls.Interactions.WindowState.Hidden) 
     { 
      Window.Moved -= WindowMoved; 
      Window.WindowStateChanged -= WindowStateChanged; 
      if (MainWindow != null) 
      { 
       MainWindow.SizeChanged -= MainWindowSizeChanged; 
      } 
     } 
    } 

    private void WindowMoved(object sender, MovedEventArgs e) 
    { 
     InvalidateVisual(); 
    } 

    private void MainWindowSizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     InvalidateVisual(); 
    } 

    protected override void OnRender(DrawingContext drawingContext) 
    { 
     drawingContext.DrawRectangle(_brush, null, WindowRect()); 
     base.OnRender(drawingContext); 
    } 

    protected override Geometry GetLayoutClip(Size layoutSlotSize) 
    { 
     var xamDialogWindowRect = new Rect(new Point(Window.Left, Window.Top), layoutSlotSize); 
     var geometryGroup = new GeometryGroup(); 
     geometryGroup.Children.Add(new RectangleGeometry(WindowRect())); 
     geometryGroup.Children.Add(new RectangleGeometry(xamDialogWindowRect)); 
     return geometryGroup; 
    } 

    private Rect WindowRect() 
    { 
     var transformToAncestor = Window.TransformToAncestor(MainWindow); 
     var topLeft = transformToAncestor.Inverse.Transform(new Point(0, 0)); 
     var bottomRight = transformToAncestor.Inverse.Transform(new Point(MainWindow.ActualWidth, MainWindow.ActualHeight)); 
     return new Rect(topLeft, bottomRight); 
    } 

    public override GeneralTransform GetDesiredTransform(GeneralTransform transform) 
    { 
     InvalidateVisual(); 
     return base.GetDesiredTransform(transform); 
    } 
    } 
} 
+0

* "Une exception est survenue lors de la gestion d'une autre exception" * Je pourrais être intéressé par cette autre exception qui y est mentionnée ... Obtenir une exception lorsque le tout est déjà dans un état impair n'est pas aussi intéressant que premier point d'échec. – grek40

+0

@ grek40 la première exception est la même exception, je reçois quelques exceptions les unes après les autres – Sybren

+0

Avez-vous la même exception la première fois que le code est exécuté (point d'arrêt)? – grek40

Répondre

0

Votre fenêtre n'est pas présent dans l'arborescence visuelle. Vous avez deux options:

  1. écouter l'événement chargé. Ensuite, une fois chargé, commencez à écouter les autres événements (Moved, WindowStateChanged, SizeChanged).
  2. Utilisez le répartiteur là où c'est nécessaire et espérez le meilleur.

Quant à votre demande d'une application d'exemple que je peux vous assurer que la question que vous voyez est incroyablement difficile à reproduire.

+0

J'ai contacté Infragistics à ce sujet, ils ont ressenti la même chose dans l'un de leurs contrôles, leur solution était d'essayer/attraper l'exception qui le fixe pour eux. – Sybren