Mega Code Archive

 
Categories / VB.Net Tutorial / WPF
 

Annotation Service

<Window        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:a="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework"     Title="Flow Document Reader + Annotations"   x:Class="Window1" Initialized="OnInitialized" Closed="OnClosed">   <StackPanel>     <StackPanel Orientation="Horizontal">       <Label>Control Annotations:</Label>       <Button Command="a:AnnotationService.CreateTextStickyNoteCommand"          CommandTarget="{Binding ElementName=reader}">Create Text Note</Button>       <Button Command="a:AnnotationService.CreateInkStickyNoteCommand" CommandTarget="{Binding ElementName=reader}">         Create Ink Note       </Button>       <Button Command="a:AnnotationService.DeleteStickyNotesCommand" CommandTarget="{Binding ElementName=reader}">         Remove Note       </Button>       <Button Command="a:AnnotationService.CreateHighlightCommand"  CommandTarget="{Binding ElementName=reader}">         Create Yellow Highlight       </Button>       <Button Command="a:AnnotationService.ClearHighlightsCommand" CommandTarget="{Binding ElementName=reader}">         Remove Highlight       </Button>     </StackPanel>     <FlowDocumentReader x:Name="reader">       <FlowDocument>         <Paragraph FontSize="22" FontWeight="Bold">Chapter 1</Paragraph>         <Paragraph FontSize="35" FontWeight="Bold">Why WPF?</Paragraph>         <Paragraph>           this is a test         </Paragraph>         <Paragraph>                 this is a test                           </Paragraph>         <Paragraph>           this is another test         </Paragraph>         <Paragraph>...</Paragraph>       </FlowDocument>     </FlowDocumentReader>   </StackPanel> </Window> //File:Window.xaml.vb Imports System Imports System.IO Imports System.Windows Imports System.Windows.Annotations Imports System.Windows.Annotations.Storage Public Partial Class Window1   Inherits Window   Private stream As Stream   Public Sub New()     InitializeComponent()   End Sub   Protected Overloads Sub OnInitialized(sender As Object, e As EventArgs)     Dim service As AnnotationService = AnnotationService.GetService(reader)     If service Is Nothing Then       stream = New FileStream("storage.xml", FileMode.OpenOrCreate)       service = New AnnotationService(reader)       Dim store As AnnotationStore = New XmlStreamStore(stream)       service.Enable(store)     End If   End Sub   Protected Overloads Sub OnClosed(sender As Object, e As EventArgs)     Dim service As AnnotationService = AnnotationService.GetService(reader)     If service IsNot Nothing AndAlso service.IsEnabled Then       service.Store.Flush()       service.Disable()       stream.Close()     End If   End Sub End Class