这两个组件都属于选择器,而且它们也有很多相似的地方,最明显的上一点,它们都是用来选择图片。

 

 

一、CameraCaptureTask选择器。

 

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

 

  1. <Grid>

  2. <Grid.RowDefinitions>

  3. <RowDefinitionHeight="*"/>

  4. <RowDefinitionHeight="auto"/>

  5. </Grid.RowDefinitions>

  6. <Imagex:Name="img"Grid.Row="0"Stretch="Uniform"

  7. HorizontalAlignment="Stretch"

  8. VerticalAlignment="Stretch"/>

  9. <Buttonx:Name="btnCamera"Grid.Row="1"

  10. Content="启动相机程序"Click="btnCamera_Click"/>

  11. </Grid>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="img" Grid.Row="0" Stretch="Uniform"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"/>
        <Button x:Name="btnCamera" Grid.Row="1"
                Content="启动相机程序" Click="btnCamera_Click"/>
    </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. CameraCaptureTask MyCamera = new CameraCaptureTask(); 

  20. // 构造函数

  21. public MainPage() 

  22. InitializeComponent(); 

  23. // 第二步,在页面构造函数中注册完成回调事件

  24. MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed); 

  25. privatevoid btnCamera_Click(object sender, RoutedEventArgs e) 

  26. // 第三步,显示组件

  27. MyCamera.Show(); 

  28. // 第四步,处理事返回结果

  29. void MyCamera_Completed(object sender, PhotoResult e) 

  30. // 确定用户确认了还是取消了操作。

  31. if (e.TaskResult == TaskResult.OK) 

  32. // 从返回的流中创建图象

  33. BitmapImage bmp = new BitmapImage(); 

  34. try

  35. bmp.SetSource(e.ChosenPhoto); 

  36. // 把图象作为Image控件的源。

  37. // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。

  38. Dispatcher.BeginInvoke(() => 

  39. this.img.Source = bmp; 

  40. }); 

  41. catch (Exception ex) 

  42. MessageBox.Show(ex.Message); 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
// 引入以下命名空间。
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 第一步,声明类级别的局部变量,并实例化。
        CameraCaptureTask MyCamera = new CameraCaptureTask();

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 第二步,在页面构造函数中注册完成回调事件
            MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed);
        }

        private void btnCamera_Click(object sender, RoutedEventArgs e)
        {
            // 第三步,显示组件
            MyCamera.Show();
        }

        // 第四步,处理事返回结果
        void MyCamera_Completed(object sender, PhotoResult e)
        {
            // 确定用户确认了还是取消了操作。
            if (e.TaskResult == TaskResult.OK)
            {
                // 从返回的流中创建图象
                BitmapImage bmp = new BitmapImage();
                try
                {
                    bmp.SetSource(e.ChosenPhoto);
                    // 把图象作为Image控件的源。
                    // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。
                    Dispatcher.BeginInvoke(() =>
                    {
                        this.img.Source = bmp;
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }
    }
}


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

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask _Windows Phone开发(23):

 

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask _Windows Phone开发(23):_02

 

 

 

二、PhotoChooserTask选择器。

 

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

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

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

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

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

 

  1. <Grid>

  2. <Grid.RowDefinitions>

  3. <RowDefinitionHeight="*"/>

  4. <RowDefinitionHeight="auto"/>

  5. </Grid.RowDefinitions>

  6. <Imagex:Name="img"Stretch="Uniform"HorizontalAlignment="Stretch"VerticalAlignment="Stretch"Grid.Row="0"/>

  7. <GridGrid.Row="1">

  8. <Grid.ColumnDefinitions>

  9. <ColumnDefinitionWidth="auto"/>

  10. <ColumnDefinitionWidth="auto"/>

  11. <ColumnDefinitionWidth="auto"/>

  12. <ColumnDefinitionWidth="auto"/>

  13. </Grid.ColumnDefinitions>

  14. <Grid.RowDefinitions>

  15. <RowDefinitionHeight="auto"/>

  16. <RowDefinitionHeight="auto"/>

  17. </Grid.RowDefinitions>

  18. <TextBlockGrid.Column="0"Grid.Row="0"Text="高度:"/>

  19. <TextBlockGrid.Column="2"Grid.Row="0"Text="宽度:"/>

  20. <TextBoxx:Name="txtHeight"Grid.Column="1"

  21. Grid.Row="0"Width="160"Height="auto"FontSize="20">

  22. <TextBox.InputScope>

  23. <InputScope>

  24. <InputScopeNameNameValue="Number"/>

  25. </InputScope>

  26. </TextBox.InputScope>

  27. </TextBox>

  28. <TextBoxx:Name="txtWidth"Grid.Column="3"

  29. Grid.Row="0"Width="160"Height="auto"FontSize="20">

  30. <TextBox.InputScope>

  31. <InputScope>

  32. <InputScopeNameNameValue="Number"/>

  33. </InputScope>

  34. </TextBox.InputScope>

  35. </TextBox>

  36. <CheckBoxx:Name="chkShowCamera"

  37. Grid.Row="1"Grid.ColumnSpan="2"

  38. Content="显示启动相机"/>

  39. <Buttonx: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>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="img" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"/>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="高度:"/>
            <TextBlock Grid.Column="2" Grid.Row="0" Text="宽度:"/>
            <TextBox x:Name="txtHeight" Grid.Column="1"
                     Grid.Row="0" Width="160" Height="auto" FontSize="20">
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <TextBox x:Name="txtWidth" Grid.Column="3"
                     Grid.Row="0" Width="160" Height="auto" FontSize="20">
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <CheckBox x:Name="chkShowCamera"
                      Grid.Row="1" Grid.ColumnSpan="2"
                      Content="显示启动相机"/>
            <Button x:Name="btnShow"
                    Grid.Column="2" Grid.Row="1"
                    Grid.ColumnSpan="2"
                    Content="选择图片..."
                    Margin="5"
                    Click="btnShow_Click"/>
        </Grid>
    </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. PhotoChooserTask ptc = new PhotoChooserTask(); 

  19. public Page1() 

  20. InitializeComponent(); 

  21. ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed); 

  22. void ptc_Completed(object sender, PhotoResult e) 

  23. if (e.TaskResult == TaskResult.OK) 

  24. BitmapImage bmp = new BitmapImage(); 

  25. try

  26. bmp.SetSource(e.ChosenPhoto); 

  27. Dispatcher.BeginInvoke(() => { 

  28. this.img.Source = bmp; 

  29. }); 

  30. catch (Exception ex) 

  31. MessageBox.Show(ex.Message); 

  32. privatevoid btnShow_Click(object sender, RoutedEventArgs e) 

  33. // 设置相关属性

  34. ptc.PixelHeight = int.Parse(txtHeight.Text); 

  35. ptc.PixelWidth=int.Parse(txtWidth.Text); 

  36. ptc.ShowCamera = this.chkShowCamera.IsChecked.HasValue ? chkShowCamera.IsChecked.Value : false

  37. ptc.Show(); 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;

namespace PhoneApp1
{
    public partial class Page1 : PhoneApplicationPage
    {
        PhotoChooserTask ptc = new PhotoChooserTask();

        public Page1()
        {
            InitializeComponent();

            ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed);
        }

        void ptc_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmp = new BitmapImage();
                try
                {
                    bmp.SetSource(e.ChosenPhoto);
                    Dispatcher.BeginInvoke(() => {
                        this.img.Source = bmp;
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            // 设置相关属性
            ptc.PixelHeight = int.Parse(txtHeight.Text);
            ptc.PixelWidth=int.Parse(txtWidth.Text);
            ptc.ShowCamera = this.chkShowCamera.IsChecked.HasValue ? chkShowCamera.IsChecked.Value : false;

            ptc.Show();
        }
    }
}

 

 

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask _Windows Phone开发(23):_03

 

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask _Windows Phone开发(23):_04

 

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask _Windows Phone开发(23):_05

http://blog.csdn.net/tcjiaan/article/details/7396621