How can I create an application that is used to declare a Context Menu with routed commands that the users can access to create and manage annotations ?
Users typically create annotations by first selecting some text or an item of interest and then right-clicking to display a context menu of annotation options. The following example shows the XAML you can use to declare a Context Menu with routed commands that users can access to create and manage annotations. [XAML] <DocumentViewer.ContextMenu> <ContextMenu> <MenuItem Command='ApplicationCommands.Copy' /> <Separator /> <!-- Add a Highlight annotation to a user selection. --> <MenuItem Command='ann:AnnotationService.CreateHighlightCommand' Header='Add Highlight' /> <!-- Add a Text Note annotation to a user selection. --> <MenuItem Command='ann:AnnotationService.CreateTextStickyNoteCommand' Header='Add Text Note' /> <!-- Add an Ink Note annotation to a user selection. --> <MenuItem Command='ann:AnnotationService.CreateInkStickyNoteCommand' Header='Add Ink Note' /> <Separator /> <!-- Remove Highlights from a user selection. --> <MenuItem Command='ann:AnnotationService.ClearHighlightsCommand' Header='Remove Highlights' /> <!-- Remove Text Notes and Ink Notes from a user selection. --> <MenuItem Command='ann:AnnotationService.DeleteStickyNotesCommand' Header='Remove Notes' /> <!-- Remove Highlights, Text Notes, Ink Notes from a selection. --> <MenuItem Command='ann:AnnotationService.DeleteAnnotationsCommand' Header='Remove Highlights & Notes' /> </ContextMenu> </DocumentViewer.ContextMenu>
How can I Match Annotations with Annotated Objects ?
You can match annotations with the corresponding annotated objects. For example, consider a simple document reader application that has a comments pane. The comments pane might be a listbox that displays the text from a list of annotations that are anchored to a document. If the user selects an item in the listbox, then the application brings into view the paragraph in the document that the corresponding annotation object is anchored to. The following piece of C # code demonstrates how to implement the event handler of such a listbox that serves as the comments pane. [C#] void annotationsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { Annotation comment = (sender as ListBox).SelectedItem as Annotation; if (comment != null) { // IAnchorInfo info; // service is an AnnotationService object // comment is an Annotation object info = AnnotationHelper.GetAnchorInfo(this.service, comment); TextAnchor resolvedAnchor = info.ResolvedAnchor as TextAnchor; TextPointer textPointer = (TextPointer)resolvedAnchor.BoundingStart; textPointer.Paragraph.BringIntoView(); } }
How can I do versioning using Custom Serialization ?
Versioning can be done using serialization as follows. [C#] //Version 2.0.0.0 [Serializable] public class MyClass : ISerializable { public int Number; public int NewField; public void GetObjectData( SerializationInfo info,StreamingContext context) { info.AddValue('Number',Number); info.AddValue('NewField',NewField); } protected MyClass(SerializationInfo info,StreamingContext context) { Number = info.GetInt32('Number'); Version storedVersion = SerializationUtil.GetVersion(info); if(storedVersion.ToString() == '2.0.0.0') { NewField = info.GetInt32('NewField'); } else { NewField = 123;//Some default value } } public MyClass() {} } public static class SerializationUtil { static public Version GetVersion(SerializationInfo info) { string assemblyName = info.AssemblyName; /* AssemblyName is in the form of 'MyAssembly, Version=1.2.3.4, Culture=neutral,PublicKeyToken=null' */ char[] separators = {’,’,’=’}; string[] nameParts = assemblyName.Split(separators); return new Version(nameParts[2]); } //Rest of SerializationUtil }
How can I use MemoryStream to create a deep clone of a serializable object ?
This can be done as follows. [C#] public static class SerializationUtil { static public T Clone<t>(T source) { Debug.Assert(typeof(T).IsSerializable); IGenericFormatter formatter = new GenericBinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream,source); stream.Seek(0,SeekOrigin.Begin); T clone = formatter.Deserialize<t>(stream); stream.Close(); return clone; } //Rest of SerializationUtil } </t></t>
How can I store a given document in a specific format using CreateSerializerWriter methods ?
You can store a given document format using CreateSerializerWriter method as follows. [C#] // Create a SerializerProvider for accessing plug-in serializers. SerializerProvider serializerProvider = new SerializerProvider(); // Locate the serializer that matches the fileName extension. SerializerDescriptor selectedPlugIn = null; foreach ( SerializerDescriptor serializerDescriptor in serializerProvider.InstalledSerializers ) { if ( serializerDescriptor.IsLoadable && fileName.EndsWith(serializerDescriptor.DefaultFileExtension) ) { // The plug-in serializer and fileName extensions match. selectedPlugIn = serializerDescriptor; break; // foreach } } // If a match for a plug-in serializer was found, // use it to output and store the document. if (selectedPlugIn != null) { Stream package = File.Create(fileName); SerializerWriter serializerWriter = serializerProvider.CreateSerializerWriter(selectedPlugIn, package); IDocumentPaginatorSource idoc = flowDocument as IDocumentPaginatorSource; serializerWriter.Write(idoc.DocumentPaginator, null); package.Close(); return true; }