如何在 Xamarin.forms 中复制标签的视图框行为?

我想将我的 WPF 应用程序重写为 Xamarin.forms,但我在将标签缩放到最大可用空间时遇到了困难。


在 WPF 我可以简单地做:


<Viewbox Grid.ColumnSpan="2" Grid.RowSpan="3"

         HorizontalAlignment="Center" VerticalAlignment="Center">

    <Label Width="auto" Height="auto"

           HorizontalAlignment="Center" VerticalAlignment="Center"

           Content="ABCD"/>

</Viewbox>

我试过这样的东西,但它只扩展标签的背景而不是文本


<Label Grid.ColumnSpan="2" Grid.RowSpan="3"

       HorizontalOptions="FillAndExpand"

       VerticalOptions="FillAndExpand"

       Text="ABCD"

/>

xamarin.forms 是否提供了与 WPF 视图框内的标签类似的行为?


繁星点点滴滴
浏览 161回答 2
2回答

饮歌长啸

我想出了这个非常天真的解决方案:XAML:<BoxView x:Name="measureViewBox"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Grid.RowSpan="3" Grid.ColumnSpan="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Opacity="0"/><Label x:Name="myLabel"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Grid.RowSpan="3" Grid.ColumnSpan="2"&nbsp; &nbsp; &nbsp; &nbsp;HorizontalTextAlignment="Center" VerticalTextAlignment="Center"&nbsp; &nbsp; &nbsp; &nbsp;HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"&nbsp; &nbsp; &nbsp; &nbsp;Text="ABCD">后面的代码:private void ContentView_SizeChanged(object sender, EventArgs e){&nbsp; &nbsp; double fontSize = measureViewBox.Width;&nbsp; &nbsp; myLabel.FontSize = fontSize;&nbsp; &nbsp; SizeRequest measurement = myLabel.Measure(double.PositiveInfinity, double.PositiveInfinity);&nbsp; &nbsp; while (measurement.Request.Width >= measureViewBox.Width)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; fontSize -= 2;&nbsp; &nbsp; &nbsp; &nbsp; myLabel.FontSize = fontSize;&nbsp; &nbsp; &nbsp; &nbsp; measurement = myLabel.Measure(double.PositiveInfinity, double.PositiveInfinity);&nbsp; &nbsp; }}当我在 UWP 中调整窗口大小时,它非常滞后,但至少现在,它完成了工作。我将在未来尝试改进这一点,因此任何建议将不胜感激。
打开App,查看更多内容
随时随地看视频慕课网APP