本文介绍了从代码附加图像/后面的ImageBrush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个图像作为用户控件的背景。根据一个变量的值,我需要改变的背景,但无论我用,背景不改变路径或URI格式。

I'm trying to add an Image as the background of a UserControl. Depending on the value of a variable I need to change that background but whatever the path or Uri format I use, the background does not change.

我见过很多问题在这里的计算器,但没有解决我的一个问题。
我让下面的代码:

I've seen lots of questions here in stackoverflow but none fixes my single problem.I let the code below:

            if (callback.liveUvis.ContainsUVI(uvi))
            {
                this.Status.Text = "LIVE";


                ImageBrush imgB = new ImageBrush();
                BitmapImage btpImg = new BitmapImage();
                btpImg.UriSource = new Uri(@"///IMG///Live///bck_frame_info_video_live.png", UriKind.Relative);
                //imgB.ImageSource = new BitmapImage(new Uri("~/IMG/Live/bck_frame_info_video_live.png", UriKind.RelativeOrAbsolute));
                //imgB.ImageSource = new BitmapImage(new Uri("ms-appx:///IMG/Live/bck_frame_info_video_live.png"));
                imgB.ImageSource = btpImg;
                this.Background = imgB;
            }



试图附加一个图像,当我面临同样的问题?我想这是到URI格式,还可以,但我让代码太以防万一:)

I'm facing the same problem when trying to attach an image... I guess it's up to the Uri format also, but I let the code too just in case :)

    private void setIcon_Desc(string dd)
    {
        try
        {
            Image img = new Image();
            img.Source = new BitmapImage(new Uri(this.BaseUri, "IMG/pictos_small/white/160dpi/" + dd + ".png"));
            img.Stretch = Stretch.None;
            this.Icon = img;
            this.Sport.Text = callback.disc.getDescription(dd).ToUpper();
        }
        catch(Exception ex)
        {
            callback.exception.writeExceptions(ex);
        }

    }

在此先感谢!

推荐答案

更改用户控件的背景时,我可以重现您的问题。

I can reproduce your issue when changing the background of a user control.

目前的解决方法我用正在改变在控制根UIElement的背景。

The current workaround I used was changing the background of root UIElement in the control.

<Grid x:Name="container">
    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="Images/bg-blue.png"/>
    </Grid.Background>
    <StackPanel>
        <TextBlock>Hello World</TextBlock>
        <Button Click="Button_Click">Change Background</Button>
        <Image x:Name="display"></Image>
    </StackPanel>
</Grid>



public sealed partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ImageBrush imgB = new ImageBrush();

        BitmapImage btpImg = new BitmapImage();

        btpImg.UriSource = new Uri(@"ms-appx:///images/bg-light-blue.png");

        imgB.ImageSource = btpImg;

        container.Background = imgB;
    }
}

这篇关于从代码附加图像/后面的ImageBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:28