我有一个 Xamarin.CommunityToolkit - Popup。我不想预先定义它的大小。我希望它根据内容动态调整大小。有什么办法可以达到这个目的吗?
这是我的XAML代码:
<?xml version="1.0" encoding="UTF-8"?>
<xct:Popup xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="SampleProject.Views.SamplePopup">
<StackLayout>
<Label Text="Hello Xamarin.Forms!" />
<Button Text="Close Popup" Clicked="ClosePopup" />
</StackLayout>
</xct:Popup>
由于社区工具包弹出窗口动态自动高度调整问题存在错误报告,您可以考虑通过根据其内容动态设置弹出窗口高度来解决问题。
<xct:Popup
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="SampleXamarinForms.SamplePopup"
SizeChanged="Popup_SizeChanged"
x:Name="myPopup">
<StackLayout
x:Name="myStacklayout">
<Label Text="Hello Xamarin.Forms!" />
<Button Text="Close Popup" Clicked="ClosePopup" />
</StackLayout>
</xct:Popup>
public partial class SamplePopup : Popup
{
public SamplePopup ()
{
InitializeComponent ();
}
void Popup_SizeChanged(object sender, EventArgs e)
{
double width = GetDeviceWidth() - 40;
double height = myStacklayout.Height;
myPopup.Size = new Size(width, height);
}
private double GetDeviceWidth()
{
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var width = mainDisplayInfo.Width;
return width;
}
}