מדריך Windows Phone – פקדים בסיסיים: טקסט
ישנם מספר פקדים להצגה ועריכת טקסט, לכל אחד תכונות שונות. בפרק זה אנו סוקרים את החשובים ביניהם.
הפקד TextBlock
הפקד TextBlock נועד להצגה של טקסט למשתמש ללא אפשרות עריכה. התכונה החשובה ביותר של פקד היא היא התכונה Text שמאפשרת לקבוע את הטקסט להצגה.
דוגמא לשימוש בפקד TextBlock:
<phone:PhoneApplicationPage
x:Class="PhoneDemo.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait">
<Grid>
<TextBlock
Text="Hello from text block" />
</Grid>
</phone:PhoneApplicationPage>
והתוצאה:
הפקד TextBox
הפקד TextBox מאפשר לקבל טקסט מהמשתמש. גם בפקד זה התכונה החשובה ביותר נקראת Text. ההבדל המרכזי בין פקד זה לקודם הוא יכולת העריכה של הטקסט ע"י המשתמש.
תכונה מועילה נוספת של פקד TextBox היא MaxLength שמגבילה את כמות הטקסט שהמשתמש יכול להכניס. בנוסף לפקד יש אירוע TextChanged אשר נורה בכל פעם שהטקסט משתנה.
<phone:PhoneApplicationPage
x:Class="PhoneDemo.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait">
<StackPanel>
<TextBlock
Text="Enter your name:" />
<TextBox
Text="My name is.." />
</StackPanel>
</phone:PhoneApplicationPage>
הפקד PasswordBox
הפקד PasswordBox הוא פקד דומה לפקד TextBox, אלא שהוא מותאם במיוחד לקבלת סיסמאות מהמשתמש. פקד זה יציג כוכביות (או כל סימן אחר) כאשר המשתמש יכניס אליו קלט. בנוסף מאחורי הקלעים הפקד שומר את המידע בצורה קצת יותר מאובטחת ע"י שימוש במחלקה SecureString
.להלן דוגמת שימוש בפקד PasswordBox:
<phone:PhoneApplicationPage
x:Class="PhoneDemo.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait">
<StackPanel>
<TextBlock
Text="Enter your password:" />
<PasswordBox />
</StackPanel>
</phone:PhoneApplicationPage>
הפקד RichTextBox
פקד RichTextBox הוא פקד נוסף המאפשר עריכת טקסט, אלא שבניגוד לפקד TextBox, פקד זה מאפשר שימוש בטקסט "עשיר", כולל שימוש בצבעים, Bold ועוד.
להלן דוגמא של שימוש בפקד להצגת טקסט עשיר. שימו לב לשימוש באובייקטים מסוג Run לצורך הגדרת מקטע טקסט בעל מאפיינים ייחודיים:
<phone:PhoneApplicationPage
x:Class="PhoneDemo.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait">
<StackPanel>
<TextBlock
Text="Enter your rich text:" />
<RichTextBox
HorizontalAlignment="Left"
Margin="10,12,0,0"
Name="contentBox"
VerticalAlignment="Top"
IsReadOnly="True">
<Paragraph
FontFamily="Georgia"
FontSize="28"
TextAlignment="Justify">
This photograph shows how
<Run
Text=" to use "
FontStyle="Italic"
FontWeight="ExtraBold" /> a
<Run
Text=" rich text box "
Foreground="Red"
FontWeight="ExtraBold" /> .
</Paragraph>
</RichTextBox>
</StackPanel>
</phone:PhoneApplicationPage>
תגובות בפייסבוק