UIElement以及FrameworkElement中常见事件的运行顺序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Diagnostics;
namespace WpfApplication2
{
    class TestControl : Control
    {
        public TestControl()
        {
            Loaded += new System.Windows.RoutedEventHandler(TestControl_Loaded);
            Initialized += new EventHandler(TestControl_Initialized);
            LayoutUpdated += new EventHandler(TestControl_LayoutUpdated);
        }
        public bool Prop
        {
            get { return (bool)GetValue(PropProperty); }
            set { SetValue(PropProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Prop.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PropProperty =
            DependencyProperty.Register("Prop", typeof(bool), typeof(TestControl), new FrameworkPropertyMetadata((bool
                )false, FrameworkPropertyMetadataOptions.AffectsRender,new PropertyChangedCallback(OnPropChanged),new CoerceValueCallback(CoerceProp)));
        private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.WriteLine("DP:Changed");
        }
        private static object CoerceProp(DependencyObject d, object value)
        {
            Debug.WriteLine("DP:Coerce");
            return value;
        }
        void TestControl_LayoutUpdated(object sender, EventArgs e)
        {
            Debug.WriteLine("UIElement.LayoutUpdated");
        }
        void TestControl_Initialized(object sender, EventArgs e)
        {
            Debug.WriteLine("FrameworkElement.Initialized");
        }
        void TestControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Debug.WriteLine("FrameworkElement.Loaded");
        }
        protected override Size MeasureOverride(Size constraint)
        {
            Debug.WriteLine("FrameworkElement.MeasureOverride");
            return base.MeasureOverride(constraint);
        }
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            Debug.WriteLine("FrameworkElement.ArrangeOverride");
            return base.ArrangeOverride(arrangeBounds);
        }
        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            Debug.WriteLine("UIElement.OnRender");
            base.OnRender(drawingContext);
        }
    }
}

<Window x:Class="WpfApplication2.TestControlWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="TestControlWindow" Height="300" Width="300">
    <Grid>
        <local:TestControl />
    </Grid>
</Window>

点击查看原图

知识共享许可协议
《UIElement以及FrameworkElement中常见事件的运行顺序》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     153.67ms