簡體   English   中英

Xamarin.Forms 設置自定義視圖的大小

[英]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);
        }        
    }
}

除了設置WidthRequestHeightRequest ,您還必須設置HorizontalOptionsVerticalOptions設置。

為什么? 我不知道,但這是其他地方建議的解決方案,並且一直對我有用。

看起來你想要它在中心,所以你可以簡單地寫

<controls:bpButton 
    Height="20"
    Width="20" 
    HeightRequest="20"
    WidthRequest="20"
    HorizontalOptions="Center"
    VerticalOptions="Center" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM