【问题标题】:Display of screen is pushed down xamarin.forms android屏幕显示被下推 xamarin.forms android
【发布时间】:2017-12-08 11:02:14
【问题描述】:

奇怪的是,我的屏幕显示被向下推,只有 MainPage 此后显示适合屏幕。我已经尝试删除该页面并重新添加它,但这也不起作用。有谁知道这可能是什么原因以及如何解决它?

如下所示:顶部的黑色空间不应该存在

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="LoyaltyWorx.Views.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:LoyaltyWorx.Views.Converters"
    xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
    prism:ViewModelLocator.AutowireViewModel="True"
    >
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:InvertBooleanConverter x:Key="InvertBooleanConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <Grid>
        <Image Source="bk3.jpg" Aspect="AspectFill"/>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Horizontal" VerticalOptions="Start">
            <!-- top controls -->
        </StackLayout>
        <StackLayout VerticalOptions="CenterAndExpand">
            <!-- middle controls -->
            <BoxView HeightRequest="430"></BoxView>
            <Button Text="Continue with Facebook" x:Name="LoginBtn" BackgroundColor="#4867aa" TextColor="White" FontFamily="Klavika" HorizontalOptions="CenterAndExpand" Clicked="LoginBtn_Clicked" />
            <Button Text="Continue with Google  " Command="{Binding GoogleLoginCommand}" BackgroundColor="#d34836" TextColor="White" FontFamily="Klavika" HorizontalOptions="CenterAndExpand"/>
        </StackLayout>
        <StackLayout Orientation="Horizontal" VerticalOptions="End" HorizontalOptions="Center">
            <!-- bottom controls -->
            <Frame OutlineColor="White" HorizontalOptions="StartAndExpand">
                <StackLayout Orientation="Horizontal" VerticalOptions="End" HorizontalOptions="Center">
                    <!-- bottom controls -->
                    <StackLayout Grid.Row="1" Orientation="Horizontal" Spacing="0">
                        <Label  
                               Text="Terms and conditions"
                              FontSize="13"
                            TextColor="#71757a"
                            FontAttributes="Bold"
                            x:Name="LblTerms"/>
                        <Label  
                            Text=" and"
                            FontSize="13"
                            TextColor="#71757a"
                            />

                        <Label  
                               Text=" privacy policy"
                            FontSize="13"
                            TextColor="#71757a"
                            FontAttributes="Bold"
                            x:Name="LblPrivacy"/>
                    </StackLayout>

                </StackLayout>
            </Frame>
        </StackLayout>
    </StackLayout>
    </Grid>
</ContentPage>

App.cs

public partial class App : PrismApplication
    {

        public App(IPlatformInitializer initializer = null) : base(initializer) { }


        protected override void OnInitialized()
        { 
            InitializeComponent();
            SetMainPage();
            NavigationService.NavigateAsync("NavigationPage/MainPage?title=Hello%20from%20Xamarin.Forms");
        }

        protected override void RegisterTypes()
        {
            Container.RegisterTypeForNavigation<NavigationPage>();
            Container.RegisterTypeForNavigation<MainPage>();
        }

        private void SetMainPage()
        {
            if (!string.IsNullOrEmpty(Helpers.Settings.Usertoken))
            {
                Application.Current.MainPage = (new Profile());
            }
            else
            {
                MainPage = new NavigationPage(new MainPage())
                { BarBackgroundColor = Color.FromHex("#393b3d") };

            }
        }

        public async static Task NavigateToSMS()
        {
            await Application.Current.MainPage.Navigation.PushAsync(new SMS());
        }

        public static void NavigateToProfile()
        {
            Application.Current.MainPage = (new InvalidLogin());
        }
        public static void NavigateToVerified()
        {
            Application.Current.MainPage = (new Profile());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

【问题讨论】:

  • 首先,为什么要在网格内使用stacklayout,而不是使用网格行。您想在其上覆盖图像,但为了更好地在网格周围使用 absolutelayout 并将图像设置在 absolutelayout 的中间。这是性能影响。关于您的问题,请在您的 Grid 上尝试 VerticalOptions="StartAndExpand"。
  • 不要认为它与我在 xaml 上的布局有任何关系,我只是将其删除并添加了一个新的 xaml 页面,而我这边没有代码,它是一样的。我假设我的 app.cs 中的某些东西导致了问题
  • 如果您最近升级了 xamarin 表单包,其中一个或最新的表单版本存在类似问题,这可能会导致问题。
  • 尝试将 Image Aspect 属性更改为 Fill 而不是 AspectFill。我认为这会在渲染时垂直增加图像视图。如果您在 Android 设备的开发者设置中启用Show layout bounds,您可以看到它。

标签: xaml xamarin xamarin.forms


【解决方案1】:

您将MainPage 包裹在NavigationPage 中,默认情况下会显示导航栏。你可以在你的cs代码或者MainPage.xaml中关闭它

cs

var mainPage = new MainPage();
MainPage = new NavigationPage(mainPage);
NavigationPage.SetHasNavigationBar(mainPage, false);

xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="LoyaltyWorx.Views.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:LoyaltyWorx.Views.Converters"
    xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
    prism:ViewModelLocator.AutowireViewModel="True"
    NavigationPage.HasNavigationBar="False">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多