0

I had all manner of difficulty with the code below until I referenced this SO post and decided I must need Shadows, after which it worked just fine; I just don't really understand it. The referenced post seemed to have many adherents to the 'don't like to EVER use Shadows' group, but I don't see how the code below could be written without Shadows, nor do I understand why it ultimately was required.

There was a XAML page that defines the buttons to invoke the annotation methods and also display the FlowDocumentReader
I didn't think that was necessary for this question but can add it if necessary

Imports System.IO
Imports System.Windows.Annotations
Imports System.Windows.Annotations.Storage

Partial Public Class MainWindow
  Inherits Window

  Private stream As Stream

  Public Sub New()
    InitializeComponent()
  End Sub

  Protected Shadows Sub OnInitialized(sender As Object, e As EventArgs)
    ' Enable and load annotations
    Dim service As AnnotationService = AnnotationService.GetService(reader6)
    If service Is Nothing Then
      stream = New FileStream("storage.xml", FileMode.OpenOrCreate)
      service = New AnnotationService(reader6)
      Dim store As AnnotationStore = New XmlStreamStore(stream)
      service.Enable(store)
    End If
  End Sub

  Protected Shadows Sub OnClosed(sender As Object, e As EventArgs)
    ' Disable and save annotations
    Dim service As AnnotationService = AnnotationService.GetService(reader6)
    If service IsNot Nothing AndAlso service.IsEnabled Then
      service.Store.Flush()
      service.Disable()
      stream.Close()
    End If
  End Sub

End Class

The code was written for a tutorial to see Annotations in action with a Flow Document. The Window element on the XAML page has:

Initialized="OnInitialized" Closed="OnClosed" 

Why is Shadow required instead of Overrides, and is this a proper use of Shadows? I've used Overrides before without problem, but not here. It seemed like some of the later comments in the referenced post may have been relevant to this situation and indicated Shadows is OK, but I wanted to ask this question pointedly.

Community
  • 1
  • 1
Alan
  • 1,587
  • 3
  • 23
  • 43
  • That is because the base class methods are not available, hence why you need to use `shadows`... Using `shadows` preserves and or maintains the definition of the method `OnClosed`; the reason is if the base class method has been changed. – Trevor Apr 03 '17 at 21:17

1 Answers1

1

OnInitialized and OnClosed are methods on the Window class, and their parameters don't match what you have (there is no sender parameter), so you need to declare them as Shadows to make the compiler happy. I think what you want to do is to avoid using OnInitialized and OnClosed as your event handler names, e.g.

Protected Sub Window_Initialized(sender As Object, e As EventArgs)
'...
Protected Sub Window_Closed(sender As Object, e As EventArgs)

'Initialized="Window_Initialized" Closed="Window_Closed" 
Mark
  • 8,140
  • 1
  • 14
  • 29
  • So to confirm, you are saying simply pick a different method name because the XAML Window element `Initialized=""` doesn't really care what the method is named because that method will be called when the Window element is Initialized no matter what. Same for `Closed=""`, and I happened to pick bad method names because I was following the book tutorial too closely. – Alan Apr 03 '17 at 21:58
  • 1
    Yes, you can use any method name for your event handlers, but you want to avoid methods that exist on the base class so that you don't run into things like this. – Mark Apr 03 '17 at 22:09
  • @Alan - The problem is that you are inheriting the base class `Window`. If you want to override the base class method, then your method signature must match the base class. – Chris Dunaway Apr 04 '17 at 15:10