2017-04-03 10 views
0

Visual Studio 2015でXamarin Formsを使用してアプリケーションを作成しようとしています。画面/ページに背景画像を追加したいと思います。これまでのところ、以下のコードに見られるように私はここに到達しましたが、動作しません。Xamarinフォームのページ/スクリーンに背景画像を設定する

これは私のMainPage.XAMLコードです。

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:TrackMyMeds" 
      x:Class="TrackMyMeds.MainPage" 
      BackgroundImage="/Images/blue_gradient"> 

    <StackLayout BackgroundColor="Gray" Padding="8,15,8,0"> 

     <Label HorizontalTextAlignment="Center" Text="Welcome to TrackMyMeds" TextColor="White" FontSize="40" FontAttributes="Bold"> 

     </Label> 

     <Label HorizontalTextAlignment="Center" Text="Let us take charge of your health" TextColor="Black" FontSize="18"> 

     </Label> 
     <StackLayout Padding="0,40,0,15"> 
      <Label Text="If you already have an account:" TextColor="Black"> 

      </Label> 

      <Button Clicked="LoginPage_Clicked" Text="Log In Here!" BackgroundColor="#387ef5" TextColor="White" FontSize="18"> 

      </Button> 
     </StackLayout> 
     <StackLayout Padding="0,15,0,0"> 
      <Label Text="If you're new here then:" TextColor="Black"> 

      </Label> 

      <Button Clicked="SignupPage_Clicked" Text="Sign Up Here!" BackgroundColor="#387ef5" TextColor="White" FontSize="18"> 

      </Button> 
     </StackLayout> 

    </StackLayout> 

</ContentPage> 

これは私のMainPage.xaml.csコードです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace TrackMyMeds 
{ 
    public partial class MainPage : ContentPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void LoginPage_Clicked(object sender, EventArgs e) 
     { 
      App.Current.MainPage = new LoginPage(); 
     } 

     private void SignupPage_Clicked(object sender, EventArgs e) 
     { 
      App.Current.MainPage = new SignupPage(); 
     } 
    } 
} 
+0

どこに画像ファイルがありますか? FormsプロジェクトまたはiOSとAndroidプロジェクトの場合 – JimBobBennett

答えて

0

は右、ちょうど縮小画像の大きさ、それを手に入れたし、ドラッグとAndroidでの特定のフォルダにドロップ - >リソース - >描画可能ディレクトリとiOSリソースディレクトリ。

ありがとうございました。

1

次のようMainPage.xamlをファイルにイメージファイル名を追加する必要があります。

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:BgImgTestApp" 
      x:Class="BgImgTestApp.MainPage" 
      BackgroundImage="mobilebg.png"> 
    <Label Text="Welcome to Xamarin Forms!" 
      VerticalOptions="Center" 
      HorizontalOptions="Center" /> 
</ContentPage> 

は、それぞれのフォルダに画像ファイルを追加します。 Android用:リソース - >描画可能フォルダ iOS用:リソースフォルダ Windows用& Windows Phone:Assetsフォルダ。 WindowsデスクトップアプリケーションおよびWindows Phoneアプリケーションの背景を設定するには、MainPage.xamlファイルでこれらの3つの変更を行う必要があります。

1. "フォーム:WindowsPage"を削除し、 "ページ"に切り替えます。 2.Addイメージソース

MainPage.xamlを

<Page 
    x:Class="BgImgTestApp.Windows.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:forms="using:Xamarin.Forms.Platform.WinRT" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:BgImgTestApp.Windows" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Image Source="Assets/mobilebg.png"/> 
    </Grid> 
</Page> 

3.commentこの:

MainPage.xaml.cs

// LoadApplication(新しい.....アプリケーション( ));

ここで働いサンプルアプリ検索:https://github.com/abhiguptame/xamarinsamples/tree/master/BgImgTestApp

注:お使いのデバイスやプラットフォームに応じてリサイズした画像を置いてもよいです。単にその後、

1

あなたがAndroid上で表示している場合は、Androidプロジェクトの描画可能なフォルダにファイルを保存する必要が

<ContentPage BackgroundImage = "image.jpg">... </ContentPage> 
関連する問題