Setting image from gallery as dynamic background image to pages

KrunalKrunal USMember ✭✭

Hello,

I am developing app using Xamarin.Forms. I have a requirement where in user can select any image from gallery and should be able to set as background image on all the pages.

  1. I understand there is a backgroundImage property in Page control to which background can be set however its value can only be assigned to inbuilt resources images (i.e. images which are part of project as resource) and I can't assign the gallery image here. If I'm not correct then pls point me the way how to use non resources image.

  2. Also, I want the set background Image to all the pages once setting is done. How I can apply the image to all the pages simultaneously?

Any pointer/help will be much appreciated.

Regards,

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    1. Use a ControlTemplate for your pages. Then you can define common aspects in one place.
  • KrunalKrunal USMember ✭✭

    @ClintStLaurent
    I'm not clear how controltemplate will help? "BackgroundImage" property on page is not bindable property. So how I can dynamically change the image without binding?

    Or you are saying that I use the Image control inside my template and bind ImageSource?

    If you can provide any code snippet then it will be great.

    Thanks

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm doing an upgrade so I can't test this for about an hour...
    But can't you set the backgroundimage in the ControlTemplate just like you would on a single page?
    Then all your pages that use that template will therefore have that same image?

  • KrunalKrunal USMember ✭✭

    @ClintStLaurent But my image selection is dynamic i.e. there will be some button and upon clicking the button, I will allow selection of the image from gallery.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Krunal said:
    @ClintStLaurent But my image selection is dynamic i.e. there will be some button and upon clicking the button, I will allow selection of the image from gallery.

    I wasn't attempting to answer question 1. Just question 2 "How do I apply the same image to all pages"

    1. Use a ControlTemplate for your pages. Then you can define common aspects in one place.

    ControlTemplate is the basic solution for applying a consistent set of layout to all elements using that template.

    Question 1 is more complex. As you said you can't use binding. And you have to access the gallery images. So yeah, most likely you're going to have to abandon use of the backgroundimage property and place an image which is bindable, and layout your own backgroundimage instead.

Sign In or Register to comment.