"Win 10 development" Using the Richeditbox control summary

Source: Internet
Author: User

The text being edited in the Richeditbox control is exposed by the document property and is a itextdocument interface that has no public implementation type and can only be obtained by the document property of the Richeditbox class, Win The app API is similar to COM, so some types only expose interfaces and do not expose implementation types.

The selection property of the ITextDocument interface can get to the selected text area in the current edit box, and if the text in the edit box is not selected, the property gets the current cursor position. The Itextselection interface inherits many members from the Itextrange interface, and is more commonly used:

ParagraphFormat Property--Sets paragraph formatting.

Characterformat Property--Sets the format of a text area, such as bold display.

Copy method, cut method, paste method-can be copied, clipped, pasted operation.

MoveStart method, MoveEnd method, Move Method--Moves the text insertion point.

Insertimage Method-Inserts an image into the document.



The following old weeks use some examples to illustrate how to format the editing document.

first, set the font size:

Sets the font size for the selected text
EditBox. Document.Selection.CharacterFormat.Size = (float) Val;

Second, set the text color:

Set Text color
EditBox. Document.Selection.CharacterFormat.ForegroundColor = C;

Insert an image in the document:


Fileopenpicker picker = new Fileopenpicker ();
Picker. Filetypefilter.add (". jpg");
Picker. Filetypefilter.add (". png");
Picker. Filetypefilter.add (". jpeg");

Storagefile file = await picker. Picksinglefileasync ();
Open File stream
Irandomaccessstream stream = await file? OpenReadAsync ();
Insert Picture
EditBox. Document.Selection.InsertImage (0, Windows.UI.Text.VerticalCharacterAlignment.Baseline, "image", stream);


Four, the text application bold display:

Formateffect.toggle indicates a toggle state
EditBox. Document.Selection.CharacterFormat.Bold = Windows.UI.Text.FormatEffect.Toggle;

Set to toggle indicates a toggle state, that is, if the text is already in bold, it is set to a normal font, or bold if the text is not bold.

set the underline for text:


Itextcharacterformat format = EditBox. Document.Selection.CharacterFormat;
if (format. underline = = Underlinetype.single)
{
Format. underline = Underlinetype.none;
}
Else
{
Format. underline = Underlinetype.single;
}


From the above pile of examples also found, in fact, Richeditbox is not difficult to use, the right to call the correct members on the line.

However, this richeditbox is a problem control, it has a lot of small problems. One of the highlights is that when the font color is set for the text selected in the Richeditbox control, the cursor is repositioned back to the Richeditbox control, and the font is restored to its original color, which means that our changes to the color of the text are invalid. I believe that many people will encounter this problem, and some technical communities abroad also see similar questions.

The culprit is the visualization state, where a visual state named focused is declared in the Richeditbox control template, and the state is activated when the control gets focus. and set the foreground color of the control to the default Systemcontrolforegroundchromeblackhighbrush.

This is the source that causes text color settings to always be restored.

Find the cause, you can prescribe the right medicine. Use the XAML designer for VS to generate the default control template for Richeditbox, and then find the VisualStateGroups group under the root element grid in ControlTemplate, then find the CommonStates grouping, You will see that there is such a visual state inside.

<visualstate x:name= "focused" >
<Storyboard>
<objectanimationusingkeyframes storyboard.targetproperty= "Foreground" storyboard.targetname= " Placeholdertextcontentpresenter ">
<discreteobjectkeyframe keytime= "0" value= "{Themeresource Systemcontrolpagetextchromeblackmediumlowbrush}"/ >
</ObjectAnimationUsingKeyFrames>
<objectanimationusingkeyframes storyboard.targetproperty= "Background" Storyboard.targetname= " Backgroundelement ">
<discreteobjectkeyframe keytime= "0" value= "{themeresource systemcontrolbackgroundchromewhitebrush}"/>
</ObjectAnimationUsingKeyFrames>
<objectanimationusingkeyframes storyboard.targetproperty= "Opacity" storyboard.targetname= "BackgroundElement" >
<discreteobjectkeyframe keytime= "0" value= "{themeresource textcontrolbackgroundfocusedopacity}"/>
</ObjectAnimationUsingKeyFrames>
<objectanimationusingkeyframes storyboard.targetproperty= "BorderBrush" storyboard.targetname= "BorderElement" >
<discreteobjectkeyframe keytime= "0" value= "{themeresource systemcontrolhighlightaccentbrush}"/>
</ObjectAnimationUsingKeyFrames>
<objectanimationusingkeyframes storyboard.targetproperty= "Foreground" storyboard.targetname= "ContentElement" >
<discreteobjectkeyframe keytime= "0" value= "{themeresource systemcontrolforegroundchromeblackhighbrush}"/>
</ObjectAnimationUsingKeyFrames>
<objectanimationusingkeyframes storyboard.targetproperty= "Requestedtheme" Storyboard.targetname= " ContentElement ">
<discreteobjectkeyframe keytime= "0" value= "Light"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>

Among them, there is a timeline object that will draw your attention to it:

<objectanimationusingkeyframes storyboard.targetproperty= "Foreground" storyboard.targetname= "ContentElement" >
<discreteobjectkeyframe keytime= "0" value= "{themeresource systemcontrolforegroundchromeblackhighbrush}"/>
</ObjectAnimationUsingKeyFrames>

Because changes to the foreground property of the control affect the text in the edit box, and the focused state is activated when the Richeditbox control obtains the focus, the foreground property is restored to the default value, thereby restoring the text color of the current editing document.

So, as long as the objectanimationusingkeyframes time line to remove that can solve the problem, if you do not trust, you can first note it out.


In addition, Richeditbox also has a problem, that is, often can not set the text to italic, or the English alphabet can be italic, and Chinese characters are not available, or there will be bold and italic mixed with the problem. This problem may be a problem with fonts, and the old week has not found a solution for the moment.


Finally, give the guys a look at the results of the above example.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.