簡體   English   中英

訪問CustomTextParagraphProperties我需要參考什么?

[英]What do I need to reference to access CustomTextParagraphProperties?

使用TextFormatter時,我在MSDN上看到了本文,其中顯示了以下示例代碼:

// Create a TextFormatter object.
TextFormatter formatter = TextFormatter.Create();

// Create common paragraph property settings.
CustomTextParagraphProperties customTextParagraphProperties
    = new CustomTextParagraphProperties();

// Format each line of text from the text store and draw it.
while (textStorePosition < customTextSource.Text.Length)
{
    // Create a textline from the text store using the TextFormatter object.
    using (TextLine myTextLine = formatter.FormatLine(
        customTextSource,
        textStorePosition,
        96 * 6,
        customTextParagraphProperties,
        null))
    {
        // Draw the formatted text into the drawing context.
        myTextLine.Draw(drawingContext, linePosition, InvertAxes.None);

        // Update the index position in the text store.
        textStorePosition += myTextLine.Length;

        // Update the line position coordinate for the displayed line.
        linePosition.Y += myTextLine.Height;
    }
}

我遇到的問題甚至是在引用System.Windows.Media.TextFormatting之后,沒有使用CustomTextParagraphProperties類/對象,因此該行引發了錯誤(當然,因為它不存在)

訪問該類還需要參考什么?

這是我的using清單:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Windows.Media.TextFormatting;
using System.IO;

這是我的參考文獻清單:

參考資料

我找到了TextParagraphProperties但這是一個abstract類,因此無法使用new來創建,這意味着它不能代替CustomTextParagraphProperties如示例所示,以下行將無法工作:

TextParagraphProperties tp = new TextParagraphProperties();

注意:我添加了WPF標記,因為該項目繼承了WPF功能,但這不是WPF項目。

我開始鑽研一個兔子洞,找到了父依賴的越來越多的相互依賴的abstract類,並且由於不能使用new的實例化對象,因此必須編寫一堆代碼,而且看不到目的。相當令人沮喪:

#region textformatter
    class TextRunProperties : System.Windows.Media.TextFormatting.TextRunProperties {
        public override Windows.Media.Brush BackgroundBrush { get; set; }
        public override Windows.BaselineAlignment BaselineAlignment { get { return base.BaselineAlignment; } }
        public override CultureInfo CultureInfo { get; set; }
        public override double FontHintingEmSize { get; set; }
        public override double FontRenderingEmSize { get; set; }
        public override Windows.Media.Brush ForegroundBrush { get; set; }
        public override Windows.Media.NumberSubstitution NumberSubstitution { get { return base.NumberSubstitution; } }
        public override TextDecorationCollection TextDecorations { get; set; }
        public override Windows.Media.TextEffectCollection TextEffects { get; set; }
        public override Windows.Media.Typeface Typeface { get; set; }
        public override Windows.Media.TextFormatting.TextRunTypographyProperties TypographyProperties { get { return base.TypographyProperties; } }
    }

    class TextRun : System.Windows.Media.TextFormatting.TextRun {
        public override CharacterBufferReference CharacterBufferReference { get; set; }
        public override int Length { get; set; }
        public override Windows.Media.TextFormatting.TextRunProperties Properties { get; set; }
    }

    class TextSource : System.Windows.Media.TextFormatting.TextSource {
        public override Windows.Media.TextFormatting.TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText( int textSourceCharacterIndexLimit ) {
            return new TextSpan<CultureSpecificCharacterBufferRange>(
                textSourceCharacterIndexLimit,
                new CultureSpecificCharacterBufferRange(
                    new CultureInfo(0),
                    Windows.Media.TextFormatting.CharacterBufferRange.Empty
                )
            );
        }

        public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex( int textSourceCharacterIndex ) {
            return 0;
        }

        public override Windows.Media.TextFormatting.TextRun GetTextRun( int textSourceCharacterIndex ) {
            return new TextRun();
        }
    }

    class TextFormatter : System.Windows.Media.TextFormatting.TextFormatter {
        public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties, TextRunCache textRunCache ) {
            throw new NotImplementedException();
        }

    }

    class TextLine : System.Windows.Media.TextFormatting.TextLine {
        public override double Baseline {
            get { throw new NotImplementedException(); }
        }

        public override int DependentLength {
            get { throw new NotImplementedException(); }
        }

        public override double Extent {
            get { throw new NotImplementedException(); }
        }
        public override bool HasCollapsed {
            get { throw new NotImplementedException(); }
        }
        public override bool HasOverflowed {
            get { throw new NotImplementedException(); }
        }

        public override double Height {
            get { throw new NotImplementedException(); }
        }

        public override bool IsTruncated {
            get {
                return base.IsTruncated;
            }
        }

        public override int Length {
            get { throw new NotImplementedException(); }
        }

        public override double MarkerBaseline {
            get { throw new NotImplementedException(); }
        }
        public override double MarkerHeight {
            get { throw new NotImplementedException(); }
        }
        public override int NewlineLength {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangAfter {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangLeading {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangTrailing {
            get { throw new NotImplementedException(); }
        }
        public override double Start {
            get { throw new NotImplementedException(); }
        }
        public override double TextBaseline {
            get { throw new NotImplementedException(); }
        }
        public override double TextHeight {
            get { throw new NotImplementedException(); }
        }
        public override int TrailingWhitespaceLength {
            get { throw new NotImplementedException(); }
        }
        public override double Width {
            get { throw new NotImplementedException(); }
        }
        public override double WidthIncludingTrailingWhitespace {
            get { throw new NotImplementedException(); }
        }

        // functions
        public override Windows.Media.TextFormatting.TextLine Collapse( params TextCollapsingProperties[] collapsingPropertiesList ) {
            throw new NotImplementedException();
        }
        public override void Draw( DrawingContext drawingContext, Windows.Point origin, InvertAxes inversion ) {
            throw new NotImplementedException();
        }
        public override CharacterHit GetBackspaceCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }

        public override CharacterHit GetCharacterHitFromDistance( double distance ) {
            throw new NotImplementedException();
        }
        public override double GetDistanceFromCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override IEnumerable<IndexedGlyphRun> GetIndexedGlyphRuns() {
            throw new NotImplementedException();
        }
        public override CharacterHit GetNextCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override CharacterHit GetPreviousCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override IList<TextBounds> GetTextBounds( int firstTextSourceCharacterIndex, int textLength ) {
            throw new NotImplementedException();
        }
        public override IList<TextCollapsedRange> GetTextCollapsedRanges() {
            throw new NotImplementedException();
        }
        public override TextLineBreak GetTextLineBreak() {
            throw new NotImplementedException();
        }
        public override IList<TextSpan<Windows.Media.TextFormatting.TextRun>> GetTextRunSpans() {
            throw new NotImplementedException();
        }
    }

#endregion

我什至不知道這是否正確,但是從表面上看,這些都must override任何方法,所以該類到底做什么? 它是否會處理任何代碼,例如獲取TextEffect,還是所有必須手寫的東西-aka,我必須組成自己的TextEffects並將它們作為具有更多方法的抽象結構填充到該代碼中沒做什么 ....

您將必須從TextParagraphProperties派生您自己的類,並根據需要覆蓋屬性

public class CustomTextParagraphProperties  : TextParagraphProperties{...}

您還必須派生TextRunPropertiesTextMarkerProperties來返回您自己的值。

public class CustomTextRunProperties : TextRunProperties{...}
public class CustomTextMarkerProperties : TextMarkerProperties{...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM