2012-03-31 37 views
0

我想要在拖動圖像時在手機屏幕上查找圖像的URI。 我有很多圖像在屏幕上,我需要知道哪一個被拖動,我可以很容易地從圖像的文件名中找到它們,因爲它們被命名爲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()); 
    } 

這將返回System.Windows.Controls.Image。 另一方面,這引發了一個異常,指出ConvertToString尚未實現。

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

有沒有什麼辦法可以做到這一點?

回答

1

如果您知道Source屬性爲BitmapImage(它有可能是,如果你傳遞一個URI的Image在XAML的Source屬性),那麼你可以使用UriSource:

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

我建議您可以使用Image控件的Tag屬性來存儲所需的信息。從長遠來看,可擴展性更強。

+0

非常感謝,標籤屬性正是我一直在尋找的東西,但沒有意識到它。 – nikhil 2012-03-31 13:58:04