Windows Phone开发(23):启动器与选择器之二

移动开发
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验整合至手机中。

一、CameraCaptureTask选择器。

它用于启动照相机,当你拍下照片后,自动把照的字节流返回给调用方应用程序。前文说过,启动器和选择的使用方法和步骤都是一样的。对于 CameraCaptureTask组件也如此,不过注意的一点是,处理Completed事件时一定要记住,尽可能的使用页面类的 Dispatcher.BeginInvoke方法,因为异步回调直接访问UI元素是不安全的,极有可能会引发异常,但我不是说绝对。

  1. <Grid> 
  2.     <Grid.RowDefinitions> 
  3.         <RowDefinition Height="*"/> 
  4.         <RowDefinition Height="auto"/> 
  5.     </Grid.RowDefinitions> 
  6.     <Image x:Name="img" Grid.Row="0" Stretch="Uniform" 
  7.            HorizontalAlignment="Stretch" 
  8.            VerticalAlignment="Stretch"/> 
  9.     <Button x:Name="btnCamera" Grid.Row="1" 
  10.             Content="启动相机程序" Click="btnCamera_Click"/> 
  11. </Grid>
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. // 引入以下命名空间。 
  14. using Microsoft.Phone.Tasks; 
  15. using System.Windows.Media.Imaging; 
  16. namespace PhoneApp1 
  17.     public partial class MainPage : PhoneApplicationPage 
  18.     { 
  19.         // 第一步,声明类级别的局部变量,并实例化。 
  20.         CameraCaptureTask MyCamera = new CameraCaptureTask(); 
  21.  
  22.         // 构造函数 
  23.         public MainPage() 
  24.         { 
  25.             InitializeComponent(); 
  26.  
  27.             // 第二步,在页面构造函数中注册完成回调事件 
  28.             MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed); 
  29.         } 
  30.         private void btnCamera_Click(object sender, RoutedEventArgs e) 
  31.         { 
  32.             // 第三步,显示组件 
  33.             MyCamera.Show(); 
  34.         } 
  35.         // 第四步,处理事返回结果 
  36.         void MyCamera_Completed(object sender, PhotoResult e) 
  37.         { 
  38.             // 确定用户确认了还是取消了操作。 
  39.             if (e.TaskResult == TaskResult.OK) 
  40.             { 
  41.                 // 从返回的流中创建图象 
  42.                 BitmapImage bmp = new BitmapImage(); 
  43.                 try 
  44.                 { 
  45.                     bmp.SetSource(e.ChosenPhoto); 
  46.                     // 把图象作为Image控件的源。 
  47.                     // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。 
  48.                     Dispatcher.BeginInvoke(() => 
  49.                     { 
  50.                         this.img.Source = bmp; 
  51.                     }); 
  52.                 } 
  53.                 catch (Exception ex) 
  54.                 { 
  55.                     MessageBox.Show(ex.Message); 
  56.                 } 
  57.             } 
  58.         } 
  59.     } 

当然,在模拟器中你是不能进行拍摄的,但可以进行模拟操作,也就是说无论你拍的什么,最后都是返回同一张照片。

 

 别走开,下页更精彩!

 #p#

二、PhotoChooserTask选择器。

这个选择器已经包含CameraCaptureTask的功能,当然,它主要是为了选择图片。

1、ShowCamera属性设置是否显示可以让用户启动相机的按钮;

2、PixelHeight:选择图片后将其裁剪的高度;

3、PixelWidth属性与上面相同,裁剪宽度。

照片被选择后,以流的形式返回,驼过Completed事件的参数PhotoResult的ChosenPhoto属性获取。

  1. <Grid> 
  2.     <Grid.RowDefinitions> 
  3.         <RowDefinition Height="*"/> 
  4.         <RowDefinition Height="auto"/> 
  5.     </Grid.RowDefinitions> 
  6.     <Image x:Name="img" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"/> 
  7.     <Grid Grid.Row="1"
  8.         <Grid.ColumnDefinitions> 
  9.             <ColumnDefinition Width="auto"/> 
  10.             <ColumnDefinition Width="auto"/> 
  11.             <ColumnDefinition Width="auto"/> 
  12.             <ColumnDefinition Width="auto"/> 
  13.         </Grid.ColumnDefinitions> 
  14.         <Grid.RowDefinitions> 
  15.             <RowDefinition Height="auto"/> 
  16.             <RowDefinition Height="auto"/> 
  17.         </Grid.RowDefinitions> 
  18.         <TextBlock Grid.Column="0" Grid.Row="0" Text="高度:"/> 
  19.         <TextBlock Grid.Column="2" Grid.Row="0" Text="宽度:"/> 
  20.         <TextBox x:Name="txtHeight" Grid.Column="1" 
  21.                  Grid.Row="0" Width="160" Height="auto" FontSize="20"
  22.             <TextBox.InputScope> 
  23.                 <InputScope> 
  24.                     <InputScopeName NameValue="Number"/> 
  25.                 </InputScope> 
  26.             </TextBox.InputScope> 
  27.         </TextBox> 
  28.         <TextBox x:Name="txtWidth" Grid.Column="3" 
  29.                  Grid.Row="0" Width="160" Height="auto" FontSize="20"
  30.             <TextBox.InputScope> 
  31.                 <InputScope> 
  32.                     <InputScopeName NameValue="Number"/> 
  33.                 </InputScope> 
  34.             </TextBox.InputScope> 
  35.         </TextBox> 
  36.         <CheckBox x:Name="chkShowCamera" 
  37.                   Grid.Row="1" Grid.ColumnSpan="2" 
  38.                   Content="显示启动相机"/> 
  39.         <Button x:Name="btnShow" 
  40.                 Grid.Column="2" Grid.Row="1" 
  41.                 Grid.ColumnSpan="2" 
  42.                 Content="选择图片..." 
  43.                 Margin="5" 
  44.                 Click="btnShow_Click"/> 
  45.     </Grid> 
  46. </Grid>
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. // 
  14. using Microsoft.Phone.Tasks; 
  15. using System.Windows.Media.Imaging; 
  16. namespace PhoneApp1 
  17.     public partial class Page1 : PhoneApplicationPage 
  18.     { 
  19.         PhotoChooserTask ptc = new PhotoChooserTask(); 
  20.         public Page1() 
  21.         { 
  22.             InitializeComponent(); 
  23.             ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed); 
  24.         } 
  25.         void ptc_Completed(object sender, PhotoResult e) 
  26.         { 
  27.             if (e.TaskResult == TaskResult.OK) 
  28.             { 
  29.                 BitmapImage bmp = new BitmapImage(); 
  30.                 try 
  31.                 { 
  32.                     bmp.SetSource(e.ChosenPhoto); 
  33.                     Dispatcher.BeginInvoke(() => { 
  34.                         this.img.Source = bmp; 
  35.                     }); 
  36.                 } 
  37.                 catch (Exception ex) 
  38.                 { 
  39.                     MessageBox.Show(ex.Message); 
  40.                 } 
  41.             } 
  42.         } 
  43.         private void btnShow_Click(object sender, RoutedEventArgs e) 
  44.         { 
  45.             // 设置相关属性 
  46.             ptc.PixelHeight = int.Parse(txtHeight.Text); 
  47.             ptc.PixelWidth=int.Parse(txtWidth.Text); 
  48.             ptc.ShowCamera = this.chkShowCamera.IsChecked.HasValue ? chkShowCamera.IsChecked.Value : false
  49.             ptc.Show(); 
  50.         } 
  51.     } 
  52. }

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-19 15:22:31

Windows Pho启动器与选择器

2013-04-18 11:13:04

Windows Pho启动器与选择器BingMapsDir

2013-04-18 13:56:09

Windows Pho启动器与选择器

2013-04-18 13:47:43

Windows Pho启动器与选择器发送短信

2012-06-20 10:21:50

Windows Pho

2012-06-21 10:59:31

Windows Pho

2010-04-12 17:32:59

Windows Pho

2011-10-19 09:56:58

Gnome Pie程序启动器

2012-04-16 14:32:31

iOS选择器代码

2012-04-19 08:42:22

春Phone沙龙

2017-03-20 14:46:07

Android日期时间选择器

2011-11-28 13:42:55

Sencha Touc组件选择器

2012-12-27 14:08:39

Android开发颜色选择器

2009-06-30 13:58:00

Java启动器

2012-11-09 14:33:38

WindowsChrome

2013-12-02 14:22:14

jQuery选择器

2014-08-12 09:54:05

Android定制化启动器

2010-08-06 15:44:28

Windows PhoWindows PhoSilverlight

2009-07-16 11:02:33

Swing文件选择器

2013-03-11 10:30:56

CSSWeb
点赞
收藏

51CTO技术栈公众号