Silverlight中Dependency Property(依存属性)

9720 ワード

原文接続http://virgos.javaeye.com/blog/592047
 
SL/WPFを学んで、Dependency Propties(依存性)は全く新しいですが、避けられない概念です.http://www.wpftutorial.net/DependencyProperties.htmlここに分かりやすい文章がありますが、E文の友達でもあります.なぜ依存性が現れたのですか?まず、伝統的な属性(Property)を思い出してみます.通常は属性を読み取ったり、与えたりする場合、実際には属性の背後にある私有メンバーを読み書きしています.オブジェクトの属性が多くなるにつれて、オブジェクトから派生したサブオブジェクトに加えて、サブオブジェクトから「孫」オブジェクトが再生されます.最終的なオブジェクト実行例には多くのプライベートメンバーがいますが、各プライベートメンバーはメモリを割り当てて、一定のリソースを占有します.しかし、逆に考えてみると、通常私たちはコントロール/オブジェクトを使用する場合、いくつかの属性のみを使用することがあります.ほとんどの属性(時には90%以上に達することもあります.)は採用されたデフォルト値です.これはWPF/SLにとって大きな性能損失です.したがって、この背景の下でDP(Dependency Proptiesの略称)が現れました.静的な方法またはメンバーを思い出してみます.静的なメンバー/方法の呼び出しは実例に依存しないです.これはクラスのいくつかの例があっても、静的な成員はメモリの中で一つしか占めないです.2.属性に依存する大体の原理と利点はすべて属性に依存する対象であり、DependencyObjectから継承されています.DependencyObjectの中には「辞書」の格納エリアがあります.依存属性を保存するために使われています.そして、staticで読み取られています.だから、なぜ直接txt.Left=xxxで値を付けられないのか?これは、スタティックメンバーがインスタンスで呼び出せないためです.DPの利点:(1)メモリ消費を効果的に低減する.(2)上位の属性値を直接継承する(ここではなぜ上位コントロールが下位コントロールに自動配置されているのか説明しています.下位コントロールは上位コントロールの関連属性値を自動的に継承しています.)(3)「変更通知」が自動的に実現されました.
3.属性値に依存する読み取りポリシーSilverlight中Dependency Property(依赖属性)この図は、GetValueとSetValueの内部読取ポリシーを記述しています.4.属性に依存する使用例:ユーザー独自のコントロールにどのようにMessage依存属性を追加するかを示します.(1)まずSilverlight User Controlを構築し、MyControlと名付けます.
Javaコード
  • <UserControl> x:Class=「DPStudy.MyControl」  
  •     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation「    
  •     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml「    
  •     >   
  •                
  •           
  •       
  •   
  •  
    <UserControl x:Class="DPStudy.MyControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        >
        <Border CornerRadius="5" BorderThickness="3" BorderBrush="#FFEF410D" Width="300" Height="40" Margin="5"  >        
            <TextBlock x:Name="txt" Text="" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
        </Border>
    </UserControl>
    
    CS部分:(テクニック:vs 2008ではpropdpを入力し、2回のTabキーを連打すると、vsは自動的に依存属性のコードテンプレートを追加します.)
    Javaコード
  • using System.Windows  
  • using System.Windows.Coontrols;  
  •   
  •   
  • namespace DPStudy  
  • {  
  •     public パーティー クラス MyControl : UserControl  
  •     {  
  •                
  •            
  •         public static readonly DependencyProperty Message Property = DependencyProperty.Register(Message)、 typeof(string) typeof(MyControl)、 new PropertyMetadata(「Messageのデフォルト値」、 new PropertyChangedCallback(OnMessage PropertyChanged);  
  •   
  •         public ストリングス メッセンジャー  
  •         {  
  •             get { return (string)GetValue(Message Property) }   
  •             セット { SetValue(Message Property) value; }   
  •         }   
  •   
  •         ///   
  •         /// メッセージの変更時の通知処理  
  •         ///   
  •         /// <パラム name="d"  
  •         /// <パラム name="e"  
  •         prvate static void OnMessage PropertyChangd(DependencyObject) d, DependencyPropertyChangedEventAgs e)  
  •         {  
  •             MyControl ctl = d as MyControl  
  •             ctl.txt.Text = d.GetValue(MyControl.Message Property).ToString()  
  •         }         
  •   
  •         public MyControl()  
  •         {  
  •             Initialize Component();  
  •   
  •             this.Loaded += new RoutedEvent Handler(MyConttrol Loaded);  
  •         }   
  •   
  •         void MyControl Loaded(object) sender RoutedEventArgs e)  
  •         {  
  •             this.txt.Text = Message;/初期ローディング時にMessageの初期値が表示されます.  
  •         }   
  •     }   
  • }  
  •  
    using System.Windows;
    using System.Windows.Controls;
    
    
    namespace DPStudy
    {
        public partial class MyControl : UserControl
        {
                
            
            public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(MyControl), new PropertyMetadata("Message    ", new PropertyChangedCallback(OnMessagePropertyChanged)));
    
            public string Message
            {
                get { return (string)GetValue(MessageProperty); }
                set { SetValue(MessageProperty, value); }
            }
    
            /// <summary>
            /// Message        
            /// </summary>
            /// <param name="d"></param>
            /// <param name="e"></param>
            private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MyControl ctl = d as MyControl;
                ctl.txt.Text = d.GetValue(MyControl.MessageProperty).ToString();
            }      
    
            public MyControl()
            {
                InitializeComponent();
    
                this.Loaded += new RoutedEventHandler(MyControl_Loaded);
            }
    
            void MyControl_Loaded(object sender, RoutedEventArgs e)
            {
                this.txt.Text = Message;//     ,  Message    
            }
        }
    }
    
    ここでは、Messageのstringタイプ依存性を定義しています.一般的な属性との違いは、DependencyProperty.Registerを使用してこの属性を登録しなければなりません.また、「属性名」はPropertyをバックにします.また、読み取り時にはSetValue/GetValue静的な方法を呼び出してその値を読み取ります.最後に「属性値の変化」を追加できます.コールバック処理.(2)マイコンをMainPage.xamlに置くと、リトルトライ牛刀MainPage.Xamlの内容は以下の通りです.
    Javaコード
  • <UserControl> x:Class=「DPStudy.MainPage」  
  •     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation「    
  •     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml「  
  •     xmlns:d="http://schemas.microsoft.com/expression/blend/2008「 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006「    
  •              xmlns:l="clr-namespace:DPStudy"  
  •     mc:Ignorable="d" d:Design Width="640" d:DesignHeight="480"  
  •   
  •           
  •         
  •       
  •   
  •  
    <UserControl x:Class="DPStudy.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:l="clr-namespace:DPStudy"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <StackPanel x:Name="LayoutRoot">        
            <l:MyControl x:Name="myctl"></l:MyControl>
            <Button Click="Button_Click" Content="  Message   " Width="130" ></Button>
        </StackPanel>
    </UserControl>
    
    MainPage.Xaml.csの内容は以下の通りです.
    Javaコード
  • using System.Windows  
  • using System.Windows.Coontrols;  
  •   
  • namespace DPStudy  
  • {  
  •     public パーティー クラス MainPage : UserControl  
  •     {  
  •         public MainPage()  
  •         {  
  •             Initialize Component();               
  •         }   
  •           
  •         prvate void Butonick(object) sender RoutedEventArgs e)  
  •         {  
  •             myctl.SetValue(MyControl.Message Property) 新しい値  
  •         }   
  •     }   
  • }  
  •  
    using System.Windows;
    using System.Windows.Controls;
    
    namespace DPStudy
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();            
            }
           
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                myctl.SetValue(MyControl.MessageProperty, "  ");
            }
        }
    }
    
    運転後、ボタンを押すと、MyControlコントロールのメッセージ属性値が変わります.また、MyControlのテキストコントロールの文字も新しい値になります.