locked
What is the syntax for setting same background image for all pages?

    Question

  • I know I can set one background image for all xaml pages in the app.xaml.cpp.  I've only found the syntax for cs, but no c++.  Please help on this.  I will be eternally grateful.
    Wednesday, July 24, 2013 6:43 AM

Answers

  • This seems to work.  (I did not try it on multiple pages though.)

    		auto imageBrush = ref new ImageBrush();
    		imageBrush->Stretch = Stretch::UniformToFill;
    		auto uri = ref new Uri("ms-appx:///Assets/cat2.jpeg");
    		auto bitmapImage = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
    		imageBrush->ImageSource = bitmapImage;
    		rootFrame->Background = imageBrush;
    BTW, make sure the background on the top level Grid on your pages is not covering up the background you set here.


    • Edited by jrboddie Thursday, July 25, 2013 2:41 AM added
    • Marked as answer by RandyPete Thursday, July 25, 2013 5:32 AM
    Wednesday, July 24, 2013 10:29 PM

All replies

  • I'm having a hard time figure out how to write out the code for this.  Everything I tried came out in error.  What do I put inside the imagebrush?

    auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
    
    	// Do not repeat app initialization when the Window already has content,
    	// just ensure that the window is active
    	if (rootFrame == nullptr)
    	{
    		// Create a Frame to act as the navigation context and associate it with
    		// a SuspensionManager key
    		rootFrame = ref new Frame();
    
    		// set the app background image
    		rootFrame->Background = ref new ImageBrush (
                
    			
    
            );

    Wednesday, July 24, 2013 4:34 PM
  • This seems to work.  (I did not try it on multiple pages though.)

    		auto imageBrush = ref new ImageBrush();
    		imageBrush->Stretch = Stretch::UniformToFill;
    		auto uri = ref new Uri("ms-appx:///Assets/cat2.jpeg");
    		auto bitmapImage = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
    		imageBrush->ImageSource = bitmapImage;
    		rootFrame->Background = imageBrush;
    BTW, make sure the background on the top level Grid on your pages is not covering up the background you set here.


    • Edited by jrboddie Thursday, July 25, 2013 2:41 AM added
    • Marked as answer by RandyPete Thursday, July 25, 2013 5:32 AM
    Wednesday, July 24, 2013 10:29 PM
  • This seems to work.  (I did not try it on multiple pages though.)

    		auto imageBrush = ref new ImageBrush();
    		imageBrush->Stretch = Stretch::UniformToFill;
    		auto uri = ref new Uri("ms-appx:///Assets/cat2.jpeg");
    		auto bitmapImage = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
    		imageBrush->ImageSource = bitmapImage;
    		rootFrame->Background = imageBrush;
    BTW, make sure the background on the top level Grid on your pages is not covering up the background you set here.


    Works great with 10+ pages.  My app  has been somewhat stuttering on my test devices because of the background image reloading every time I switch pages.  This 6 lines of code got rid of the stuttering.

    You, sir, are a saint.


    • Edited by RandyPete Thursday, July 25, 2013 3:53 AM spelling
    Thursday, July 25, 2013 3:52 AM