C# 表单设置自定义视图的大小
如何设置自定义视图或更具体地说,自定义BoxView的大小(以xaml为单位)?当前,视图占用整个设备大小,而不是请求的20x20 这是我的xamlC# 表单设置自定义视图的大小,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,如何设置自定义视图或更具体地说,自定义BoxView的大小(以xaml为单位)?当前,视图占用整个设备大小,而不是请求的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
<?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>
和我的自定义渲染器
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
,还必须设置水平选项
和垂直选项
设置
为什么??我不知道,但这是其他地方提出的解决方案,一直对我有效
看起来你想把它放在中间,这样你就可以简单地写了
<controls:bpButton
Height="20"
Width="20"
HeightRequest="20"
WidthRequest="20"
HorizontalOptions="Center"
VerticalOptions="Center" />
是否检查了OnDraw函数中的WidthRequest和HeightRequest的值是否正确?我怀疑您必须强制重新绘制OnElementChanged函数。谢谢!由于某种原因,它有点偏离中心。。。但我希望我能找到答案。。。。
<controls:bpButton
Height="20"
Width="20"
HeightRequest="20"
WidthRequest="20"
HorizontalOptions="Center"
VerticalOptions="Center" />