【问题标题】:insert background image in a grid, that fits the size with Xamarin forms pcl在网格中插入背景图像,该图像适合 Xamarin forms pcl 的大小
【发布时间】:2016-12-20 11:11:10
【问题描述】:

我需要根据设备大小在单独适合的网格中插入背景。 我尝试了上千种组合,但仍然很小,或者不适合所有组合。 在下图中可以看到红色圈出的图片是我要适应的Grid,Grid位于屏幕底部的后半部分。

我正在尝试把图片放到网格中,图片有x:Name="backgroundImage",我要适应下半屏整个网格的对象是什么,我该怎么做做?这是代码:

 <?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="Fimap.Login"
             BackgroundColor="#333">
  <ContentPage.Padding>
  <OnPlatform x:TypeArguments="Thickness"
                iOS="0,0,0,0"
                Android="0,0,0,0"
                WinPhone="0,0,0,0" />
  </ContentPage.Padding>
  <ContentPage.Content>
    <ScrollView>
      <StackLayout VerticalOptions="Center" HorizontalOptions="CenterAndExpand" Orientation="Vertical" Spacing="0">
        <ActivityIndicator x:Name="loadingBeforeLogin" IsVisible="true" Color="#008ECC" IsRunning="true" />
      </StackLayout>
      <RelativeLayout VerticalOptions="Center" HorizontalOptions="CenterAndExpand" x:Name="allContent">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="5*"></RowDefinition>
            <RowDefinition Height="5*"></RowDefinition>
          </Grid.RowDefinitions>
          <!-- immagini loghi e scritta login sopra-->
          <Grid Grid.Row="0" BackgroundColor="#1f2429">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="2*"></ColumnDefinition>
              <ColumnDefinition Width="6*"></ColumnDefinition>
              <ColumnDefinition Width="2*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions Height="*">
              <RowDefinition Height="2*"></RowDefinition>
              <RowDefinition Height="6*"></RowDefinition>
            </Grid.RowDefinitions>
            <Image Aspect="AspectFit" x:Name="logoScritta" Grid.Row="0" Grid.Column="1" Source="Images/fimaLogoLogin.png"></Image>
            <Image Aspect="AspectFit" x:Name="logo" Grid.Row="1" Grid.Column="1" Source="Images/logo.png"></Image>
            <Label TextColor="#fff" Grid.Row="2" FontSize="22" Grid.Column="1" Text="Login" />
          </Grid>
          <!-- username e password input e ricorda password -->            
          <Grid Grid.Row="1">
            <Image x:Name="backgroundImage"/>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="2*"></ColumnDefinition>
              <ColumnDefinition Width="6*"></ColumnDefinition>
              <ColumnDefinition Width="2*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="1*"></RowDefinition>
              <RowDefinition Height="2*"></RowDefinition>
              <RowDefinition Height="2*"></RowDefinition>
              <RowDefinition Height="2*"></RowDefinition>
              <RowDefinition Height="2*"></RowDefinition>
              <RowDefinition Height="2*"></RowDefinition>
            </Grid.RowDefinitions>
            <ActivityIndicator x:Name="loading" Grid.Row="0" Grid.Column="1" IsVisible="false" Color="#008ECC" IsRunning="true" />
            <Label TextColor="#2196F3" Grid.Row="1" Grid.Column="1" Text="" />
            <Entry TextColor="#2196F3" PlaceholderColor="#A9D6FA" FontSize="24" Grid.Row="1" Grid.Column="1" Placeholder="Username" x:Name="UsernameEntry" Text="" />
            <Label TextColor="#2196F3" Grid.Row="2" Grid.Column="1" Text="" />
            <Entry TextColor="#2196F3" PlaceholderColor="#A9D6FA" FontSize="24" Grid.Row="2" Grid.Column="1" Placeholder="Password" IsPassword="True" x:Name="PasswordEntry" Text="" />
            <Button x:Name="LoginButton" FontSize="22" Grid.Row="3" Grid.Column="1" Text="Accedi" Clicked="Login_OnClicked"/>
            <Grid Grid.Row="4" Grid.Column="1">
              <Grid.RowDefinitions>
                <RowDefinition Height="1*"></RowDefinition>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="7*"></ColumnDefinition>
                <ColumnDefinition Width="3*"></ColumnDefinition>
              </Grid.ColumnDefinitions>
              <Label TextColor="#2196F3" Text="Ricorda accesso" Grid.Row="0" Grid.Column="0"></Label>
              <Switch x:Name="switchRememberPassword" Grid.Row="0" Grid.Column="1"></Switch>
            </Grid>
            <Label x:Name="recuperaPassword" Grid.Row="5" Grid.Column="1" TextColor="#2196F3" Text="Hai dimenticato la password?" FontSize="12"></Label>
          </Grid>
        </Grid>
      </RelativeLayout>
    </ScrollView>
  </ContentPage.Content>
</ContentPage>

解决方案 将代码隐藏 c# 中的图像添加到名为 relative 的 RelativeLayout 中

Image backgroundImage = new Image();    
backgroundImage.Source = "loginBackground.png";
                backgroundImage.Aspect = Aspect.AspectFill;            

                relative.Children.Add(backgroundImage,
                    Constraint.RelativeToParent((parent) =>
                    {
                        backgroundImage.WidthRequest = parent.Width;
                        return 0;
                    }),
                    Constraint.RelativeToParent((parent) =>
                    {
                        return parent.Height - backgroundImage.Height;
                    })
                );

【问题讨论】:

  • 请编辑您的问题以提供更多信息,而不是一次又一次地创建关于相同主题的相同问题。
  • 是几个问题,有相同的例子,但有不同的要求,请在编写法律之前仔细阅读。我正在学习,我需要几个例子。
  • 您是否要让图像填充网格的底部作为背景?

标签: c# xaml xamarin xamarin.forms portable-class-library


【解决方案1】:

在调用App构造函数之前的iOS项目中:

App.ScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);

在 PCL 的 App 类中:

public static Size ScreenSize;

简单地说,在给出网格列和行的大小时,您应该使用如下方式:

App.ScreenSize.Width * 1, App.ScreenSize.Height * 1

【讨论】:

  • 此解决方案不适用于相对布局,而是用于全屏
猜你喜欢
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2013-11-24
  • 2015-07-30
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多