wp7学习笔记 之一

Emulator中文叫仿真器,是精确地在一种环境下仿真另一种环境下的运行方式。其目的是完全仿真。Simulator中文叫模拟,是通过某种手段来模拟某些东西,不一定要完全正确的原理,追求的只是尽可能的相像。所以XDE是WP7的Emulator,是一个WP7的虚拟机,我准备写一个XP下开发WP7程序的Simulator。

1、XBox和WP7之间的游戏移植很方便。
2、WP7程序不能运行在后台,不支持多任务。
3、C#是唯一支持的开发语言,不能使用native code,不能PInvoke
4、WP7使用Zune连接电脑,不再支持通过ActiveSync的联系人的同步,而是通过网络同步联系人。
5、使用高级Task的时候必须对模拟器进行Unlock,因为没有Unlock的话模拟器的内容很少。
6、WP7首页上的方框叫Tile,
7、一个程序只能有一个Frame,是所有页面的容器,为SystemTray和ApplicationBar预留空间
8、显示的方向通过Orientation属性来设置:Portrait是竖向,Landscape是横向;SupportedOrientations属性设置支持的方向(默认值是Portrait,也就是只支持竖向。如果设置为PortraitOrLandscape则在转向的时候界面会自动调整);当转向的时候OrientationChanged事件会被触发。
9、WP7中页面的跳转不再是Show这种方式,而是页面Navigation跳转,类似于网页:this.NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relative));this.NavigationService.GoBack();this.NavigationService.GoForward();
跳转的时候还可以填值:this.NavigationService.Navigate(new Uri("/Page1.xaml?name=tome",UriKind.Relative));
页面中获取值:this.NavigationContext.QueryString["name"];

可以override OnNavigatedFrom、OnNavigatedTo、OnNavigatingFrom几个方法进行导航的响应。

在普通SL中执行这个的话需要使用“导航Silverlight”模板,参考:http://www.cnblogs.com/steven_gsj/archive/2010/04/24/1719265.html 如果导航失败的话是设置UriMapper的原因,清空即可。NavigationService在Page中才有。

10、shell:SystemTray.IsVisiable属性控制SystemTray可见性
11、ApplicationBar就是菜单栏。ApplicationBarIconButton属性IconUrl、Text,图标要求是48*48png图,前景色一般是白色,微软在C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Icons中提供了一些常用的图标.ApplicationBarIconButton最多只能添加4个,引进图片的时候一定要注意在VS选择图片的属性“BuildAction=Content”、“如果较新则复制”。MenuItems也是放到ApplicationBar中的,MenuItems没有图片、没有二级菜单。

12、Style="{StaticResource PhoneTextNormalStyle}"引用的是资源。代码中用Application.Current.Resources["name"]来加载资源。尽量不要使用自己的颜色,而是引用系统资源中定义的颜色,这个和CSS原理类似。默认系统资源在C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Design\ThemeResources.xaml下

13、独立存储(IsolatedStorage)只有本程序可以访问该区域,安全性高,默认大小为2GB,不能长期保存数据。位于System.IO.IsolatedStorage命名空间下,solatedStorageFile、solatedStorageFileStream两个类。
创建IsolatedStorageFile的方法IsolatedStorageFile.GetUserStoreForApplication(),IsolatedStorageFile类有CreateDirectory()、DirectoryExists()等很多有用的方法。
创建读写流的方法:IsolatedStorageFileStream stream = new IsolatedStorageFileStream("3.txt", FileMode.OpenOrCreate,
IsolatedStorageFile.GetUserStoreForApplication())

14、在Tile中的图片是在项目的属性中修改的。
15、如果采用Grid布局,相当于ColumnDefinitions和RowDefinitions将界面划分成了网格,然后元素通过Grid.Column、Grid.Row属性来设置它显示在哪个格子中。
16、Canvas布局中代码修改元素位置的方法Canvas.SetLeft(img1,30);
17、Popup容器可以将内部的内容放到顶层显示,IsOpen属性用来标志是否显示。因为SL中页面显示一般是用导航的,但是如果想实现WinForm中对话框的效果就可以用Popup
18、ScrollView包含的内容会可以滚动。
19、HyperlinkButton是一个用于本地、Web的超链接。NavigationUri指向地址,如果指向的是Web地址,则必须设定Target属性。HyperlinkButton可以给Background属性设定ImageBrush来显示图片超链接。
20、用模拟器的时候按Pause键就可以用电脑键盘输入了。
21、TextBox设定InputScope属性可以让软键盘换不同的布局,设置为TelephoneNumber会显示为数字键盘,设置为Url键盘会显示“.com”按钮。所有可选值位于InputScopeNameValue这个枚举中。
22、Image控件只支持png和jpg图片,可以通过Source属性显示本地或者网络图像,网络图片直接填路径就行了,不用通过代码先下载。通过代码加载this.img1.Source = new BitmapImage(new Uri("Desert.jpg",UriKind.Reltive));this.img1.Source = new BitmapImage(new Uri("http://www.baidu.com/Desert.jpg",UriKind.Absolute));
23、MediaElement能够播放本地或者网络上的多媒体,支持WAV、MP3等,支持mms、rtspt、rtsp等流媒体协议。主要属性:IsMuted,是否静音;Stretch,填充方式;AutoPlay:自动播放。事件:MediaOpend、MediaEnded、MediaFailed。
24、推荐使用DispatcherTimer来做定时。因为如果用Timer的话在Timer的Elapsed中访问UI的话需要同步到UI线程执行,而DispatcherTimer是运行在Dispatcher上的,所以访问UI的时候不需要委托到UI线程执行。
25、Thumb是一个可拖动的控件,简化了拖动的开发,Thumb是默认没有再ToolBox中的,需要ChooseItem选进来。通过DragCompleted、DragDelta、DragStarted事件来响应。
当一个Thumb拖动的时候跟着鼠标走:

<Thumb Height= "87" HorizontalAlignment= "Left" Margin= "89,163,0,0" Name= "thumb1" VerticalAlignment= "Top"
Width= "91" DragCompleted= "thumb1_DragCompleted" DragDelta= "thumb1_DragDelta">
<Thumb.RenderTransform>
<TranslateTransform x:Name= "tr"></TranslateTransform>
</Thumb.RenderTransform>
</Thumb>
private void thumb1_DragDelta( object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
tr.X = tr.X+e.HorizontalChange;
tr.Y = tr.Y+e.VerticalChange;
}

在Silverlight中一般不直接修改元素的坐标、大小,在需要修改元素的坐标、大小的时候一般使用Transform来做。
26、ListBox通过ItemTemplate来定制外观,Template内部用Text="{Binding Text}"进行绑定
绑定的对象的类要是public的,否则会绑定失败

<ListBox Height="361" HorizontalAlignment="Left" Margin="60,165,0,0" Name="listBox1" VerticalAlignment="Top" Width="311" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="100" Height="100" Source="{Binding Avatar}"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public ImageSource Avatar { get; set; }
}

List<Person> list = new List<Person>();
list.Add(new Person { Name="Tom",Age=20,Avatar=new BitmapImage(new Uri("Images/1.jpg",UriKind.Relative))});
list.Add(new Person { Name = "Jim", Age = 22, Avatar = new BitmapImage(new Uri("Images/2.jpg", UriKind.Relative)) });
listBox1.ItemsSource = list;

ImgSource属性是ImageSource类型,ImageSource的一个子类是BitmapImage
27、InkPresenter可以产生手写效果。事件:LostMouseCapture、MouseLeftButtonDown、MouseMove。
鼠标画图的典型代码:

private Stroke stroke;
private void inkPresenter1_MouseLeftButtonDown( object sender, MouseButtonEventArgs e)
{
inkPresenter1.CaptureMouse();
stroke = new Stroke();
stroke.DrawingAttributes.Color = Colors.Red;
inkPresenter1.Strokes.Add(stroke);
}

private void inkPresenter1_MouseMove( object sender, MouseEventArgs e)
{
stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter1));
}

28、WP7中可以使用BMSL来使用BingMaps
29、微软发布了Silverlight控件之外的Silverlight Toolkit,有wp7的版本。提供了额外的控件:AutoCompleteBox、ContextMenu、DatePicker、TimePicker、LongListSelector、ListPicker、ToggleSwitch(很好看的开关)、WrapPanel等控件
1)ContextMenu控件提供了上下文菜单,长按屏幕弹出,因为手机中没有右键。ContextMenu不能用在容器控件中

< TextBox Height= "72" HorizontalAlignment= "Left" Margin= "17,109,0,0" Name= "textBox1" Text= "TextBox"
VerticalAlignment= "Top" Width= "384">
< toolkit:ContextMenuService.ContextMenu>
< toolkit:ContextMenu>
< toolkit:MenuItem Header= "复制" />
< toolkit:Separator/>
< toolkit:MenuItem>
< toolkit:MenuItem.Header>
< Image Width= "30" Height= "30" Source= "ApplicationIcon.png" />
< /toolkit:MenuItem.Header>
< /toolkit:MenuItem>
< /toolkit:ContextMenu>
< /toolkit:ContextMenuService.ContextMenu>
< /TextBox>

ContextMenu还可以设定Margin属性,这时候Margin属性用来改变菜单相对于控件的位置。
2)WrapPanel会将放到其中的控件按照从左到右、从上到下的顺序依次排列。
3)DatePicker、TimePicker是非常易用的日期、时间选择器,而且可以通过ValueStringFormat来修改显示格式,语法就是DateTime.ToString的格式语法。
4)ListPicker和ListBox的不同在于,如果为FullMode那么当点击ListPicker的时候会弹出一个全屏窗口来选择,用起来更方便。
5)LongListSelector用来对数据进行快速的分类定位,非常方便,用起来比较复杂,看例子就行。

30、Panorama控件是一个全景视图控件,像一个窗口,能够查看内容的一部分。全景视图由Background、Title、Items组成。可以自己加这些控件,也可以用项目向导中的“WindowsPhone Panorama Application”快速创建一个完整的Panorama控件项目,用来学习很方便。用法看向导生成的代码即可。
31、Pivot控件是一个竖向滚动的控件。
32、WP7中可以使用DeepZoom技术。
wp7社区 http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series
33、Blend的使用:
1)向左下角倾斜的按钮是渐变工具,可以设置控件的渐变。渐变有线性渐变、径向渐变两种。
2)笔刷变换工具是对控件的Brush进行变形的工具,和控件变形不一样。
3)转换(Transform)中的RenderTransfer可以用来实现旋转、缩放、位移等效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值