none
[来自windows phone develper netowrd] 关于WP7 中页面元素生成 加载顺序的简单分析 RRS feed

  • 常规讨论

  • 详见原文 :http://www.wpdevn.com/showtopic-17.aspx

    在写代码时 我们通常会关系 页面中各个控件的加载顺序,现在我们来做一个简单分析 

    我们先创建一个WP 应用程序  Phone App2  

    我们先重写 3个控件 Grid、StackPlane、Button 并在构造函数中添加输出一条debug 信息

    1. public class CustomGrid : System.Windows.Controls.Grid
    2.     {
    3.         public CustomGrid()
    4.             :base()
    5.         {
    6.             System.Diagnostics.Debug.WriteLine("Structure Grid");
    7.         }

    8.       
    9.     }

    10.     public class CustomStackPanel: System.Windows.Controls.StackPanel
    11.     {
    12.         public CustomStackPanel()
    13.             : base()
    14.         {
    15.             System.Diagnostics.Debug.WriteLine("Structure StackPanel");
    16.             
    17.             

    18.         }


    19.     }


    20.     public class CustomButton:Button
    21.     {
    22.         public CustomButton()
    23.             : base()
    24.         {
    25.             System.Diagnostics.Debug.WriteLine("Structure Button");

    26.         }
    27.     }
    复制代码
    然后我们在MainPage 中添加如下界面布局 ,大家注意到  这个布局是用的是我们刚才定制的空间 并且 这是一个嵌套的布局
    1.     <c:CustomGrid x:Name="LayoutRoot" Background="Transparent" Loaded="LayoutRoot_Loaded" Unloaded="LayoutRoot_Unloaded">
    2.         <c:CustomStackPanel x:Name="panle" Loaded="StackPanel_Loaded" Unloaded="CustomStackPanel_Unloaded">

    3.             <c:CustomButton x:Name="button" Content="Button" Loaded="CustomButton_Loaded" Unloaded="CustomButton_Unloaded" Click="button_Click"></c:CustomButton>
    4.         </c:CustomStackPanel>
    5.     </c:CustomGrid>
    复制代码
    接下来我们 分别为这3个控件 写loaded 和 unloaded 方法 同样在里面输出一条 Debug信息
    1. private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    2.         {
    3.             System.Diagnostics.Debug.WriteLine("Page Loaded");

    4.         }

    5.         private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    6.         {
    7.             System.Diagnostics.Debug.WriteLine("LayoutRoot Loaded");

    8.         }

    9.         private void StackPanel_Loaded(object sender, RoutedEventArgs e)
    10.         {
    11.             System.Diagnostics.Debug.WriteLine("Panle Loaded");

    12.         }

    13.         private void CustomButton_Loaded(object sender, RoutedEventArgs e)
    14.         {
    15.             System.Diagnostics.Debug.WriteLine("button Loaded");

    16.         }

    17.         private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
    18.         {
    19.             System.Diagnostics.Debug.WriteLine("Page UnLoaded");

    20.         }

    21.         private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
    22.         {
    23.             System.Diagnostics.Debug.WriteLine("LayoutRoot UnLoaded");

    24.         }

    25.         private void CustomStackPanel_Unloaded(object sender, RoutedEventArgs e)
    26.         {
    27.             System.Diagnostics.Debug.WriteLine("Panle UnLoaded");

    28.         }

    29.         private void CustomButton_Unloaded(object sender, RoutedEventArgs e)
    30.         {
    31.             System.Diagnostics.Debug.WriteLine("button UnLoaded");

    32.         }
    复制代码
    重写 Nav To 和  Nav From  在里面输出debug 信息
    1. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    2.         {
    3.             System.Diagnostics.Debug.WriteLine("Nav From");

    4.             base.OnNavigatedFrom(e);
    5.         }

    6.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    7.         {
    8.             System.Diagnostics.Debug.WriteLine("Nav To");

    9.             base.OnNavigatedTo(e);
    10.         }
    复制代码
    在页面构造函数中添加如下代码
    1. 太长了 放不下 详见原文 :http://www.wpdevn.com/showtopic-17.aspx

    2011年12月21日 18:23

全部回复