BackgroundImage not working

I'm having trouble with the background image in xamarin.forms. I've tried the backgroundImage line before and after the this.content = layout;

I've also put the image into \resources\drawable and all the different densities (drawable-hdpi, xhdpi, etc) for android.

    namespace MyNameSpace
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                var layout = new StackLayout { Padding = new Thickness(5, 10) };
                this.Title = "myapptitle";

                layout.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, FontSize = 24, Text = "my label" });

                this.Content = layout;

                this.BackgroundImage = "bg.png";

What an I missing?

Tagged:

Best Answer

  • bugtusslebugtussle US
    Accepted Answer

    This was caused by my inexperience with visual studio.

    I put the files in the folder in windows explorer but did not reference them in visual studio. I thought it would see an image in there and add it to the project automatically. The clue was that the filename was not visible in the resources\drawable folder in visual studio. :/

    hopefully this helps someone else in the future

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    It wouldn't be the first I've seen that failed when done from C# but works in XAML.
    UI should be created in XAML, not code behind. Give that a shot.

    It should go without saying that you included the image in the right resource directory of the platform project. But we all forget now and then. Or put it in Android and forget to put in iOS.

  • bugtusslebugtussle USMember
    Accepted Answer

    This was caused by my inexperience with visual studio.

    I put the files in the folder in windows explorer but did not reference them in visual studio. I thought it would see an image in there and add it to the project automatically. The clue was that the filename was not visible in the resources\drawable folder in visual studio. :/

    hopefully this helps someone else in the future

Sign In or Register to comment.