מדריך Windows Phone

מדריך Windows Phone – שימוש ב Data Binding: מועד ביצוע הסנכרון

‏ • Sela

אפשרות נוספת שניתנת לשליטה היא המועד שבו מתרחש סנכרון המידע. אפשרות הגיונית אחת היא לבצע סנכרון ברגע שהערך משתנה. אולם יש מקרים שבהם שיטה זו תהיה בזבזנית, חשבו על מצב שבו יש התכונה Text של TextBox מקושרת לתכונה מסוג String באובייקט נתונים, אם נבצע סנכרון בכל פעם שהמשתמש מקיש אות נוספת תתבצענה המון פעולות מיותרות. במקרה זה היה עדיף לעשות את הסנכרון רק בסוף מילוי ה TextBox, כאשר המשתמש עובר לשדה אחר.

השליטה במועד ביצוע הסנכרון נעשית באמצעות התכונה UpdateSourceTrigger של אובייקט ה Binding. האפשרויות לתכונה זו הם:

  1. Default – העדכון יתבצע בכל שינוי בתכונת המקור.
  2. Explicit – העדכון יבוצע אך ורק כאשר נריץ שורת קוד ב#C שתגרום לסנכרון. אפשרות זו טובה כאשר רוצים שליטה מלאה על זמן ביצוע הסנכרון.

בדוגמת הקוד הבאה ניתן לראות שימוש בשתי השיטות:

<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>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock
     Grid.Row="0"
     Grid.Column="0"
     Text="Update Source Trigger"
     TextWrapping="Wrap"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="0"
     Grid.Column="1"
     Text="Data"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="0"
     Grid.Column="2"
     Text="UI"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="1"
     Grid.Column="0"
     Text="Default" />
    <TextBlock
     Grid.Row="2"
     Grid.Column="0"
     Text="Explicit" />
    <TextBlock
     x:Name="data1"
     Grid.Row="1"
     Grid.Column="1"
     Text="text" />
    <TextBlock
     x:Name="data2"
     Grid.Row="2"
     Grid.Column="1"
     Text="text" />
    <TextBox
     x:Name="ui1"
     Grid.Row="1"
     Grid.Column="2"
     Text="{Binding ElementName=data1, Path=Text, UpdateSourceTrigger=Default, Mode=TwoWay}" />
    <TextBox
     x:Name="ui2"
     Grid.Row="2"
     Grid.Column="2"
     Text="{Binding ElementName=data2, Path=Text, UpdateSourceTrigger=Explicit, Mode=TwoWay}" />
    <Button
     Grid.Row="3"
     Grid.Column="0"
     Grid.ColumnSpan="3"
     Content="Explicit"
     Click="Button_Click" />
  </Grid
>
</
phone:PhoneApplicationPage
>

קוד ה#C שמתלווה לתוכנית זו הוא:

using System.Windows;
using System.Windows.Controls;
using
Microsoft.Phone.Controls;

namespace
PhoneDemo
{
 
public partial class MainPage : PhoneApplicationPage
  {
   
// Constructor
    public
MainPage()
    {
      InitializeComponent();
    }

   
private void Button_Click(object sender, RoutedEventArgs
e)
    {
      ui2.GetBindingExpression(
TextBox.TextProperty).UpdateSource();
    }
  }
}

שימו לב לשימוש בפקודה UpdateSource, זו הפקודה שגורמת לסנכרון ידני.

תוצאת הרצת התוכנית נראית כך:

מדריך Windows Phone – שימוש ב Data Binding: מועד ביצוע הסנכרון

כאשר נשנה את ה TextBox בשורה הראשונה (Default) הData ישתנה כאשר נעזוב את הTextBox. בשורה השניה (Explicit) הData לא יתעדכן כל עוד לא לחצנו על הכפתור Explicit.

מומלץ לכתוב בעצמכם את התוכנית ולהתנסות בהבדלים.

תגיות: , , , ,

arikp

אריק פוזננסקי הוא יועץ בכיר ומרצה בסלע. הוא השלים שני תארי B.Sc. במתמטיקה ומדעי המחשב בהצטיינות יתרה בטכניון. לאריק ידע נרחב בטכנולוגיות מיקרוסופט, כולל .NET עם C#, WPF, Silverlight, WinForms, Interop, COM/ATL, C++ Win32 ו reverse engineering.

תגובות בפייסבוק