[英]Xamarin.Forms set size of custom view
如何在 xaml 中設置自定義視圖或更具體地說是自定義 BoxView 的大小? 目前,視圖占據了整個設備大小,而不是請求的 20x20。
這是我的 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"
xmlns:local="clr-namespace:Beltpack"
xmlns:controls="clr-namespace:Beltpack.Views;assembly=Beltpack"
x:Class="Beltpack.MainPage">
<controls:bpButton Height="20" Width="20" HeightRequest="20" WidthRequest="20" />
</ContentPage>
我從 BoxView 派生的按鈕(目前沒有做任何事情)
using Xamarin.Forms;
namespace Beltpack.Views {
public class bpButton : BoxView {
public bpButton() { }
}
}
還有我的自定義渲染器
using Android.Graphics;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using Beltpack.Views;
using Beltpack.Droid;
[assembly: ExportRenderer(typeof(bpButton), typeof(bpButtonRenderer))]
namespace Beltpack.Droid {
public class bpButtonRenderer : BoxRenderer {
public bpButtonRenderer() {}
protected override void OnDraw(Canvas canvas) {
bpButton btn = (bpButton)this.Element;
Paint paint = new Paint();
paint.Color = Android.Graphics.Color.Aqua;
canvas.DrawCircle((int)(btn.WidthRequest * .5), (int)(btn.HeightRequest * .5), (int)(btn.WidthRequest * .5), paint);
}
}
}
除了設置WidthRequest
和HeightRequest
,您還必須設置HorizontalOptions
和VerticalOptions
設置。
為什么? 我不知道,但這是其他地方建議的解決方案,並且一直對我有用。
看起來你想要它在中心,所以你可以簡單地寫
<controls:bpButton
Height="20"
Width="20"
HeightRequest="20"
WidthRequest="20"
HorizontalOptions="Center"
VerticalOptions="Center" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.