2015-08-06 2 views
-1

Для заметки annotation есть способ извлечь текст из кода позади?Получить текст из примечания к заметке

На основании принятого ответа это то, что я использовал

List<string> stickiesText = new List<string>(); 
foreach (Annotation annotation in service.Store.GetAnnotations()) 
{ 
    if (annotation.AnnotationType != null) 
    { 
     Debug.WriteLine(annotation.AnnotationType.ToString()); 
     if (annotation.AnnotationType.ToString().EndsWith("TextStickyNote")) 
     { 
      string base64Text = annotation.Cargos[1].Contents[0].InnerText; 
      byte[] decoded = Convert.FromBase64String(base64Text); 

      MemoryStream m = new MemoryStream(decoded); 

      Section section = XamlReader.Load(m) as Section; 
      m.Close(); 

      TextRange range = new TextRange(section.ContentStart, section.ContentEnd); 

      Debug.WriteLine(range.Text); 
      stickiesText.Add(range.Text); 
     } 
    } 
} 
+0

Слезай голосования. В чем проблема? – Paparazzi

ответ

1

Wow. Да, есть, но они не облегчают. Что за ковер.

На основе приведенных здесь примеров: Pro WPF 4.5 in C#: Windows Presentation Foundation in .NET 4.5, Matthew MacDonald, и здесь: Introduction to Annotations.

XAML:

<Window x:Class="MvvmLight5.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:annot="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:ignore="http://www.ignore.com" 
     xmlns:local="clr-namespace:MvvmLight5.Model" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     Title="MVVM Light Application" 
     Width="Auto" 
     Height="Auto" 
     DataContext="{Binding Main, 
           Source={StaticResource Locator}}" 
     Loaded="OnLoaded" 
     Unloaded="OnUnloaded" 
     mc:Ignorable="d ignore"> 

    <Grid x:Name="LayoutRoot"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="300" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <!-- Viewer and simple content --> 

     <ToolBarTray Grid.Row="0"> 
      <!-- Annotations Toolbar --> 
      <ToolBar> 
       <!-- StickyNote Operations --> 
       <GroupBox Header="Notes"> 
        <StackPanel Orientation="Horizontal"> 
         <Button Width="30" 
           Command="annot:AnnotationService.CreateTextStickyNoteCommand" 
           Content="Text" /> 
         <Button Width="30" 
           Command="annot:AnnotationService.CreateInkStickyNoteCommand" 
           Content="Ink" /> 
         <Button Width="50" 
           Command="annot:AnnotationService.DeleteStickyNotesCommand" 
           Content="Delete" /> 
        </StackPanel> 
       </GroupBox> 
      </ToolBar> 
     </ToolBarTray> 

     <FlowDocumentPageViewer Name="Viewer" Grid.Row="1"> 
      <FlowDocument> 
       <Paragraph>This is a simple example of a flow document and how you can add annotations to one.</Paragraph> 
       <Paragraph>Later we'll get more complicated content...</Paragraph> 
      </FlowDocument> 
     </FlowDocumentPageViewer> 

     <Button Grid.Row="2" 
       Width="50" 
       Height="50" 
       Click="Button_Click" /> 
    </Grid> 
</Window> 

Codebehind:

// Turn Annotations On. 
protected void OnLoaded(object sender, RoutedEventArgs e) 
{ 
    // Make sure that an AnnotationService isn’t already enabled. 
    AnnotationService service = AnnotationService.GetService(Viewer); 

    if (service == null) 
    { 
     // (a) Create a Stream for the annotations to be stored in. 
     AnnotationStream = 
      new FileStream("annotations.xml", FileMode.OpenOrCreate); 

     // (b) Create an AnnotationService on our 
     // FlowDocumentPageViewer. 
     service = new AnnotationService(Viewer); 

     // (c) Create an AnnotationStore and give it the stream we 
     // created. (Autoflush == false) 
     AnnotationStore store = new XmlStreamStore(AnnotationStream); 

     // (d) "Turn on annotations". Annotations will be persisted in 
     // the stream created at (a). 
     service.Enable(store); 
    } 
} 

// Turn Annotations off. 
protected void OnUnloaded(object sender, RoutedEventArgs e) 
{ 
    // (a) Check that an AnnotationService actually 
    // existed and was Enabled. 
    AnnotationService service = 
    AnnotationService.GetService(Viewer); 

    if (service != null && service.IsEnabled) 
    { 
     // (b) Flush changes to annotations to our stream. 
     service.Store.Flush(); 
     // (c) Turn off annotations. 
     service.Disable(); 
     // (d) Close our stream. 
     AnnotationStream.Close(); 
    } 
} 

// The stream that we will store annotations in. 
Stream AnnotationStream; 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    AnnotationService service = 
    AnnotationService.GetService(Viewer); 

    if (service != null && service.IsEnabled) 
    { 
     var annotation = service.Store.GetAnnotations().ElementAt(0); 

     string base64Text = annotation.Cargos[1].Contents[0].InnerText; 
     byte[] decoded = Convert.FromBase64String(base64Text); 

     MemoryStream m = new MemoryStream(decoded); 

     Section section = XamlReader.Load(m) as Section; 
     m.Close(); 

     TextRange range = new TextRange(section.ContentStart, section.ContentEnd); 

     MessageBox.Show(range.Text); 
    } 
} 
+0

Ничего себе, спасибо. У меня нет времени проверять его прямо сейчас, но я это сделаю. – Paparazzi