简体   繁体   中英

How to set background image of ContentPage in Xamarin

I am trying to set a background image on my Xamarin app. I saw already Xamarin Forms ContentPage background image tiling article. At that code, has set a background image from the URL.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SuperForms.Samples.Page1">
  <ContentView>
    <Image Source="https://i.stack.imgur.com/ajyCo.png"
           Aspect="AspectFill"/>
  </ContentView>
</ContentPage>

I want to set from local Resources. So I have modified the XAML file like the following code. But It doesn't work. How to should I fix the following code.

I want to set the background image via only the XAML file. not via C# code.

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SuperForms.Samples.Page1">
  <ContentView>
    <Image Source="Image/background.png"
           Aspect="AspectFill"/>
  </ContentView>
</ContentPage>

This is my Xamarin solution structure.

在此处输入图片说明

I want to build my app on Android, iOS, and Windows. Please let me know in detail because I am a beginner at Xamarin.

Best regards.

According to xamarin document:

Image files can be added to each application project and referenced from Xamarin.Forms shared code. This method of distributing images is required when images are platform-specific, such as when using different resolutions on different platforms, or slightly different designs.

And on andriod.Place images in the Resources/drawable directory with Build Action: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi). see the link below:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM