代码改变世界

wp8.1 学习笔记 001 动态生成图片 并更改图片位置

2015-05-06 18:52  kiddfu  阅读(206)  评论(0编辑  收藏  举报

1、在xaml中划分表格

 1 <Grid Name="gr">
 2         <Grid.ColumnDefinitions>
 3             <ColumnDefinition></ColumnDefinition>
 4             <ColumnDefinition></ColumnDefinition>
 5             <ColumnDefinition></ColumnDefinition>
 6             <ColumnDefinition></ColumnDefinition>
 7             <ColumnDefinition></ColumnDefinition>
 8         </Grid.ColumnDefinitions>
 9         <Grid.RowDefinitions>
10             <RowDefinition></RowDefinition>
11             <RowDefinition></RowDefinition>
12             <RowDefinition></RowDefinition>
13             <RowDefinition></RowDefinition>
14             <RowDefinition></RowDefinition>
15         </Grid.RowDefinitions>
16 
17     </Grid>

2、在后台中生成图片并 通过计时器 改变在表格中的位置

 1 public sealed partial class MainPage : Page
 2     {
 3         Image im = new Image();
 4         int i = 0;
 5         Random rand = new Random();
 6         public MainPage()
 7         {
 8             this.InitializeComponent();
 9 
10             this.NavigationCacheMode = NavigationCacheMode.Required;
11             
12         }
13         
14         /// <summary>
15         /// 在此页将要在 Frame 中显示时进行调用。
16         /// </summary>
17         /// <param name="e">描述如何访问此页的事件数据。
18         /// 此参数通常用于配置页。</param>
19         protected override void OnNavigatedTo(NavigationEventArgs e)
20         {
21             // TODO: 准备此处显示的页面。
22 
23             // TODO: 如果您的应用程序包含多个页面,请确保
24             // 通过注册以下事件来处理硬件“后退”按钮:
25             // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
26             // 如果使用由某些模板提供的 NavigationHelper,
27             // 则系统会为您处理该事件。
28 
29             //生成定时器
30             DispatcherTimer timer = new DispatcherTimer();
31             timer.Tick += timer_Tick;
32             timer.Interval = new TimeSpan(0,0,1);
33             timer.Start();
34             
35 
36             //指定图片
37             im.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Logo.scale-240.png"));
38 
39             //将图片加入Grid
40             gr.Children.Add(im);
41         }
42 
43         void timer_Tick(object sender, object e)
44         {
45             //throw new NotImplementedException();
46 
47             //移除显示图片
48             //bool b = gr.Children.Remove(im);
49 
50             //随机显示图片
51             int row = rand.Next(0,5);
52             int col = rand.Next(0, 5);
53             Grid.SetRow(im,row);
54             Grid.SetColumn(im, col);
55 
56             //图片循环显示
57             //Grid.SetRow(im, i);
58             //Grid.SetColumn(im, i);
59             //i++;
60             //i=(i == 5)?( i = 0) : i;
61         }
62     }