Address: http://create.msdn.com/en-US/education/quickstarts/Working_with_Text_on_the_Windows_Phone
Note: This articleArticleIt is the sixth article translated from Microsoft's official WP7 Quickstart, which describes the use of texts in WP. Some of the content is added to your understanding and expression habits. The main purpose of this series is to practice English, and to give yourself an understanding of WP7 development as a bi developer. For some improper translations, I hope you will point out criticism and correction]
Silverlight in Windows Phone provides some controls for displaying text, with some attributes to format the text. In Silverlight, text-based controls include textblock, Textbox, and passwordbox. This article describes how to display and input text with these controls.
Including the following content
Textblock
Textbox
Keypad under textbox
Passwordbox
Textblock
Textblock displays read-only text in Windows Phone. You can use its text attribute to specify the displayed text.
The following XAML demonstrates how to define a textblock control and set its text attributes.
XAML
<Textblock text = "Hello, world! "/>
The result is as follows.
You can also display more strings in textblock, and each string can have different formats. You can use the run element to define the format of each string and separate it using linebreak.
The following XAML demonstrates that text in textblock uses run to define different formats and uses linebreak to separate them.
XAML
<Grid>
<Textblock fontfamily = "Arial" width = "400">
<Linebreak/>
<Run foreground = "maroon" fontfamily = "Courier New" fontsize = "40">
Courier New 24
</Run>
<Linebreak/>
<Run foreground = "teal" fontfamily = "Times New Roman" fontsize = "30" fontstyle = "italic">
Times New Roman italic 18
</Run>
<Linebreak/>
<Run foreground = "steelblue" fontfamily = "verdana" fontsize = "20" fontweight = "bold">
Verdana bold 14
</Run>
</Textblock>
</GRID>
The result is as follows.
Textbox
Textbox is used to input single or multiple lines of text. The text attribute is the text content. In the following example, there are three text input areas. When entered in the first Textbox, the same text will appear in the second textbox. The textchanged event is used here. The third textbox displays the watermark. The watermark effect can be achieved through the font attributes, such as foreground and fontsize, as well as some events, such as gotfocus and lostfocus.
In the following online instance, click the first text box and enter some content to see the running effect.
Note: This online instance uses traditional Silverlight to simulate the Silverlight effect on Windows Phone. The actual running effect is slightly different from that of the Windows Phone simulator and physical devices.
XAML
<Stackpanel background = "Transparent">
<Textblock text = "type text here"/>
<Textbox X: Name = "readwritetb" textchanged = "readwritetb_textchanged"
Isreadonly = "false"/>
<Textblock text = "Read Only textbox"/>
<Textbox X: Name = "readonlytb" isreadonly = "true"/>
<Textblock text = "search type textbox"/>
<Textblock fontsize = "17" textwrapping = "Wrap">
When you click inside the text box the watermark text is removed and
Cursor appears ready for input.
</Textblock>
<Textbox X: Name = "watermarktb" text = "Search"
Foreground = "gray" gotfocus = "watermarktb_gotfocus"
Lostfocus = "watermarktb_lostfocus"/>
</Stackpanel>
C #
// The following method displays the text entered in readwritetb in readonlytb.
Private void readwritetb_textchanged (Object sender, routedeventargs E)
{
Readonlytb. Text = readwritetb. text;
}
// The foreground color of the text in watermarktb is set to magenta when watermarktb
// Gets focus.
Private void watermarktb _ gotfocus (Object sender, routedeventargs E)
{
If (watermarktb. Text = "Search ")
{
Watermarktb. Text = "";
Solidcolorbrush brush1 = new solidcolorbrush ();
Brush1.color = colors. Magenta;
Watermarktb. Foreground = brush1;
}
}
// The foreground color of the text in watermarktb is set to blue when watermarktb
// Loses focus. Also, if searchtb loses focus and no text is entered,
// Text "Search" is displayed.
Private void watermarktb _ lostfocus (Object sender, routedeventargs E)
{
If (watermarktb. Text = string. Empty)
{
Watermarktb. Text = "Search ";
Solidcolorbrush brush2 = new solidcolorbrush ();
Brush2.color = colors. blue;
Watermarktb. Foreground = brush2;
}
}
Keypad under textbox
In Windows Phone, the main way to input text is through the keyboard on the screen, that is, the soft keyboard. When a control like textbox is activated, the keyboard slides in from the bottom of the screen. When you click a slider or return button outside the editing area, it will slide out from the bottom of the screen. If the device has a physical keyboard, the keyboard is automatically disabled. You can set the appearance of the keyboard.ProgramIs easier to enter characters. For example, when you enter the zip code, you only want to see the digital keyboard. At this time, you can set it through the inputscope attribute.
The following lists the commonly used Enis, as well asCodeInputscopenamevalue attribute specified in.
Default
Default style. The first letter is automatically capitalized when a sentence is entered.
Text
It is suitable for use in documents or emails.
Chat
Suitable for text messages, chat interfaces, and variousCommunityClient.
URL
Suitable for entering webpage addresses.
Telephonenumber
12 keys, suitable for entering phone numbers.
Emailsmtpaddress
It is suitable for entering an electronic right-click address with the @ and. com keys.
Currencyamount
Suitable for currency input.
In addition, there are more than 60 modes, which are not described here.
The following example shows how to use XAML and C # To set the input mode for textbox.
XAML
<Textbox text = "helloworld">
<Textbox. inputscope>
<Inputscope>
<Inputscopename namevalue = "chat"/>
</Inputscope>
</Textbox. inputscope>
</Textbox>
C #
Inputscope ipchat = new inputscope ();
Inputscopename ipnchat = new inputscopename ();
Ipnchat. namevalue = inputscopenamevalue. Chat;
Ipchat. Names. Add (ipnchat );
Tbchatwindow. inputscope = ipchat;
Passwordbox
It is used to enter the password. The password entered by the user is displayed as *, so it cannot be seen. You can use the Password attribute to retrieve the entered password and use the passwordchar attribute to specify the characters that replace the password.
[Translator's note: The content of this part is basically similar to that of traditional Silverlight. The password box is somewhat different, and the support for the soft keyboard is also unique under WP .]