为什么在列表视图之间拖放失败?

如何解决为什么在列表视图之间拖放失败?

我从 Xaml 控件库中复制了代码。当我运行并拖动列表项时,应用程序关闭。如果我在调试中运行,F10 不会引导我到代码中断的部分。事实上,该应用程序保持打开状态,但没有任何反应。我被难住了。

(来自winui3-preview 4)

Contact.txt https://github.com/microsoft/Xaml-Controls-Gallery/blob/master/XamlControlsGallery/Assets/Contacts.txt

// ListBoxPage.xaml (I know bad naming)

<Page
x:Class="NavTest.ListBoxPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NavTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:local1="using:Windows.ApplicationModel.Contacts"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Resources>
    <!-- 
        ListViews with grouped items must be bound to a CollectionViewSource,as shown below.
        This CollectionViewSource is defined in the XAML below,but is bound to an ItemsSource in the C#
        code-behind. See the C# code below for more details on how to create/bind to a grouped list. 
    -->

    <CollectionViewSource x:Name="ContactsCVS" IsSourceGrouped="True"/>

    <!-- 
        In this example,the ListView's ItemTemplate property is bound to a data template (shown below)
        called ContactListViewTemplate,defined in a Page.Resources section. 
    -->

    <DataTemplate x:Key="ContactListViewTemplate" x:DataType="local:Contact">
        <TextBlock Text="{x:Bind Name}" x:Phase="1" Margin="0,5,5"/>
    </DataTemplate>
</Page.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListView
        Grid.Row="0"
        Grid.Column="0"
        x:Name="BaseExample"
        SelectionMode="Extended"
        ItemsSource="{x:Bind ContactsCVS.View,Mode=OneWay}"
        ItemTemplate="{StaticResource ContactListViewTemplate}"
        BorderThickness="1"
        BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
        Width="550"
        Height="400"
        CanDragItems="True"
        CanReorderItems="True"
        AllowDrop="True"
        DragItemsStarting="BaseExample_DragItemsStarting"
        DragOver="BaseExample_DragOver"
        Drop="BaseExample_Drop"
        HorizontalAlignment="Left">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsStackPanel AreStickyGroupHeadersEnabled="True"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate x:DataType="local:GroupInfoList">
                        <Border>
                            <TextBlock Text="{x:Bind Key}" Style="{ThemeResource TitleTextBlockStyle}" />
                        </Border>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
    <ListView
        Grid.Row="0"
        Grid.Column="1"
        x:Name="BaseExample2"
        SelectionMode="Extended"
        ItemTemplate="{StaticResource ContactListViewTemplate}"
        BorderThickness="1"
        BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
        Width="550"
        Height="400"
        CanDragItems="True"
        CanReorderItems="True"
        AllowDrop="True"
        DragItemsStarting="BaseExample2_DragItemsStarting"
        DragOver="BaseExample2_DragOver"
        DragEnter="BaseExample2_DragEnter"
        Drop="BaseExample_Drop"
        HorizontalAlignment="Left">

    </ListView>


</Grid>

// ListBoxPage.xaml.cs

    using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;

using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.WebUI;




// To learn more about WinUI,the WinUI project structure,// and more about our project templates,see: http://aka.ms/winui-project-info.

namespace NavTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ListBoxPage : Page
    {

        ObservableCollection<Contact> contacts1 = new ObservableCollection<Contact>();
        ObservableCollection<Contact> contacts2 = new ObservableCollection<Contact>();

        public ListBoxPage()
        {
            this.InitializeComponent();           
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            // The ItemsSource for the ListView is generated by a method of the Contact class called
            // GetContactsAsync().This method pulls data from an internal data source and creates
            // Contact objects from that data. Those Contact objects are placed in a collection
            // which is returned from the GetContactsAsync() function.

            contacts1 = await Contact.GetContactsAsync();
            contacts2 = new ObservableCollection<Contact>();
            BaseExample.ItemsSource = contacts1;
            BaseExample2.ItemsSource = contacts2;

            ContactsCVS.Source = await Contact.GetContactsGroupedAsync();
        }

        private void BaseExample_DragItemsStarting(object sender,DragItemsStartingEventArgs e)
        {
            // Prepare a string with one dragged item per line
            StringBuilder items = new StringBuilder();
            foreach (Contact item in e.Items)
            {
                if (items.Length > 0) { items.AppendLine(); }
                if (item.ToString() != null)
                {
                    // Append name from contact object onto data string
                    items.Append(item.ToString() + " " + item.Company);
                }
            }
            // Set the content of the DataPackage
            e.Data.SetText(items.ToString());

            e.Data.RequestedOperation = DataPackageOperation.Move;
        }

        private void BaseExample_DragOver(object sender,DragEventArgs e)
        {
            e.AcceptedOperation = DataPackageOperation.Move;
        }

        private async void BaseExample_Drop(object sender,DragEventArgs e)
        {
            ListView target = (ListView)sender;

            if (e.DataView.Contains(StandardDataFormats.Text))
            {
                DragOperationDeferral def = e.GetDeferral();
                string s = await e.DataView.GetTextAsync();
                string[] items = s.Split('\n');
                foreach (string item in items)
                {

                    // Create Contact object from string,add to existing target ListView
                    string[] info = item.Split(" ",3);
                    Contact temp = new Contact(info[0],info[1],info[2]);

                    // Find the insertion index:
                    Windows.Foundation.Point pos = e.GetPosition(target.ItemsPanelRoot);

                    // Find which ListView is the target,find height of first item
                    ListViewItem sampleItem;
                    if (target.Name == "BaseExample")
                    {
                        sampleItem = (ListViewItem)BaseExample2.ContainerFromIndex(0);
                    }
                    // Only other case is target = DragDropListView2
                    else
                    {
                        sampleItem = (ListViewItem)BaseExample.ContainerFromIndex(0);
                    }

                    // Adjust ItemHeight for margins
                    double itemHeight = sampleItem.ActualHeight + sampleItem.Margin.Top + sampleItem.Margin.Bottom;

                    // Find index based on dividing number of items by height of each item
                    int index = Math.Min(target.Items.Count - 1,(int)(pos.Y / itemHeight));

                    // Find the item that we want to drop
                    ListViewItem targetItem = (ListViewItem)target.ContainerFromIndex(index); ;

                    // Figure out if to insert above or below
                    Windows.Foundation.Point positionInItem = e.GetPosition(targetItem);
                    if (positionInItem.Y > itemHeight / 2)
                    {
                        index++;
                    }

                    // Don't go out of bounds
                    index = Math.Min(target.Items.Count,index);

                    // Find correct source list
                    if (target.Name == "BaseExample")
                    {
                        // Find the ItemsSource for the target ListView and insert
                        contacts1.Insert(index,temp);
                        //Go through source list and remove the items that are being moved
                        foreach (Contact contact in BaseExample2.Items)
                        {
                            if (contact.FirstName == temp.FirstName && contact.LastName == temp.LastName && contact.Company == temp.Company)
                            {
                                contacts2.Remove(contact);
                                break;
                            }
                        }
                    }
                    else if (target.Name == "BaseExample2")
                    {
                        contacts2.Insert(index,temp);
                        foreach (Contact contact in BaseExample.Items)
                        {
                            if (contact.FirstName == temp.FirstName && contact.LastName == temp.LastName && contact.Company == temp.Company)
                            {
                                contacts1.Remove(contact);
                                break;
                            }
                        }
                    }
                }

                e.AcceptedOperation = DataPackageOperation.Move;
                def.Complete();
            }
        }

        private void BaseExample_DragEnter(object sender,DragEventArgs e)
        {
            // We don't want to show the Move icon
            e.DragUIOverride.IsGlyphVisible = false;
        }

        private void BaseExample2_DragItemsStarting(object sender,DragItemsStartingEventArgs e)
        {
            if (e.Items.Count == 1)
            {
                // Prepare ListViewItem to be moved
                Contact tmp = (Contact)e.Items[0];

                e.Data.SetText(tmp.FirstName + " " + tmp.LastName + " " + tmp.Company);
                e.Data.RequestedOperation = DataPackageOperation.Move;
            }
        }

        private void BaseExample2_DragOver(object sender,DragEventArgs e)
        {
            e.AcceptedOperation = DataPackageOperation.Move;
        }

        private async void BaseExample2_Drop(object sender,temp);
                        foreach (Contact contact in BaseExample.Items)
                        {
                            if (contact.FirstName == temp.FirstName && contact.LastName == temp.LastName && contact.Company == temp.Company)
                            {
                                contacts1.Remove(contact);
                                break;
                            }
                        }
                    }
                }

                e.AcceptedOperation = DataPackageOperation.Move;
                def.Complete();
            }
        }

        private void BaseExample2_DragEnter(object sender,DragEventArgs e)
        {
            // We don't want to show the Move icon
            e.DragUIOverride.IsGlyphVisible = false;
        }
    }

    // C# code-behind

    // The data template is defined to display a Contact object (class definition shown below),and the text
    // displayed is bound to the Contact object's Name attribute.

    public class Contact
    {
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Company { get; private set; }
        public string Name => FirstName + " " + LastName;

        public static string ContactsPath => $@"{AppDomain.CurrentDomain.BaseDirectory}\Assets\Contacts.txt";

        public Contact(string firstName,string lastName,string company)
        {
            FirstName = firstName;
            LastName = lastName;
            Company = company;
        }

        public override string ToString()
        {
            return Name;
        }

        #region Public Methods
        public static async Task<ObservableCollection<Contact>> GetContactsAsync()
        {
            string contactsPath = $@"{AppDomain.CurrentDomain.BaseDirectory}\Assets\Contacts.txt";
            Uri contactsUri = new Uri(contactsPath,UriKind.RelativeOrAbsolute);
            Uri _contactsUri = new Uri(ContactsPath,UriKind.RelativeOrAbsolute);
            StorageFile file = await StorageFile.GetFileFromPathAsync(contactsPath);
            IList<string> lines = await FileIO.ReadLinesAsync(file);

            ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();

            for (int i = 0; i < lines.Count; i += 3)
            {
                contacts.Add(new Contact(lines[i],lines[i + 1],lines[i + 2]));
            }

            return contacts;
        }

        // To create a collection of grouped items,create a query that groups
        // an existing list,or returns a grouped collection from a database.
        // The following method is used to create the ItemsSource for our CollectionViewSource:

        public static async Task<ObservableCollection<GroupInfoList>> GetContactsGroupedAsync()
        {
            // Grab Contact objects from pre-existing list (list is returned from function GetContactsAsync())
            var query = from item in await GetContactsAsync()

                            // Group the items returned from the query,sort and select the ones you want to keep
                        group item by item.LastName.Substring(0,1).ToUpper() into g
                        orderby g.Key

                        // GroupInfoList is a simple custom class that has an IEnumerable type attribute,and
                        // a key attribute. The IGrouping-typed variable g now holds the Contact objects,// and these objects will be used to create a new GroupInfoList object.
                        select new GroupInfoList(g) { Key = g.Key };

            return new ObservableCollection<GroupInfoList>(query);
        }

        #endregion

    }

    // GroupInfoList class definition:
    public class GroupInfoList : List<object>
    {
        public GroupInfoList(IEnumerable<object> items) : base(items)
        {
        }
        public object Key { get; set; }
    }
}

解决方法

拖放不在预览版 0.5 支持的功能列表中 我认为他们在 0.5 中修复了一些严重的错误。 等 0.8 发布后再试。

请记住,当您现在使用 WinUI3 时,您是最早的鸟之一。你不仅会得到蠕虫,还会得到泥土。

,

我认为问题不在于发布的代码,我认为您更改了 App.xamlApp.xaml.cs 或(不是 xor)删除了默认引用 MainWindow 以便使您的应用程序。如果您的应用没有启动并且没有在您的页面中到达任何断点,那是因为它不在以 App.xaml 开头的“执行链”中。

App.xaml 调用 MainWindow 类(视图):

<Application x:Class="MyProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyProject"
             StartupUri="MainWindow.xaml"> <!-- reference here-->
    <Application.Resources>
    </Application.Resources>
</Application>

然后,从 MainWindow(.xaml 和 .xaml.cs)开始,您应该引用您的页面。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res