2015-08-06 14 views
6

Używam Xamarin.Forms i kieruję reklamy pod numer iOS i Android.Jak zapobiec przyciśnięciu obrazu tła po otwarciu klawiatury?

Widok domyślny without keyboard

otwarty Klawiatura i obraz tła wyciska keyboard opened

Tło zdjęcie wyciśnięty jak również w trybie poziomym 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> 

Czy jest jakiś sposób, stosując koncepcję w CSS do Xamarin.Forms?

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

Odpowiedz

16

Z ContentPage.BackgroundImage nie można kontrolować proporcje. Zamiast używać Image połączeniu z AbsoluteLayout (i ustawić Aspect własność 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

Lub po prostu użyj 'Grid' zamiast' AbsoluteLayout'. Nie ma potrzeby konfigurowania tych dodatkowych właściwości. – mr5

+0

Świetna odpowiedź! Właściwość BackgroundImage w ContentPage nie obsługuje dodawania parametrów aspektu. Również zmiana właściwości BackgroundImage ContentPage powoduje awarię VS (community Mac) z jakiegoś powodu. – Willempie

2

Wewnątrz główną działalność można wykorzystać AdjustPan lub AdjustResize:

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

Wolę robić kodem, tu jest moje rozwiązanie:

zawsze mam moje urządzenie ekran jak to w moim App.xaml:

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

Wtedy w moim Android Główna działalność:

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

I w moim iOS AppDelegate:

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

Więc kiedy masz ekranie Szerokość i Wysokość można to zrobić w każdej stronie masz:

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; 
    } 
} 
Powiązane problemy