簡體   English   中英

從XAML獲取按鈕發件人ImageBrush.ImageSource

[英]Get button sender ImageBrush.ImageSource from XAML

首先,我有多個將我發送到另一頁的按鈕,我希望第二頁上顯示該按鈕的ImageBrush x: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

有任何想法嗎? 也許還有其他字段可以存儲此路徑,但“ Tag ,“ Name和“ Content除外?

您應該使用BitmapImage而不是ImageSource

您可以這樣獲得圖像路徑:

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

uriStr字段是您想要的值!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM