日期:2014-05-17 浏览次数:21183 次
这两天在摸索WindowsPhone应用程序的开发,实现了一个简单的画图板应用程序和一个简单的手机端与电脑端的通信应用程序。今晚先总结一下自己实现的画图板,画图板能够实现基本的画直线,矩形,三角形和圆形,废话少说,先上图,下面是实现的画图板的截图:

?
首先是在Visual Studio2010中新建一个WindowsPhone应用程序项目,按照以前Java实现画图板的思路,首先是在界面上显示几个按钮和一个面板。在WindowsPhone中组件的添加和android中是差不多,都是以类似于XML标签的形式添加的,然后可以设置各个标签的属性值。在我的程序中是添加四个按钮和一个面板,具体的代码(MainPage.xaml文件)如下:
<phone:PhoneApplicationPage
x:Class="Drawer.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">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的画图板" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="画图" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="save" HorizontalAlignment="Stretch" Content="长方形"
Height="80" Background="Blue" FontFamily="Arial" FontSize="20"
Margin="-6,1,1,1" />
<Button Grid.Column="1" Name="reck" Content="直线" FontFamily="Arial" FontSize="20"
Height="80" Background="Blue" Margin="0,1" HorizontalAlignment="Right" Width="100" />
<Button Grid.Column="2" Name="line" HorizontalAlignment="Stretch" Content="圆形"
Height="81" Background="Blue" FontFamily="Arial" FontSize="20"
Margin="1,1,5,0" />
<Button Grid.Column="3" Name="rect" HorizontalAlignment="Stretch" Content="三角形"
Height="80" Background="Blue" FontFamily="Arial" FontSize="20"
Margin="1,1,1,1" />
</Grid>
<Grid Grid.Row="2">
<Canvas Name="canvas" Background="White">
</Canvas>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
?
需要注意的一点是<Grid.RowDefinitions></Grid.RowDefinitions>标签之间添加一行<RowDefinition Height="Auto" />,意思就是在原有界面上在划分出一行,因为原先刚创建工程时,界面上默认的只划分两行,同样可以以同样的方式添加更多的行。在一行中要定义列的话,是在<Grid.ColumnDefinitions></Grid.ColumnDefinitions>这一对标签之间添加,用<ColumnDefinition/>标签。
?
然后是要给各个组件添加监听器,添加监听器有两种方式,一种是以标签属性的方式添加的,就像写JS中的事件处理一样。另外一种是在MainPage.g.i.cs文件的