.net 如何在Xamarin.Forms的XAML中设置最小宽度或更大的宽度

.net 如何在Xamarin.Forms的XAML中设置最小宽度或更大的宽度,.net,xaml,xamarin.forms,.net,Xaml,Xamarin.forms,我想知道如何将元素的宽度设置为最小X或更大 例如,考虑下面的XAML(只包含用于简单的屏幕截图中的蓝色背景的标记): 我希望蓝色背景的文本元素占据它所拥有的空间,但是如果文本更大,我希望宽度扩大 但现在对于更大的文本,它看起来是这样的: 然而,我想让它看起来像这样,而不是(在第一个屏幕截图中仍然使用最小宽度): 也许你可以考虑用这个来实现: 例如: 对于android: 创建一个最小宽度标签 public class MiniWidthLabel :Label { public

我想知道如何将元素的宽度设置为最小X或更大

例如,考虑下面的XAML(只包含用于简单的屏幕截图中的蓝色背景的标记):


我希望蓝色背景的文本元素占据它所拥有的空间,但是如果文本更大,我希望宽度扩大

但现在对于更大的文本,它看起来是这样的:

然而,我想让它看起来像这样,而不是(在第一个屏幕截图中仍然使用最小宽度):

也许你可以考虑用这个来实现:

例如:

对于android:

创建一个最小宽度标签

public class MiniWidthLabel :Label
{
    public static readonly BindableProperty MinWidthProperty =BindableProperty.Create("MinWidth", typeof(int), typeof(MiniWidthLabel), null);
    public int MinWidth
    {
        get { return (int)GetValue(MinWidthProperty); }
        set { SetValue(MinWidthProperty, value); }
    }
}
在Android项目中创建一个MinWidthLablerEnder

[assembly: ExportRenderer(typeof(MiniWidthLabel), typeof(MinWidthLableRenderer))]
namespace your namepace
{
 class MinWidthLableRenderer:LabelRenderer
  {

    public MinWidthLableRenderer(Context context) : base(context)
    {
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        Control.SetMinWidth(((MiniWidthLabel)Element).MinWidth);

    }
  }
}
[程序集:ExportRenderer(typeof(MiniWidthLabel)、typeof(MinWidthLablerEnder))]
给你的名字空间命名
{
类MinWidthLablerEnder:LabelRenderer
{
公共MinWidthLablerEnder(上下文):基(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
Control.SetMinWidth(((MiniWidthLabel)元素).MinWidth);
}
}
}
然后在page.xaml中使用:

<local:MiniWidthLabel Margin="10,0,10,0" Text="" BackgroundColor="Aqua"  MinWidth="200" FontSize="24" HorizontalOptions="Start"></local:MiniWidthLabel>


?MinimumWidthRequest实际上是一个名称非常糟糕的属性,并不能真正满足我的要求。根据一位Xamarin团队成员的说法:大多数人将其理解为“控件所能达到的最小尺寸,而不是更小的尺寸”,其中属性的实际意图是“当无法达到所需尺寸时控件所能达到的最小尺寸”。来源:使用行定义进行限制,因为您已经在使用网格。是否有任何新的更新?
<local:MiniWidthLabel Margin="10,0,10,0" Text="" BackgroundColor="Aqua"  MinWidth="200" FontSize="24" HorizontalOptions="Start"></local:MiniWidthLabel>