[英]windows phone 8 imagebrush getting path
我有這個代碼
<Grid Width="160" x:Name="grd">
<Grid.Background>
<ImageBrush x:Name="img" ImageSource="Assets/Icons/tab-inactive.png" />
</Grid.Background>
<TextBlock x:Name="txtacticve" Text="Rating" Tap="TextBlock_Tap_1" />
</Grid>
在點擊文本塊時,將調用tap事件。如何在后面的代碼中獲取此tap事件中圖像源的完整路徑? 我真的在這里迷失了如何在我的代碼隱藏事件中到達圖像源。
一種解決方案可能是以下一種:
在XAML代碼中,可以將TextBlock.Tag
屬性綁定到所需的Grid.Background
屬性。 如果您的Grid
不是第一個父對象,那么在XAML中而不是在后面的代碼中進行操作可能會很有用(無需使用遞歸C#代碼查找所需的父對象):
<Grid Width="160" x:Name="grd">
<Grid.Background>
<ImageBrush x:Name="img" ImageSource="Assets/Icons/tab-inactive.png" />
</Grid.Background>
<TextBlock x:Name="txtacticve" Text="Rating" Tap="TextBlock_Tap_1" Tag="{Binding ElementName=grd, Path=Background}" />
</Grid>
然后在后面的代碼中,您只需要以這種方式TextBlock.Tag
轉換並使用TextBlock.Tag
屬性:
private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
var textBlock = sender as TextBlock;
if (textBlock != null)
{
var test = ((BitmapImage)((ImageBrush)textBlock.Tag).ImageSource).UriSource.OriginalString;
}
}
嘗試這個。
private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
var grid = (Grid)txtacticve.Parent;
var img = grid.Background;
var path = ((BitmapImage)(((ImageBrush)(img)).ImageSource)).UriSource.AbsolutePath;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.