2015-08-06 4 views
6

Ich verwende Xamarin.Forms und zielt auf iOS und Android.Wie kann verhindert werden, dass das Hintergrundbild beim Öffnen der Tastatur gedrückt wird?

Standardansicht without keyboard

Tastatur geöffnet und der Bildhintergrund gepresst keyboard opened

Hintergrundbild als auch im Querformat gepresste Landscape

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" 
      x:Class="XilnexOTM.Views.LoginPage" 
      BackgroundImage="bg1.png" > 
    <ScrollView> 
     <StackLayout Orientation="Vertical"> 
      <Label Text="PICTURE" /> 
      <Label Text="PIC 2" /> 

      <Entry Placeholder="Username" /> 
      <Entry Placeholder="Password" IsPassword="true"/> 

      <Button x:Name="btn_Login" 
        Text="Login" 
        BackgroundColor="#FF0000"/> 

     </StackLayout> 
    </ScrollView> 
</ContentPage> 

Gibt es einen möglichen Weg, indem man das Konzept in CSS zu Xamarin.Forms anwendet?

xx { 
background-size: cover; 
background-position: right bottom; 
} 

Antwort

16

Mit ContentPage.BackgroundImage Sie nicht Seitenverhältnis steuern kann. Statt Image mit AbsoluteLayout kombiniert verwenden (und setzen Aspect Eigenschaft Image):

<?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="XilnexOTM.Views.LoginPage"> 

    <AbsoluteLayout VerticalOptions="FillAndExpand" 
        HorizontalOptions="FillAndExpand"> 

     <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" 
      Source="bg1.png" Aspect="AspectFill"/> 

     <ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"> 
      <StackLayout Orientation="Vertical"> 
       <Label Text="PICTURE" /> 
       <Label Text="PIC 2" /> 

       <Entry Placeholder="Username" /> 
       <Entry Placeholder="Password" IsPassword="true"/> 

       <Button x:Name="btn_Login" 
         Text="Login" 
         BackgroundColor="#FF0000"/> 
      </StackLayout> 
     </ScrollView>     

    </AbsoluteLayout>    
</ContentPage> 
+0

Oder verwenden Sie einfach 'Grid' anstelle von' AbsoluteLayout'. Keine Notwendigkeit, diese zusätzlichen Eigenschaften einzurichten – mr5

+0

Große Antwort! Die BackgroundImage-Eigenschaft von ContentPage unterstützt das Hinzufügen von Aspektparametern nicht. Das Ändern der BackgroundImage-Eigenschaft von ContentPage stürzt VS (Community Mac) aus irgendeinem Grund ab. – Willempie

2

In Ihrem MainActivity Sie AdjustPan oder AdjustResize verwenden können:

[Activity(WindowSoftInputMode = SoftInput.AdjustPan)] 
public class MyActivity : Activity 
{ 
    ..... 
} 
0

ich von Code lieber tun, hier ist meine Lösung:

ich habe immer meinen Gerätebildschirm wie dies in meinem App.xaml:

public static double ScreenWidth { get; set; } 
    public static double ScreenHeight { get; set; } 

dann in meiner Android-Hauptaktivität:

 Passapp.App.ScreenWidth = (double)(Resources.DisplayMetrics.WidthPixels/Resources.DisplayMetrics.Density); 
     Passapp.App.ScreenHeight = (double)(Resources.DisplayMetrics.HeightPixels/Resources.DisplayMetrics.Density); 

Und in meinem iOS AppDelegate: So

App.ScreenWidth = UIScreen.MainScreen.Bounds.Width; 
     App.ScreenHeight = UIScreen.MainScreen.Bounds.Height; 

wenn Sie haben Ihren Bildschirm Breite und Höhe, dies zu tun in welcher Seite Sie haben:

public partial class MyPage : ContentPage 
{ 

    public MyPage() 
    { 
     InitializeComponent(); 

     NavigationPage.SetHasNavigationBar(this, false); 

     var layout = new AbsoluteLayout(); 

#if __ANDROID__ 

     BackgroundImage = "Background"; 
#endif 

#if __IOS__ 
     Image img = new Image() 
     { 
      Source = "Background", 
      Aspect = Aspect.Fill 
     }; 

     layout.Children.Add(img, new Rectangle(0, 0, App.ScreenWidth, App.ScreenHeight)); 
#endif 


     Content = layout; 
    } 
} 
Verwandte Themen