2016-08-15 97 views
4

我想将背景图片分配给我在xaml中设计的内容。我尝试了不同的方法,但没有向我展示背景图片。这是我写的代码:Xamarin Forms - 如何将背景图片添加到内容页面

内容页:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="Demo.Welcome" 
      BackgroundImage="bg1.jpg"> 

    <ContentPage.Content> 
    <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Spacing="25" Padding="0" x:Name="stackLayoutMain"> 
     <Label FontSize="40" Text="WelCome Page" HorizontalOptions="Start" VerticalOptions="StartAndExpand"></Label> 
     <Entry Placeholder="Activation Key" WidthRequest="200" FontSize="15" HorizontalOptions="Center" VerticalOptions="StartAndExpand" TextChanged="entryActivationTextChanged" x:Name="entryActivationKey" Keyboard="Numeric" HorizontalTextAlignment="Center"></Entry> 
     <Button 
     x:Name="buttonActivate" 
     BackgroundColor="#fff" 
     Text="Activate" 
     WidthRequest="100" 
     HeightRequest="50" 
     HorizontalOptions="Center" 
     VerticalOptions="StartAndExpand" 
     TextColor="#377CC1" 
     IsVisible="False" 
     Clicked="buttonActivateClicked" > 
     </Button> 
    </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

bg1.jpg是在我的便携式投影影像,我想设置为背景。我将财产

  • Build Action = "Content"
  • Copy to Output Directory = "Copy Always"

而下面是我的项目的目录结构。

Directory Structure

+0

1.你有生成操作作为“嵌入式资源”的图像? 2.它不适用于这两个平台? 3.尝试为网络使用图片,看它是否有效。 – BraveHeart

回答

12

我的建议是把图像文件夹:

的AndroidResources/drawable

的iOSResources

+0

你是对的:) – user2988458

+0

根据屏幕大小,你可能需要使用的不仅仅是可绘制的文件夹 – Darren

1
public class MainPage : ContentPage 
{ 
    List<string> objEmpCollections = new List<string>(); 
    public MainPage() 
    { 
     this.Title = "V V I P's"; 
     //this.BackgroundColor = Color.Maroon; 
     this.BackgroundImage = "EmpMgmtPCL.Images.NPO-Events-bg.png"; 
    } 
} 
+0

仅供参考这个问题表明他们使用XAML,所以答案应该是这样。 – Darren

相关问题