天天看点

Windows Phone 8开发知识笔记

1、Windows Phone 8目前支持3种屏幕分辨率,分别是

WVGA  800X480 (15:9)

WXGA  1280X768 (15:9)

720p    1280X720 (16:9)

2、在Windows Phone 8中,当BackgroundAudioPlayer的状态更改时,可以从PlayStateChangedEventArgs中捕获有关状态改变的信息。

3、要使页面支持旋转,要把PhoneApplicationPage的SupportedOrientations属性改为PortraitOrLandscape,然后可以通过定义OrientationChanged事件来处理布局。

<phone:PhoneApplicationPage

...

SupportedOrientations="PortraitOrLandscape"   

Orientation="Portrait"  

OrientationChanged="PhoneApplicationPage_OrientationChanged">

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)

        {

            MessageBox.Show("屏幕发生旋转,当前是:" + e.Orientation);

            // 如果是横向的  

            if (e.Orientation == PageOrientation.Landscape ||

                e.Orientation == PageOrientation.LandscapeLeft ||

                e.Orientation == PageOrientation.LandscapeRight)

            {

                Grid.SetColumn(this.img, 0);

                Grid.SetRow(this.img, 0);

                Grid.SetRow(this.txtBlock, 0);

                Grid.SetColumn(this.txtBlock, 1);

            }

            // 如果是纵向  

            else if (e.Orientation == PageOrientation.Portrait ||

                e.Orientation == PageOrientation.PortraitDown ||

                e.Orientation == PageOrientation.PortraitUp)

                Grid.SetRow(this.txtBlock, 1);

                Grid.SetColumn(this.txtBlock, 0);

            else

        } 

状态有PortraitUp、LandscapeLeft、LandscapeRight

4、文本显示控件TextBlock,里面文字显示不同颜色,通过<Run  Foreground="Red">Windows Phone 8</Run>标签实现,文字中间换行,使用<LineBreak/>  标记,如下:

 <TextBlock x:Name="txtBlock" 

                FontSize="70"  

                Margin="28">  

                <Run Foreground="Coral">Hello Kitty!</Run>  

                <LineBreak/>  

                <Run Foreground="Yellow">Windows Phone 8</Run>  

                <Run Foreground="SkyBlue">WebGIS</Run>

</TextBlock>

5、页导航

 <HyperlinkButton Content="跳到页面二" Height="78" HorizontalAlignment="Left" Margin="126,86,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="216" FontSize="32" FontStyle="Normal" FontStretch="Normal"  

                      NavigateUri="/pageSecond.xaml"/> 

private void button1_Click(object sender, RoutedEventArgs e)

{

            this.NavigationService.Navigate(new Uri("/pageSecond.xaml", UriKind.Relative));

}

6、OnNavigatedFrom 方法和OnNavigatedTo 方法。

      1)、当用户即将离开当前页时,将调用OnNavigatedFrom 方法;

      2)、当用户从其它页面导航到该页面时调用OnNavigatedTo 方法。

       // 离开主页面  

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)

            base.OnNavigatedFrom(e);

            System.Diagnostics.Debug.WriteLine("***** 已离开主页面。");

        }

        // 导航到第二个页面  

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

            base.OnNavigatedTo(e);

            System.Diagnostics.Debug.WriteLine("*****  Hi,已经来到第二个页面了。");

7、页之间传递参数

 this.NavigationService.Navigate(new Uri("/pageSecond.xaml?str=" + textBox1.Text, UriKind.Relative));

 // 导航到第二个页面  

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

      base.OnNavigatedTo(e);

     // 传递的参数叫什么名字,这里就按什么名字来取。  

     string pv = this.NavigationContext.QueryString["str"];

     this.textBlock1.Text = pv;

8、如何屏蔽掉“回退”按钮?

这种情况下不多见,屏蔽掉回退按钮意味着无法通过按下“回退”进行向后导航,这个做法要慎用。

要完成该操作,就得处理BackKeyPress事件,把事件参数e的Cancel属性设置为true即可取消“回退”键的操作。

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)

    base.OnBackKeyPress(e);

     e.Cancel = true;

9、如何删除导航历史记录?

比如说,我现在从主页导航到页面B,再从页面B导航到页面C,但我不希望用户导航回页面B,而是直接导航回主页。

我们要在导航的回退历史记录中删除页面B,所以,我们在离开页面B后把历史记录删除。也就是说,在页面B中重写OnNavigatedFrom方法。

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)  

    {  

        base.OnNavigatedFrom(e);  

        PhoneApplicationFrame myFrame = Application.Current.RootVisual as PhoneApplicationFrame;  

        if (myFrame != null)  

        {  

            try  

            {  

                myFrame.RemoveBackEntry();  

            }  

            catch (InvalidOperationException ex)  

                MessageBox.Show(ex.Message);  

        }  

    } 

从例子中看到,使用PhoneApplicationFrame类的RemoveBackEntry方法删除最新一条记录,每次只删除一条,要删除多条,就调用N次。因为导航历史记录是栈结构的,后进先出,所以,就像你拿一堆书放在桌面上一样,首先拿掉的是放在最上面的,如下图所示:

Windows Phone 8开发知识笔记

10、使用State保存页面的暂态数据

在页面填写数据的

继续阅读