从XAML获取按钮发件人ImageBrush.ImageSource

时间:2014-06-15 11:01:15

标签: c# xaml windows-phone-8

首先,我有多个按钮将我发送到另一个页面,我希望在第二页上显示此按钮的ImageBrushx:NameTagContent等字段已经忙碌,因此我无法使用它们。我的按钮是这样的:

XAML

<Button x:Name="but01" Click="Button2_Click" Tag="this tag is bussy" Content="First Button" >
    <Button.Background>
        <ImageBrush x:Name="brush1" ImageSource="/Assets/Logos/1.png" Stretch="Uniform" />
    </Button.Background>
</Button>

代码背后:

private void Button2_Click(object sender, EventArgs e)
{
    //this is what I've tried
    var brush = new ImageBrush();
    brush = (ImageBrush)((sender as Button).Background);
    string mynew = brush.ImageSource.ToString();
    NavigationService.Navigate(
        new Uri("/MainPage.xaml?path=" +
                HttpUtility.UrlEncode((sender as Button).Tag.ToString())+ "&source="+ mynew,
                UriKind.Relative));
}

mynew = System.Windows.Media.Imaging.BitmapImage 我希望它是:/Assets/Logos/1.png

有什么想法吗?也许除了TagNameContent之外,还有其他字段可以存储此路径?

1 个答案:

答案 0 :(得分:1)

您应该使用 BitmapImage 而不是 ImageSource

您可以像这样获得图像路径:

var brush = but01.Background as ImageBrush;
BitmapImage source = brush.ImageSource as BitmapImage;
Uri uri = source.UriSource;
string uriStr = uri.OriginalString;

字段 uriStr 是您想要的值!