6

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;
            }

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);
        }

    }

Thanks in advance!

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Eva FP
  • 775
  • 9
  • 24
  • Is the image included in the project? Make sure the compile action is set to "Content" – Kevin Gosse Oct 15 '15 at 09:49
  • @KooKiz yes, I have a folder with subfolders and images in the project (I'm aware that in Android it's not possible to use images if they are in subfolder, I ignore if it's the same for UWP...). When you say "Make sure the Build Action is set to 'Content'" you mean the image right? – Eva FP Oct 15 '15 at 10:41
  • Yes, in the file properties (in Visual Studio's solution explorer, right click in the file, click on properties, and there should be a file named "Build Action"). If the build action is set to content, then you should be able to open the file using something like `` (that is, if your picture is in the "Live" subfolder of the "IMG" folder). There may be a few subtleties when using an `Uri` object, so I suggest first trying directly from the XAML using the code I provided – Kevin Gosse Oct 15 '15 at 11:21
  • I've already checked if the image's build action was set to "Content" and it was... The thing is that I have to add the image from the code behind... I have no choice there :( But I have used images in other pages and I've been able to add them from XAML, the problem is from the CS file – Eva FP Oct 15 '15 at 11:24
  • Alright, then we can focus on the uri format. You may want to try this: http://stackoverflow.com/a/9562069/869621 it's the `ms-appx` scheme but with a single / – Kevin Gosse Oct 15 '15 at 12:09
  • it doesn't work either :'( – Eva FP Oct 15 '15 at 15:36

1 Answers1

5

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

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;
    }
}
Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22