Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Best way to set image source on every page

$
0
0

What is the best way to set the app background programatically on every page?

Currently when my app loads, it retrieves an image from the filesystem and whenever a new page is loaded, I pass the image to the next page through its constructor and manually set the image source on that page to be this image. Example:

(First page)

public partial class Login : PageParent
{
    private Image appBackground = new Image();

    ...

    private async Task LoginExistsAsync()
    {
        appBackgroundBytes = await FileManager.LoadImage(null, "appBackground");
        appBackground.Source = ImageSource.FromStream(() => new System.IO.MemoryStream(appBackgroundBytes));
        GoToMainPage();
    }

    void GoToMainPage()
    {
        Navigation.PushAsync(new MainPage(appBackground));
        Navigation.RemovePage(this);
    }
}

(Second Page)

    public MainPage(Image appBackground)
    {
        InitializeComponent();
        this.appBackground = appBackground;
        background_image.Source = appBackground.Source;
    }

Where background_image is the x:Name of my XML image, which is placed and constrained the same way a background image would be.

The implications of this method are that on every page the image loads slightly later - which looks tacky. There must be a better way of doing this... I have tried implementing control templates, with the hope that I can define it at the start of the app and have it persist until the app is closed, but the control template is re-defined on every instance call, so the same issue would occur.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>