【问题标题】:Using of PhotoChooserTaskPhotoChooserTask 的使用
【发布时间】:2012-05-15 15:14:26
【问题描述】:

我的 Windows Phone 应用程序中有一个图像列表(作为内容)。如何使用 PhotoChooserTask 查看它们?

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    这里是通过单击按钮触发任务的基本粗略示例。

    以下代码使用按钮单击事件来触发 PhotoChooserTask(),然后将选定的图像放入图像控件中。

    你需要参考任务使用

    using Microsoft.Phone.Tasks;
    

    然后使用如下代码

    public MainPage()
        {
        InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            }
            private void photochooserbtn_Click(object sender, RoutedEventArgs e)
             {
             photoChooserTask.Show();
             }
            void photoChooserTask_Completed(object sender, PhotoResult e)
             {
             if (e.TaskResult == TaskResult.OK)
             {
             System.Windows.Media.Imaging.BitmapImage bmp =new     System.Windows.Media.Imaging.BitmapImage();
             bmp.SetSource(e.ChosenPhoto);
             imagecontrol.Source = bmp;
             }
           }
    

    【讨论】:

    • 谢谢,但我有一张图片列表,我需要查看所有图片
    • 抱歉,我以为您想将时间图像保存在设备上。 Shawn Kendrot 的答案应该适合您想要做的事情,因为照片选择器任务不是您需要的。
    • 为什么他们很难找到这样的信息......?我在谷歌上搜索了几个小时,没有成功
    【解决方案2】:

    如果您已经有应用程序预加载的图像,那么您可以像这样显示它们:

    <ListBox ItemsSource="{Binding Images}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}" Width="200"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    这假定您页面的数据上下文有一个名为“Images”的属性

    public IEnumerable<Uri> Images { get; set; }
    

    并且您正在像这样填充该属性:

    Images = new List<Uri>
        {
            new Uri("Images/image1.png", UriKind.Relative),
            new Uri("Images/image2.png", UriKind.Relative),
            new Uri("Images/image3.png", UriKind.Relative),
            new Uri("Images/image4.png", UriKind.Relative)
        };
    

    这假设您已将图像放在项目中的“Images”文件夹下,并且 Build Action 设置为 Content

    上述解决方案将为您提供图像的垂直列表。如果您想让它变得更好一点,请使用 WP7 工具包并将 ListBoxItemsPanel 更改为 WrapPanel

       <ListBox.ItemsPanel>
           <ItemsPanelTemplate>
               <toolkit:WrapPanel/>
           </ItemsPanelTemplate>
       </ListBox.ItemsPanel>
    

    【讨论】:

      【解决方案3】:

      您不能使用 PhotoChooserTask 直接查看您添加到项目中的图片(作为内容)...要使其正常工作,您必须将图片保存到图片库(使用 MediaLibrary类)在启动 PhotoChooserTask 之前!

      【讨论】:

      • 谢谢,你能给我一个例子吗?
      • 这就是我的意思:PhotoChooserTask 只显示来自 MediaLibrary 的图片,这样您就可以“免费”获得!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      相关资源
      最近更新 更多