如何设置 Xamarin 社区工具包弹出窗口的大小以在 Xamarin Forms 中动态包装内容?

问题描述 投票:0回答:2

我有一个 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>
c# xaml xamarin xamarin.forms xamarin-community-toolkit
2个回答
2
投票

当前的实现不支持此类功能,正如 Amjad 在他的评论中提到的那样,过去有一个类似的功能请求,但随着事情迁移到新的 repo .NET MAUI 社区工具包,它被关闭了。

如果您有兴趣,可以打开讨论,解释或指向该功能请求的链接。


0
投票

由于社区工具包弹出窗口动态自动高度调整问题存在错误报告,您可以考虑通过根据其内容动态设置弹出窗口高度来解决问题。

<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;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.