第一个Silverlight Phone程序

  和普通C#工程一样,创建一个Windows Phone Application,如下图:
   
  在创建好的工程中可以看到Windows Phone程序的机构如下:

  +WindowsPhoneFirstDemo(工程名字)
     +Properties
        -AppManifest.xml
        -AssemblyInfo.cs
        -WMAppManifest.xml
     +References
     +APP.xaml
       -ApplicationIcon.png
       -Background.png
     +MainPage.xaml
       -SplashScreenImage.jpg 
  其中Properties中的三个文件通常不需要修改,如果希望修改在Windows Phone 7的安装程序列表中的名字,可以修改 WMAppManifest.xml文件中App节点中的Title属性,如:Title="Hello",这样在安装程序列表中会显示Hello,而不是先前的WindowsPhoneFirstDemo。这样我们第一个Silverlight Phone应用程序就创建完成了,按下F5,就会启动模拟器运行这个程序。 

现在我们开看看APP.xaml文件:

App.xaml
<Application
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"
x:Class
="WindowsPhoneFirstDemo.App">

<!--Application Resources-->
<Application.Resources>
</Application.Resources>

<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated
="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>

和普通的Silverlight程序一样,根节点中包含了命名空间的定义,但是要指出的是,xmlns:phone和xmlns:shell只有在Windows Phone程序中才会出现。
再看一下App.xaml.cs:

App.xaml.cs
public partial class App : Application
{
......
/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
// Global handler for uncaught exceptions.
// Note that exceptions thrown by ApplicationBarItem.Click will not get caught here.
UnhandledException += Application_UnhandledException;

// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;

// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;

// Enable non-production analysis visualization mode,
// which shows areas of a page that are being GPU accelerated with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
}

// Standard Silverlight initialization
InitializeComponent();

// Phone-specific initialization
InitializePhoneApplication();
}
......
}

可以发现App是一个partial类,但是另外的部分在哪儿呢?如果我们跳转到InitializeComponent函数的定义,可以发现会在IDE中打开一个名为App.g.i.cs,原来另一部在编译器生成的中间文件中。(但是生成了两个中间文件App.g.cs和App.g.i.cs,两个文件内容一样,谁能告诉我这是什么用意?)
现在我们打开MainPage.xaml,可以直观地看到一个手机模型,可以在上面拖放控件来组织程序布局,在上面创建的工程的MainPage.xaml中我们添加一个TextBlock,并设置其属性,如下:

MainPage.xaml
<phone:PhoneApplicationPage
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"
xmlns:Microsoft_Phone_Controls_Maps_Overlays
="clr-namespace:Microsoft.Phone.Controls.Maps.Overlays;assembly=Microsoft.Phone.Controls.Maps"
mc:Ignorable
="d" d:DesignWidth="480" d:DesignHeight="800"
x:Class
="WindowsPhoneFirstDemo.MainPage"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible
="False">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Windows Phone First Demo" Style="{StaticResource PhoneTextNormalStyle}" Opacity="0.8">
<TextBlock.Foreground>
<RadialGradientBrush>
<GradientStop Color="White"/>
<GradientStop Color="#FFE9E99B" Offset="1"/>
</RadialGradientBrush>
</TextBlock.Foreground>
</TextBlock>
<TextBlock x:Name="PageTitle" Text="Hello WP" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}">
<TextBlock.Foreground>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.9"/>
<GradientStop Color="White"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ContentControl Content="Hello" Margin="199,24,198,255" FontSize="26" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>  

运行该程序,可以看到文字"Hello"显示在手机模拟其中:

posted @ 2010-12-03 11:06  GyyCyy.Xap  阅读(455)  评论(0)    收藏  举报