【问题标题】:How to get location in background of button c#如何在按钮c#的背景中获取位置
【发布时间】:2015-07-14 08:55:32
【问题描述】:

我在 xaml 中有一个这样的按钮

<button>
  <button.background>
    <imagebrush imagesource="/Assets/Image1xxx.png"/>
  </button.background>
</button>

但是现在在代码后面(cs)中,我如何从任何事件中获取字符串“Assets/Image1xxx.png”。帮我。我正在尝试使用 button_name.Background.ImageBrush.Uri(),现在正在工作

【问题讨论】:

    标签: windows xaml button location


    【解决方案1】:

    您必须获取图像源。这是一个位图图像。比你可以得到 UriSource 这将是你的图像刷的图像源。试试这样的:

        <Button Tapped="SimpleButton_Tapped">
                <Button.Background>
                    <ImageBrush ImageSource="Assets/SmallLogo.png"/>
                </Button.Background>
            </Button>
    

    在后面的代码中:

      private void SimpleButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (sender is Button)
            {
                Button button = sender as Button;
                ImageBrush imageBrush = button.Background as ImageBrush;
                BitmapImage image = new BitmapImage(new Uri("ms-appx:/Assets/otherIcon.png"));
                imageBrush.ImageSource = image;
    
                BitmapImage bitmapImage = imageBrush.ImageSource as BitmapImage;
                String imageSource = bitmapImage.UriSource + "";
                Debug.WriteLine("The new imageSource of the button is: " + imageSource);
            }
        }
    

    这将为您提供以下输出:

    图像来源:ms-appx:/Assets/otherIcon.png

    【讨论】:

    • 但我没有定义名称 x:Name="BtnImageSource",因为我有很多按钮,我想为所有按钮设置 1 个事件,以更改该按钮的背景
    • 我更改了代码,它应该适用于您的每个按钮
    • 好的,我同意,但我错了,现在我有 4 个按钮,我想为所有人举办一个活动,并且在里面 if (thisbutton.background.image==image1) image=image2 else image =图 1。你站在我这边吗?
    猜你喜欢
    • 2012-02-29
    • 2016-08-12
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2012-04-07
    相关资源
    最近更新 更多