简体   繁体   English

Windows Phone 8 imagebrush获取路径

[英]windows phone 8 imagebrush getting path

i have this code 我有这个代码

 <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>

while tapping the textblock the tap event will be called.how can i get the complete path of the imagesource in this tap event in code behind? 在点击文本块时,将调用tap事件。如何在后面的代码中获取此tap事件中图像源的完整路径? i am really at the lost here how to get to the imagesource in my codebehind event. 我真的在这里迷失了如何在我的代码隐藏事件中到达图像源。

One solution could be the following one : 一种解决方案可能是以下一种:

In your XAML code, you can bind your TextBlock.Tag property to the Grid.Background property you need. 在XAML代码中,可以将TextBlock.Tag属性绑定到所需的Grid.Background属性。 It could be useful to do it in XAML rather than code behind if your Grid is not the first parent (no need to find the required parent with recursive C# code) : 如果您的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>

Then in your code behind, you just have to cast and use the TextBlock.Tag property that way : 然后在后面的代码中,您只需要以这种方式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;
    }
 }

Try this. 尝试这个。

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.

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