简体   繁体   English

如何获取GestureCompleted上图像的来源

[英]How do I get the Source of an Image on GestureCompleted

I want to find the URI of an Image on the phone screen when the image is dragged. 我想在拖动图像时在手机屏幕上找到图像的URI。 I have many images on the screen and I need to know which one has been dragged, I can easily find that from the filename of the image as they are named A.png, B.png and so on . 屏幕上有很多图像,我需要知道拖动了哪一个图像,我可以从图像的文件名中轻松找到它们,因为它们的名称分别为A.png, B.png and so on

    private void OnGestureCompleted(object sender, Microsoft.Phone.Controls.GestureEventArgs e){
        Image image = sender as Image;
        TranslateTransform transform = image.RenderTransform as TranslateTransform;
        //storing the final values after the gesture is complete
        xFin = (e.GetPosition(null).X);
        yFin = (e.GetPosition(null).Y);
        //failed attempts to convert adress to string
        MessageBox.Show(image.Source.ToString());
    }

This returns System.Windows.Controls.Image . 这将返回System.Windows.Controls.Image and this on the other hand throws an exception stating that ConvertToString hasn't been implemented. 另一方面,这引发了一个异常,指出尚未实现ConvertToString。

ImageSourceConverter convertor = new ImageSourceConverter();
string location = convertor.ConvertToString(image);
MessageBox.Show(location);

Is there some way this can be done? 有什么办法可以做到吗?

If you know that the Source property is BitmapImage (which it probably is if you are passing in a URI to the Image in the Source property in XAML) then you can use UriSource: 如果您知道Source属性是BitmapImage (如果您正在将URI传递给XAML中Source属性中的Image的话),则可以使用UriSource:

MessageBox.Show(((BitmapImage)image.Source).UriSource.ToString());

I suggest you use the Tag property of the Image control to store the info you need though. 我建议您使用Image控件的Tag属性来存储所需的信息。 More scalable robust in the long run. 从长远来看,可扩展性更强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM