简体   繁体   English

Xamarin forms 布局和尺寸未设置

[英]Xamarin forms layout and size not setting

I am a beginner in Xamarin and I am trying to learn to make simple graphics elements such as buttons and images.我是 Xamarin 的初学者,我正在尝试学习制作简单的图形元素,例如按钮和图像。 My problem is that when I try to change the dimension or the position of an element (a button in my example) nothing really seems to change.我的问题是,当我尝试更改元素(在我的示例中为按钮)的尺寸或 position 时,似乎没有任何变化。

I already tried stuff like startButton.WidthRequest = 300;我已经尝试过类似startButton.WidthRequest = 300; or using absolute layout AbsoluteLayout.SetLayoutBounds(startButton, new Rectangle(0.5, 0.2, 200, 100));或使用绝对布局AbsoluteLayout.SetLayoutBounds(startButton, new Rectangle(0.5, 0.2, 200, 100)); but nothing worked and in my application in the emulator everything seems to be set at the bottom of the screen with a default height and a width of the screen.但没有任何效果,在我的模拟器应用程序中,所有内容似乎都设置在屏幕底部,默认高度和屏幕宽度。

XAML (tried <AbsoluteLayout> too but got the same problem, everything is set on the top-left of the screen and setLayoutBounds or WidthRequest doesn't work) XAML (也尝试了<AbsoluteLayout>但遇到了同样的问题,一切都设置在屏幕的左上角并且 setLayoutBounds 或 WidthRequest 不起作用)

<?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:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         xmlns:local="clr-namespace:TestImages;assembly=TestImages"
         x:Class="TestImages.MainPage">
<StackLayout x:Name="container1">
    <!-- Place new controls here -->
    <Label Text="Welcome to MyApp" HorizontalOptions="Center" VerticalOptions="StartAndExpand" />

    <Image Source="{local:ImageResource TestImages.kids2.png}"/>

    <Button x:Name="startButton"
            Text="START"
            Clicked="goToList" />

</StackLayout>
</Contenpage>

C# C#

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

namespace TestImages
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

        var screenWidth = mainDisplayInfo.Width;
        Console.WriteLine($"Width: {screenWidth}");

        startButton.BackgroundColor = Color.LightGreen;
        startButton.BorderColor = Color.White;
        startButton.CornerRadius = 8;
        startButton.BorderWidth = 1.5;
        startButton.WidthRequest = 300;
        AbsoluteLayout.SetLayoutBounds(startButton, new Rectangle(0.5, 0.2, 200, 100));

        var bottone2 = new Button { Text = "Start222" };
        container1.Children.Add(bottone2);
        AbsoluteLayout.SetLayoutBounds(bottone2, new Rectangle(0.5, 0.2, 200, 100));
        bottone2.BackgroundColor = Color.Beige;


        Console.WriteLine($"Button: {startButton.Width}");
    }

    async void goToList(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.Write("Cliccato START");
        Console.Write("Cliccato START");
        await Navigation.PushAsync(new ListPage());

    }
  }
}

I made it print the button width for debug and it says it is = -1, don't know why and what does it mean.我让它打印按钮宽度以进行调试,它说它= -1,不知道为什么以及它是什么意思。

在此处输入图像描述

Now I added AbsoluteLayout.SetLayoutFlags(startButton, AbsoluteLayoutFlags.All);现在我添加了AbsoluteLayout.SetLayoutFlags(startButton, AbsoluteLayoutFlags.All); and it seems to work, but I am not sure what exactly does that command do and why it works它似乎有效,但我不确定该命令究竟做了什么以及它为什么有效

The way i've putted an Image in a Stack Layout is:我将图像放入Stack Layout的方式是:

<StackLayout Orientation="Vertical" 
             BackgroundColor="#444444" 
             Padding="30" 
             Spacing="40">

    <Image HorizontalOptions="Fill" 
           HeightRequest="150" 
           Source="YourImage.png"/>
</StackLayout>

Just make sure that the path to your image is correctly working and you should be good to go.只要确保您的图像路径正常工作,您应该对 go 很好。

I also learned a lot from CodeWithMosh Xamarin's course (even tho it's a bit obsolete): https://codewithmosh.com/我还从 CodeWithMosh Xamarin 的课程中学到了很多东西(即使它有点过时了): https://codewithmosh.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM