[英]setting a background to a view using relative layout programmatically xamarin forms
[英]Xamarin forms layout and size not setting
我是 Xamarin 的初學者,我正在嘗試學習制作簡單的圖形元素,例如按鈕和圖像。 我的問題是,當我嘗試更改元素(在我的示例中為按鈕)的尺寸或 position 時,似乎沒有任何變化。
我已經嘗試過類似startButton.WidthRequest = 300;
或使用絕對布局AbsoluteLayout.SetLayoutBounds(startButton, new Rectangle(0.5, 0.2, 200, 100));
但沒有任何效果,在我的模擬器應用程序中,所有內容似乎都設置在屏幕底部,默認高度和屏幕寬度。
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#
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());
}
}
}
我讓它打印按鈕寬度以進行調試,它說它= -1,不知道為什么以及它是什么意思。
現在我添加了AbsoluteLayout.SetLayoutFlags(startButton, AbsoluteLayoutFlags.All);
它似乎有效,但我不確定該命令究竟做了什么以及它為什么有效
我將圖像放入Stack Layout
的方式是:
<StackLayout Orientation="Vertical"
BackgroundColor="#444444"
Padding="30"
Spacing="40">
<Image HorizontalOptions="Fill"
HeightRequest="150"
Source="YourImage.png"/>
</StackLayout>
只要確保您的圖像路徑正常工作,您應該對 go 很好。
我還從 CodeWithMosh Xamarin 的課程中學到了很多東西(即使它有點過時了): https://codewithmosh.com/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.