Windows Phone7 基本常识(来自Programming Windows Phone7)
XNA全屏幕: this.graphics.IsFullScreen = true ; //这条语句最好放在Initialize中,如果放在构造函数中也许会出现问题哦;
增加PortraitUp支持:this.graphics.SupportedOrientations
判断:
this.Windows.CurrentOrientation == ??(这是只读的属性)
增加地理坐标:GeoCoordinateWatcher;需要添加System.Device引用
using System.Device.Location ;
浏览不同的Page:
this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
给Page传递参数数:和HTML一样:
接受参数时:增加using System.window.Navigation ;
protected override void OnNavigatedTo(NavigationEventArgs args) { IDictionary<string, string> parameters = this.NavigationContext.QueryString; if (parameters.ContainsKey("Red")) { byte R = Byte.Parse(parameters["Red"]); byte G = Byte.Parse(parameters["Green"]); byte B = Byte.Parse(parameters["Blue"]); ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, R, G, B)); } 111 base.OnNavigatedTo(args); }
好生奇怪,我在事件属性中没有找到这个Navigate的事件,只能手动添加.
5. 在全局对象App中共享不同页面的数据,呵呵,感觉就像是windows编程对话框之间传递数据一样,一般定义一个全局结构体,用来传递数
据;
看看在windows中怎么实现的
://Slierlight //在App类中添加共享变量 //public property for sharing data among pages public Color? ShareColor;
添加变量之后就可以操纵这个变量了。
protected override void OnNavigatedTo(NavigationEventArgs e) { Color? clr = (Application.Current as App).ShareColor; if (clr != null) { this.ContentPanel.Background = new SolidColorBrush(clr.Value); } base.OnNavigatedTo(e); }
这里需要注意的是可空类型的用法,可空类型变量只能赋予可空类型变量,如果赋值给非可空类型变量,请使用value属性。
6.不想使用app
那么可以使用OnNavigatedFrom函数
在这个函数中,可以对想要的页面加载之前对共有成员进行赋值,感觉类似于Asp.net界面
protected override void OnNavigatedFrom(NavigationEventArgs e) { if (this.ContentPanel.Background is SolidColorBrush) { ReturnedColor = ((SolidColorBrush)(this.ContentPanel.Background)).Color; if (ReturnedColor != null) { if (e.Content is MainPage) { ((MainPage)e.Content).ReturnedColor = ReturnedColor; } } } base.OnNavigatedFrom(e); }
7.使用状态:
State可以存取变量,在TombStoning机制中,有效(就是会恢复);
PhoneApplicationService.Current.State["Color"] = ((SolidColorBrush)(ContentPanel.Background)).Color;
需要增加using Microsoft.Phone.Shell;
8.使用IsolatedStorage在Sliverlight中;
using System.IO.IsolatedStorage;
private void LoadSetting() { IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings; if (setting.Contains("background")) { BackGround = new SolidColorBrush((Color)setting["background"]); } } private void SaveSetting() { if (BackGround != null) { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings["background"] = (object)(BackGround.Color); settings.Save(); } }
9.在XNA中使用PhoneApplicationService
增加引用:
using Microsoft.Phone.Shell
using System.Windows ;
否则出现:
Error 1 The type 'System.Windows.IApplicationService' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. d:/users/rorger/documents/visual studio 2010/Projects/ch5/MyXNATomstoning/MyXNATomstoning/MyXNATomstoning/Game1.cs 130 13 MyXNATomstoning
错误
在XNA中game构造函数中:
PhoneApplicationService appService = PhoneApplicationService.Current; appService.Launching += OnAppServiceLaunching; appService.Activated += OnAppServiceActivated; appService.Deactivated += OnAppServiceDeactivated; appService.Closing += OnAppServiceClosing;
这个和TombStoning有点像,但是不是TombStoning。因为appService只是一个引用,而不是新建的对象;
看看xml反序列化:
IsolatedStorageFileStream stream = storage.OpenFile(fileName, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(Settings)); settings = serializer.Deserialize(stream) as Settings; stream.Close();
增加:
using System.xml;
using System.xml.Serialization.
10.给windows Phone7 的appbar增加button:
图标可以在C:/Program Files/Microsoft SDKs/Windows Phone/v7.0/Icons目录中找到。
在工程中添加Images文件夹,然后添加图标,点击图标的属性,然后点击 Build action-》content;
这样你就可以引用图标了。
<shell:ApplicationBarIconButton IconUri="Images/appbar.delete.rest.png" Text="delete" x:Name="buttonDelete" Click="buttonDelete_Click"/>
ch8:
旋转/缩放/偏移
在属性RenderTransform中设置吧
时钟:DispartcherTimer 需要命名空间 using System.windows.Threading
Slverlight内置的动画支持:使用静态类
CompositionTarget.Rendering += new EventHandler(OnCompositionTargetRendering);
void OnCompositionTargetRendering(object sender, EventArgs e)
{
RenderingEventArgs args = e as RenderingEventArgs;
...
}
手势缩放:
使用ManipulationDelta事件:
private void OnEclipseManipulationDelta(object sender, ManipulationDeltaEventArgs e) { CompositeTransform compositeTransform = (sender as Ellipse).RenderTransform as CompositeTransform; if (e.DeltaManipulation.Scale.X > 0 && e.DeltaManipulation.Scale.Y > 0) { double scale = Math.Min(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y); compositeTransform.ScaleX *= scale; compositeTransform.ScaleY *= scale; } compositeTransform.TranslateX += e.DeltaManipulation.Translation.X; compositeTransform.TranslateY += e.DeltaManipulation.Translation.Y; }
注意 scale是变化系数,而不是最终的,在原来的基础上进行相乘;
平移也一样。
图像掩码:
使用 <Image.OpacityMask> </Image.OpacityMask> 中间是画刷