在Windows Phone上获取图像的文件路径

本文关键字:文件 路径 图像 获取 Windows Phone | 更新日期: 2023-09-27 18:30:20

我正在开发一个Windows Phone 8应用程序,并且已经有一些图片填充到这样的列表框中

<ListBox x:Name="Control" ItemsSource="{Binding Pictures}">
    <ListBox.ItemTemplate>
         <DataTemplate>
              <Grid Background="WhiteSmoke" Margin="10">
                    <Image Source="{Binding Source}" Margin="10"/>
              </Grid>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

视图模型在妈妈那里很简单

public class MainPageViewModel : NotificationObject
{
    private ObservableCollection<Picture> _pictures;
    public MainPageViewModel()
    {
        Pictures = new ObservableCollection<Picture>
            {
                new Picture
                    {
                        Source = new BitmapImage(new Uri("../Images/Pictures/1.jpg", UriKind.Relative))
                    }
                 //Add more images here
            };
    }
    public ObservableCollection<Picture> Pictures
    {
        get { return _pictures; }
        set
        {
            _pictures = value;
            RaisePropertyChanged(() => Pictures);
        }
    }
}

我现在希望通过点击图像,用户可以获得共享选项

 void ShowShareMediaTask(object sender, GestureEventArgs e)
 {
      ShareMediaTask shareMediaTask = new ShareMediaTask();
      shareMediaTask.FilePath = //something needs to go here
      shareMediaTask.Show();
 }

任何想法如何获得此图像的物理(完整)路径?

在Windows Phone上获取图像的文件路径

您似乎正在引用存储在应用程序文件夹(在项目中)中的图像,因此ShareMediaTask无法访问它。

ShareMediaTask要求照片位于媒体库中。

您需要做的是将照片保存到媒体库中,然后使用保存图像的路径调用ShareMediaTask(不要忘记添加using Microsoft.Xna.Framework.Media.PhoneExtensions;以访问GetPath()扩展方法)。

var picture = mediaLibrary.SavePicture(fileName, stream);
shareMediaTask = new ShareMediaTask();
shareMediaTask.FilePath = picture.GetPath(); // requires using Microsoft.Xna.Framework.Media.PhoneExtensions;
shareMediaTask.Show();

是的...您可以按照以下代码生成列表框的点击事件

private void Control_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {

        ObservableCollection<Picture> pictureobj=new ObservableCollection<Picture>();
        ListBox lst = (ListBox)sender;
        int i = lst.SelectedIndex;
        if (lst.SelectedValue == null)
        {
        }
        else
        {
            Pictures obj = (Pictures)lst.SelectedValue;
           ShareMediaTask shareMediaTask = new ShareMediaTask();
           shareMediaTask.FilePath = obj.yoursetterimagepath
           shareMediaTask.Show();
        }
    }

希望对您有所帮助