Share via


附註概觀

在書面文件上撰寫備註或註解是相當普遍的活動,導致一般人幾乎都將它視為理所當然。 這些備註或註解是我們加入文件的「附註」,以標記資訊或特別標明有興趣的項目,以供日後參照。 雖然在書面文件上撰寫備註很容易,但是在電子文件上加入個人註解的功能通常非常有限或根本完全無法使用。

本主題將回顧幾種常見的附註類型 (特別是自黏便箋和醒目提示),並說明 Microsoft Annotations Framework 如何透過 Windows Presentation Foundation (WPF) 文件檢視控制項,在應用程式中使用這幾種附註類型。 支援附註的 WPF 文件檢視控制項包括 FlowDocumentReaderFlowDocumentScrollViewer,以及衍生自 DocumentViewerBase 的控制項,例如 DocumentViewerFlowDocumentPageViewer

這個主題包含下列章節。

  • 自黏便箋
  • 醒目提示
  • 資料錨定
  • 附註和標註物件配對
  • 相關主題

自黏便箋

一般自黏便箋是將資訊寫在彩色便條紙,然後再將便條紙「貼」在文件上。 數位自黏便箋為電子文件提供類似的功能,但是增加了彈性,使您可以加入其他許多不同類型的內容,諸如輸入文字、手寫備註 (如 Tablet PC「筆墨」筆劃) 或 Web 連結等。

下圖顯示醒目提示、文字自黏便箋和筆墨自黏便箋等附註的部分範例。

反白顯示、文字和筆跡自黏便箋標註。

下列範例示範可以在應用程式中啟用附註支援的方法。

        ' ------------------------ StartAnnotations --------------------------
        ''' <summary>
        '''   Enables annotations and displays all that are viewable.</summary>
        Private Sub StartAnnotations()
            ' If there is no AnnotationService yet, create one.
            If _annotService Is Nothing Then
                ' docViewer is a document viewing control named in Window1.xaml.
                _annotService = New AnnotationService(docViewer)
            End If

            ' If the AnnotationService is currently enabled, disable it.
            If _annotService.IsEnabled = True Then
                _annotService.Disable()
            End If

            ' Open a stream to the file for storing annotations.
            _annotStream = New FileStream(_annotStorePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)

            ' Create an AnnotationStore using the file stream.
            _annotStore = New XmlStreamStore(_annotStream)

            ' Enable the AnnotationService using the new store.
            _annotService.Enable(_annotStore)
        End Sub ' end:StartAnnotations()
// ------------------------ StartAnnotations --------------------------
/// <summary>
///   Enables annotations and displays all that are viewable.</summary>
private void StartAnnotations()
{
    // If there is no AnnotationService yet, create one.
    if (_annotService == null)
        // docViewer is a document viewing control named in Window1.xaml.
        _annotService = new AnnotationService(docViewer);

    // If the AnnotationService is currently enabled, disable it.
    if (_annotService.IsEnabled == true)
        _annotService.Disable();

    // Open a stream to the file for storing annotations.
    _annotStream = new FileStream(
        _annotStorePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

    // Create an AnnotationStore using the file stream.
    _annotStore = new XmlStreamStore(_annotStream);

    // Enable the AnnotationService using the new store.
    _annotService.Enable(_annotStore);
}// end:StartAnnotations()

醒目提示

一般人在標記書面文件時,都會使用有創意的方法將注意力集中在有興趣的項目上,例如使用底線、螢光筆、或畫圈的方式標出重要的字句,或是在空白處畫記號或加上備註。 Microsoft Annotations Framework 中的醒目提示附註提供了類似的功能,可以用來標記顯示在 WPF 文件檢視控制項中的資訊。

下圖顯示醒目提示附註的範例。

反白顯示標註

使用者建立附註的方式通常是先選取某段文字或有興趣的項目,然後再按一下滑鼠右鍵,顯示附註選項的 ContextMenu。 下列範例顯示 Extensible Application Markup Language (XAML),可用來宣告 ContextMenu,供使用者存取可以用來建立及管理附註的路由命令。

<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 &amp; Notes" />
  </ContextMenu>
</DocumentViewer.ContextMenu>

資料錨定

Annotations Framework 可將附註繫結到使用者選取的資料,而不是只繫結到畫面顯示的位置。 因此,如果文件檢視發生變更,例如當使用者捲動顯示視窗或調整其大小時,附註仍會跟隨著它所繫結的資料選取範圍。 例如,下圖顯示使用者在文字選取範圍所做的附註。 當文件檢視變更 (捲動、調整大小、縮放或其他移動方式) 時,醒目提示附註會隨著原始資料選取範圍一起移動。

標註資料錨定

附註和標註物件配對

您可以針對附註與對應的標註物件進行配對。 例如,假設某個簡單的文件讀取應用程式具有註解窗格, 註解窗格可能是清單方塊,會在錨定至文件的附註清單中顯示文字。 如果使用者選取清單方塊中的項目,應用程式就會在檢視中顯示對應附註物件錨定的文件段落。

下列範例示範如何實作當做註解窗格之清單方塊的事件處理常式 (Event Handler)。

        Private Sub annotationsListBox_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

            Dim comment As Annotation = TryCast((TryCast(sender, ListBox)).SelectedItem, Annotation)
            If comment IsNot Nothing Then
                ' service is an AnnotationService object
                ' comment is an Annotation object
                info = AnnotationHelper.GetAnchorInfo(Me.service, comment)
                Dim resolvedAnchor As TextAnchor = TryCast(info.ResolvedAnchor, TextAnchor)
                Dim textPointer As TextPointer = CType(resolvedAnchor.BoundingStart, TextPointer)
                textPointer.Paragraph.BringIntoView()
            End If
        End Sub
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();
    }
}

另一個範例案例牽涉到可以透過電子郵件在文件讀取器 (Reader) 之間交換附註和自黏便箋的應用程式。 這項功能使這些應用程式可以在讀取器中巡覽到內含要交換之附註的頁面。

請參閱

工作

HOW TO:將命令加入至 MenuItem

參考

DocumentViewerBase

DocumentViewer

FlowDocumentPageViewer

FlowDocumentScrollViewer

FlowDocumentReader

IAnchorInfo

概念

附註結構描述

ContextMenu 概觀

命令概觀

非固定格式文件概觀