Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单设置自定义视图的大小_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 表单设置自定义视图的大小

C# 表单设置自定义视图的大小,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

如何设置自定义视图或更具体地说,自定义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"
             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" />