您的位置:首页 > 其它

Windows Phone 开发学习笔记(四) Hello Windows Phone之心有灵犀

2012-04-22 12:20 741 查看
经过上一篇对页面布局的调整,对XAML文件有点了解。不过在看XAML文件和它对应的CS文件时发现一个问题:XAML文件与CS文件中控件的名称是共用的。而在CS文件中没有看到像using MainPage.xaml或者其他特别的语句,难道CS文件与XAML文件心有灵犀?下面是MainPage.xaml.cs文件的主要代码:

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

private void ShowMessage_Click(object sender, RoutedEventArgs e)
{
HelloText.Text = "Hello Windows Phone";
}
}
}
首先要关注的是MainPage类的构造函数,因为其中包含类的初始化过程,看看它具体做了什么。双击选中InitializeComponent,右击选择Go to Definition。



VS自动转到一个名为MainPage.g.i.cs的文件,它的代码如下:

namespace HelloWindowsPhone {

public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage {

internal System.Windows.Controls.Grid LayoutRoot;

internal System.Windows.Controls.Grid MessagePanel;

internal System.Windows.Controls.TextBlock HelloText;

internal System.Windows.Controls.Button ShowMessage;

private bool _contentLoaded;

/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/HelloWindowsPhone;component/MainPage.xaml", System.UriKind.Relative));
this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
this.MessagePanel = ((System.Windows.Controls.Grid)(this.FindName("MessagePanel")));
this.HelloText = ((System.Windows.Controls.TextBlock)(this.FindName("HelloText")));
this.ShowMessage = ((System.Windows.Controls.Button)(this.FindName("ShowMessage")));
}
}
}
看来在构造MainPage实例时,首先检测内容是否已经载入,如果没有载入,那么就开始解析对应的XAML文件,根据XAML文件初始化各个控件。

到这里才有点明白,MainPage类的定义其实被分别放在MainPage.xaml.cs和MainPage.g.i.cs中,而MainPage.xaml文件在构造MainPage类时才会被载入。看来XAML文件和CS文件的变量名能共用只不过是表面现象,貌合神离啊。既然这样,如果我更改MainPage.g.i.cs和MainPage.xaml.cs里的代码,让MainPage类里成员名称统一,那么就算CS文件和XAML文件里的控件名称不同,程序还是能正常运行。

做个实验验证一下。将MainPage.xaml.cs和MainPage.g.i.cs里HelloText控件的名称都改为HelloText1,按F5开始调试,正常运行,看来推测是正确的。不过在调试过程中却弹出如下对话框:



提示MainPage.g.i.cs被更改,需要重新载入。可是在调试过程中我什么都没做,为什么会出现这个提示呢?点击Yes发现,MainPage.g.i.cs里控件的名称又变得和XAML一样了。看来这个改变和XAML文件脱不了关系。之前只顾上看MainPage.g.i.cs的代码,没注意它从哪冒出来的。接下来关注下另一方,XAML。

在对XAML文件的主要代码进行分析时,曾经掐头去尾一部分。尾巴只是注释,不用管。头那部分就是下面的代码:

x:Class="HelloWindowsPhone.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
感觉唯一与CS文件有关系的就是第一行:

x:Class="HelloWindowsPhone.MainPage"
因此请出MSDN解答。搜索xaml x Class,第一条就是关于这条语句的介绍

http://msdn.microsoft.com/zh-cn/library/cc189082(v=VS.95).aspx

简单的说,这个属性用来配置标记分部类(markup partial class)和功能分部类(code partial class)的联接,而标记分部类是VS根据XAML文件自动生成的。又是VS,幕后英雄啊。对MainPage来说,MainPage.g.i.cs里的类是标记分部类,MainPage.xaml.cs里的类是功能分部类。编译时,它们组合成MainPage类。整个过程整理为下图:



虽然解决了开始的问题,不过对MainPage.xaml“头”的研究只不过才刚开始,里面还有很多东西需要挖掘。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐