【问题标题】:Synchronous command doing the opposite of what identical asynchronous method does同步命令的作用与相同的异步方法相反
【发布时间】:2018-05-30 15:58:19
【问题描述】:

我使用 Xamarin.Forms 和 sqlite-net-pcl Nuget 包构建了一个非常简单的列表制作应用程序。我有一个模型ListItem,带有一个布尔属性Active。有两个列表:activeItems,其中Active = true,和inactiveItems,其中Active = false。当用户点击activeItems列表中的一个项目时,它被设置为Active = false,两个列表被重新加载。当用户点击inactiveItems列表中的一个项目时,它被设置为Active = true,并且这两个列表被重新加载。这一切都很好。

但是,我还想提供永久删除项目的选项,而不仅仅是将其标记为完成。当用户长期持有项目的MenuItem(在任一列表上)并选择“删除”时,ListItem 会按预期从数据库中删除,但不会从activeItems 列表中删除,inactiveItems列表完全消失(当我重新加载页面时,inactiveItems 列表又回来了,activeItems 列表缺少已删除的项目,正如预期的那样)。这真的很奇怪,因为我将“SetActive/Inactive”方法复制并粘贴到“DeleteListItem”方法中。唯一的区别是“DeleteListItem”是同步的,但我不明白为什么它应该做它正在做的事情。

旁注:对于每种方法,我都会重新加载两个列表的 HeightRequest,因此它们完全适合它们的内容。

有人看到问题了吗?或者,如果有人知道如何使我的“DeleteListItem”方法异步,那也太好了,因为这可能是问题所在。

更新

我尝试让我的“DeleteListItem”异步,但行为没有改变。我还尝试使用App.ListItemRepo.GetActiveListItemsAsync()); 而不是App.ListItemRepo.GetActiveListItems());,因为这是有效方法中使用的方法。但后来我得到这个错误:

Error   CS1503
Argument 1: cannot convert from 'System.Threading.Tasks.Task<System.Collections.Generic.List<Myapp.Models.ListItem>>' to 'System.Collections.Generic.List<Myapp.Models.ListItem>'

Xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="..."
             xmlns:x="..."
             x:Class="Myapp.ListPage"
             x:Name="ListPage">
    <ScrollView Orientation="Vertical">
    <StackLayout>

            <!-- CURRENT ITEMS LIST -->
        <StackLayout VerticalOptions="Fill" Orientation="Vertical" HorizontalOptions="StartAndExpand">
            <ListView x:Name="activeList" VerticalOptions="Start" ItemTapped="SetInactive">
                <ListView.ItemTemplate>
                    <DataTemplate>
                            <TextCell Text="{Binding Name}">
                                <TextCell.ContextActions>
                                    <MenuItem Command="{Binding Source={x:Reference ListPage}, Path=DeleteListItem}"
                                    CommandParameter="{Binding .}" Text="delete" />
                                </TextCell.ContextActions>
                            </TextCell>
                        </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>

        <!-- PAST ITEMS LIST -->
        <ListView x:Name="inactiveList" VerticalOptions="Start" ItemTapped="SetActive" >
            <ListView.ItemTemplate>
                <DataTemplate>
                <TextCell Text="{Binding Name}">
                    <TextCell.ContextActions>
                        <MenuItem Command="{Binding Source={x:Reference ListPage}, Path=DeleteListItem}"
                            CommandParameter="{Binding .}" Text="delete" />
                    </TextCell.ContextActions>
                </TextCell>
              </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    </ScrollView>
</ContentPage>

代码隐藏:(使用 ListItemRepository.cs 中定义的方法,定义为“App.ListItemRepo”)

using Myapp.Models;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using SQLite;
using System.Threading.Tasks;
using System.IO;
using Xamarin.Forms;
using System.Diagnostics;

namespace Myapp
{
    public partial class ListPage
    {
        public Command DeleteListItem { get; set; }

        public ListPage()
        {
            InitializeComponent();

            ObservableCollection<ListItem> activeItems =
                new ObservableCollection<ListItem>(
                    App.ListItemRepo.GetActiveListItems());
            activeList.ItemsSource = activeItems;
            activeList.HeightRequest = 50 * activeItems.Count;

            ObservableCollection<ListItem> inactiveItems =
                new ObservableCollection<ListItem>(
                    App.ListItemRepo.GetInactiveListItems());
            inactiveList.ItemsSource = inactiveItems;
            inactiveList.HeightRequest = 50 * inactiveItems.Count;

            });

            // DELETE METHOD
            DeleteListItem = new Command((parameter) => {
                ListItem item = (ListItem)parameter as ListItem;

                App.ListItemRepo.DeleteListItemAsync(item);

                ObservableCollection<ListItem> commandActiveItems =
                    new ObservableCollection<ListItem>(
                        App.ListItemRepo.GetActiveListItems());
                activeList.ItemsSource = commandActiveItems;
                activeList.HeightRequest = 50 * commandActiveItems.Count;

                ObservableCollection<ListItem> commandInactiveItems =
                    new ObservableCollection<ListItem>(
                        App.ListItemRepo.GetInactiveListItems());
                inactiveList.ItemsSource = commandInactiveItems;
                inactiveList.HeightRequest = 50 * commandInactiveItems.Count;
            });
        }

        // SET AS ACTIVE METHOD
        public async void SetActive(object sender, ItemTappedEventArgs e)
        {
            var selectedListItem = e.Item as ListItem;
            await App.ListItemRepo.SetListItemActive(selectedListItem);

            ObservableCollection<ListItem> activeItems =
                new ObservableCollection<ListItem>(
                    await App.ListItemRepo.GetActiveListItemsAsync());
            activeList.ItemsSource = activeItems;
            activeList.HeightRequest = 50 * activeItems.Count;

            ObservableCollection<ListItem> inactiveItems =
                new ObservableCollection<ListItem>(
                    await App.ListItemRepo.GetInactiveListItemsAsync());
            inactiveList.ItemsSource = inactiveItems;
            inactiveList.HeightRequest = 50 * inactiveItems.Count;
        }

        // SET AS INACTIVE METHOD
        public async void SetInactive(object sender, ItemTappedEventArgs e)
        {
            var selectedListItem = e.Item as ListItem;
            await App.ListItemRepo.SetListItemInactive(selectedListItem);

            ObservableCollection<ListItem> activeItems =
                new ObservableCollection<ListItem>(
                    await App.ListItemRepo.GetActiveListItemsAsync());
            activeList.ItemsSource = activeItems;
            activeList.HeightRequest = 50 * activeItems.Count;

            ObservableCollection<ListItem> inactiveItems =
                new ObservableCollection<ListItem>(
                    await App.ListItemRepo.GetInactiveListItemsAsync());
            inactiveList.ItemsSource = inactiveItems;
            inactiveList.HeightRequest = 50 * inactiveItems.Count;
        }
    }
}

资产/ListItemRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Myapp.Models;
using SQLite;
using System.Threading.Tasks;

namespace Myapp
{
    public class ListItemRepository
    {
        private readonly SQLiteAsyncConnection conn;
        private readonly SQLiteConnection syncconn;

        public ListItemRepository(string dbPath)
        {
            conn = new SQLiteAsyncConnection(dbPath);
            conn.CreateTableAsync<ListItem>().Wait();
            syncconn = new SQLiteConnection(dbPath);
            syncconn.CreateTable<ListItem>();
        }


        public async Task SetListItemActive(ListItem listItem)
        {
            ListItem item = (ListItem)listItem as ListItem;
            var result = await conn.UpdateAsync(new ListItem { Id = item.Id, Active = true, Name = item.Name }).ConfigureAwait(continueOnCapturedContext: false);
        }
        public async Task SetListItemInactive(ListItem listItem)
        {
            ListItem item = (ListItem)listItem as ListItem;
            var result = await conn.UpdateAsync(new ListItem { Id = item.Id, Active = false, Name = item.Name }).ConfigureAwait(continueOnCapturedContext: false);
        }
        public async Task DeleteListItemAsync(ListItem listItem)
        {
            ListItem item = (ListItem)listItem as ListItem;
            var result = await conn.DeleteAsync(new ListItem { Id = item.Id }).ConfigureAwait(continueOnCapturedContext: false);
        }

        public Task<List<ListItem>> GetActiveListItemsAsync()
        {
            return conn.QueryAsync<ListItem>("select * from list_items where Active = 1");
        }
        public Task<List<ListItem>> GetInactiveListItemsAsync()
        {
            return conn.QueryAsync<ListItem>("select * from list_items where Active = 0");
        }

        public List<ListItem> GetActiveListItems()
        {
            return syncconn.Query<ListItem>("select * from list_items where Active = 1");
        }
        public List<ListItem> GetInactiveListItems()
        {
            return syncconn.Query<ListItem>("select * from list_items where Active = 0");
        }
    }
}

【问题讨论】:

    标签: c# sqlite asynchronous xamarin xamarin.forms


    【解决方案1】:

    您可以异步调用命令。只需在 DeleteListItem 命令中的(参数)之前添加 async 关键字。然后您还应该异步调用 DeleteListItemAsync 方法。最后,调用 GetActiveListItemsAsync 而不是 GetActiveListItemsAsync。

            DeleteListItem = new Command(async (parameter) => {
                ListItem item = (ListItem)parameter as ListItem;
    
                await App.ListItemRepo.DeleteListItemAsync(item);
    
                ObservableCollection<ListItem> commandActiveItems =
                    new ObservableCollection<ListItem>(
                        await App.ListItemRepo.GetActiveListItemsAsync());
                activeList.ItemsSource = commandActiveItems;
                activeList.HeightRequest = 50 * commandActiveItems.Count;
    
                ObservableCollection<ListItem> commandInactiveItems =
                    new ObservableCollection<ListItem>(
                        await App.ListItemRepo.GetInactiveListItemsAsync());
                inactiveList.ItemsSource = commandInactiveItems;
                inactiveList.HeightRequest = 50 * commandInactiveItems.Count;
            });
    

    希望对你有帮助!如果没有,请告诉我:)

    【讨论】:

    • 谢谢!但行为并没有改变。这太奇怪了。我尝试将App.ListItemRepo.GetActiveListItems 更改为App.ListItemRepo.GetActiveListItemsAsync,这是有效方法所使用的方法,但我收到此错误:Argument 1: cannot convert from 'System.Threading.Tasks.Task&lt;System.Collections.Generic.List&lt;Myapp.Models.ListItem&gt;&gt;' to 'System.Collections.Generic.List&lt;Myapp.Models.ListItem&gt;'。你知道我该如何补救吗?
    • 你在等那个电话吗?
    • 就像 Jason 建议的那样,您应该等待调用此方法。
    • 异步任务的返回类型在有或没有等待的情况下是不同的。 @JoeMorano您应该避免同步调用异步任务方法,尝试从中获取 .Result 。
    • 你不应该依赖异步方法的同步调用;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多