首先,我有多个按钮将我发送到另一个页面,我希望在第二页上显示此按钮的ImageBrush
。 x:Name
,Tag
和Content
等字段已经忙碌,因此我无法使用它们。我的按钮是这样的:
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
有什么想法吗?也许除了Tag
,Name
和Content
之外,还有其他字段可以存储此路径?
答案 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 是您想要的值!