WP应用程序架构--------学习笔记(5)

基于Windows Phone 7的sliverlight应用都包含几个标准空间类:

  1,一个从Application类继承的App类

  2,一个PhoneAppliccationFrame类得实例

  3,一个或多个PhoneApplicationPage派生的类。

先来学习简单的页面导航

MainPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Text="send to page2" HorizontalAlignment="Center" VerticalAlignment="Center"
                       ManipulationStarted="TextBlock_ManipulationStarted" /> 
</Grid>

MainPage.xaml.cs

        Random rand = new Random();

        private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {  
            //导航到相对的位置
            this.NavigationService.Navigate(new Uri("/secondPage.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }
        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {  
            //触摸屏幕时随机换取背景颜色
            ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));
            base.OnManipulationStarted(e);
        }

在新建一个页面 sencondPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Text="go to one1" ManipulationStarted="TextBlock_ManipulationStarted" HorizontalAlignment="Center" VerticalAlignment="Center">
            </TextBlock>
</Grid>

sencondPage.xaml.cs

Random r = new Random();
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    this.NavigationService.GoBack();
    e.Complete();
    e.Handled = true;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
    ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255)));
    base.OnManipulationStarted(e);
}
运行效果是:点击文本时页面导航,点击文本以外的内容是屏幕随机换取背景颜色

运行上面实例我们发现一个问题,当一个页面(红色背景)导航到另一个页面时在从另一个页面返回时页面颜色不一致。这是为什么呢?因为window phone的页面导航是基于stack(栈)的,当源页面(调用Navigation的页面)导航到目标页面(destionnation)时,源页面放进栈中,目标页面显示出来。当调用back(或者用户按back按钮是)目标页面被抛弃,位于栈顶的元素将被弹出来。我们可以做一个实验:把sencondPage.xaml.cs的

this.NavigationService.GoBack();改变为this.NavigationService.Navigate(new Uri("/secondPage.xaml", UriKind.Relative));看看是不是有多个页面放入了栈中。答案是,创建了新的实例放入栈中。所以在页面设计的时候我们应该要注意这些问题。现在问题又来了,页与页之间好像没有什么联系,那如何在页与页之间传递数据呢?这里windows phone7为我们提供一种解决方案类似于html页面之间的传值,我们现在改写上面得代码,让源页面导航到目标页面时,在源页面的设置的值来初始化目标页面的背景颜色。
MainPage.xaml.cs 代码改变如下

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    string destination = "/secondPage.xaml";
    if (ContentPanel.Background is SolidColorBrush)
    {
        Color clr = (ContentPanel.Background as SolidColorBrush).Color;
        destination += String.Format("?red={0}&green={1}&blue={2}", clr.R, clr.G, clr.B);
    }
    //导航到相对的位置
    this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));
    e.Complete();
    e.Handled = true;
}

sendcondPage.xaml.cs 增加代码

//重写了onNavigateTo方法 说明:OnNavigatedTo定义在Page类中,page类派生了phoneApplicationPage类
//页面生成后就立刻调用OnNavigatedTo方法,这是页面的构造函数一直执行完成,其它函数还没有执行。
       protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
       {
           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));
           }
           base.OnNavigatedTo(e);
       }
我们可以初始化目标页面的背景颜色了,现在问题又来了,目标页面的背景色改变时,如何让源页面也改变呢?

我们可以想:所有的页面页面都可很方便的访问到从Applicaction派生的App类通过静态属性APPlication.current我们可以得到当前程序的Appliacation对象,然后简单的转换为app类型,然后利用app类型来保存数据,让其所有页面共享数据。具体我们这样来做:

首先编写app.xaml.cs文件夹

App.xaml.cs

      //为什么有?号,该属性定义为一个可为空的color对象,因为如果用户选择的配色方案是light,color默认保存
       //的是默认黑色透明色,这种情况下就会出错。
       /// <summary>
       /// 页面间共享颜色的公共属性
       /// </summary>
       public Color? sharedColor { get; set; }
MainPage.xaml.cs中在背景色改变时存储颜色

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{  
    //触摸屏幕时随机换取背景颜色
    ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));
    //触摸时将颜色保存到共享属性中。
    if (ContentPanel.Background is SolidColorBrush)
    {
        (Application.Current as App).sharedColor = (ContentPanel.Background as SolidColorBrush).Color;
    }
    base.OnManipulationStarted(e);
}

//重写OnNavigatedTo读取颜色

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
      {
          Color? share = (Application.Current as App).sharedColor;
          if (share != null)
          {
              ContentPanel.Background = new SolidColorBrush(share.Value);
          }
          base.OnNavigatedTo(e);
      }

secondPage.xaml.cs加载后马上加载共享属性的颜色

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    Color? sharedcolor = (Application.Current as App).sharedColor;
    if (sharedcolor != null)
    {
        ContentPanel.Background = new SolidColorBrush(sharedcolor.Value);
    }
    base.OnNavigatedTo(e);
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)

    //在触摸textblock时将本页面的颜色保存到共享属性中
     if (ContentPanel.Background is SolidColorBrush)
     {
         (Application.Current as App).sharedColor = (ContentPanel.Background as SolidColorBrush).Color;
     }
     this.NavigationService.GoBack();
     e.Complete();
     e.Handled = true;
}

通过修改代码,我们运行看看两页面之间是否能互相影响。,从运行结果来看。能满足。

今天就学习到这儿,明显来看上面的页面值得共享过于麻烦了,我们明白将学习简单的共享数据的方法,还会学习页面状态,独立存储,程序墓碑花处理,都是重要的知识。

 

错误地方希望大牛指出。。。。

posted @ 2012-04-18 23:32  Liuxiucai  阅读(356)  评论(0)    收藏  举报