拖放 .Net Maui

jae*_*lio 3 maui .net-maui

有没有办法在新的 .net Maui 中为移动应用程序(Android、Ios 和 WinApp)创建可拖动项目的布局?

我一直在寻找该平台可以做什么,但目前还没有找到任何东西。

Bas*_*s H 5

是的,使用CanDrag 和AllowDrop。

在这里找到它DragdropMaui 示例

在 MainPage.xaml 中

    <Frame Margin="20">
        <Frame.GestureRecognizers>
            <DropGestureRecognizer AllowDrop="True" Drop="DropGestureRecognizer_Drop_1" />
        </Frame.GestureRecognizers>
    </Frame>

            <Label Text="Drag Me away" HorizontalTextAlignment="Center" TextColor="Black" FontSize="36">
                <Label.GestureRecognizers>
                    <DragGestureRecognizer CanDrag="True" DragStarting="DragGestureRecognizer_DragStarting_1" />
                </Label.GestureRecognizers>
            </Label>
Run Code Online (Sandbox Code Playgroud)

在 MainPage.cs 中

 private void DragGestureRecognizer_DragStarting_1(object sender, DragStartingEventArgs e)
{
    var label = (sender as Element)?.Parent as Label;
    e.Data.Properties.Add("Text", label.Text);
}

private void DropGestureRecognizer_Drop_1(object sender, DropEventArgs e)
{
    var data = e.Data.Properties["Text"].ToString();
    var frame = (sender as Element)?.Parent as Frame;
    frame.Content = new Label
    {
        Text = data
    };
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这只能用于将元素从应用程序内拖动到其他区域吗?我正在开发一个 MAUI Windows 应用程序,并将一个文件从桌面拖到我的应用程序中的一个元素中,该元素的 AllowDrop 属性设置为 true 会导致未处理的异常,并显示消息“值不能为空”。 (2认同)