【问题标题】:Why is the refresh to pull icon stuck refreshing?为什么刷新拉图标卡在刷新?
【发布时间】:2022-08-14 07:26:50
【问题描述】:

我有这个 RefreshCommand 用于刷新 \"notes\" 的数据库,但是当我刷新页面时,不是刷新 2 秒然后只是给我笔记,而是不断刷新,这不是什么我希望它这样做,而且它落后于应用程序。

这是代码

MyNotePage.xaml

<ContentPage xmlns=\"http://xamarin.com/schemas/2014/forms\"
     xmlns:x=\"http://schemas.microsoft.com/winfx/2009/xaml\"
     xmlns:model=\"clr-namespace:MyApp.Models\"
     xmlns:mvvm=\"clr-namespace:MvvmHelpers;assembly=MvvmHelpers\"
     xmlns:viewmodels=\"clr-namespace:MyApp.ViewModels\"
     xmlns:xct=\"http://xamarin.com/schemas/2020/toolkit\"
     x:Class=\"MyApp.MyNotePage\"
     x:DataType=\"viewmodels:MyNoteViewModel\"
     BackgroundColor=\"White\">
<ContentPage.BindingContext>
    <viewmodels:MyNoteViewModel/>
</ContentPage.BindingContext>

<ContentPage.Resources>
    <ResourceDictionary>
        <xct:ItemSelectedEventArgsConverter x:Key=\"ItemSelectedEventArgsConverter\"/>
    </ResourceDictionary>
</ContentPage.Resources>

<ContentPage.ToolbarItems>
    <ToolbarItem Text=\"Add\" Command=\"{Binding AddCommand}\"/>
</ContentPage.ToolbarItems>

<ListView
BackgroundColor=\"Transparent\"
CachingStrategy=\"RecycleElement\"
HasUnevenRows=\"True\"
IsPullToRefreshEnabled=\"True\"
IsRefreshing=\"{Binding IsBusy, Mode=OneWay}\"
ItemsSource=\"{Binding Note}\"
RefreshCommand=\"{Binding RefreshCommand}\"
RefreshControlColor=\"DarkViolet\"
SelectionMode=\"None\"
SeparatorVisibility=\"None\">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType=\"model:Note\">
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem
                    Command=\"{Binding Source={Binding MyNotePage}, Path=BindingContext.RemoveCommand}\"
                    CommandParameter=\"{Binding .}\"
                    IsDestructive=\"True\"
                    Text=\"Delete\"/>
                </ViewCell.ContextActions>
                <Grid Padding=\"10\">
                    <Frame CornerRadius=\"20\" HasShadow=\"True\">
                        <StackLayout Orientation=\"Horizontal\">
                            <StackLayout VerticalOptions=\"Center\">
                                <Label
                                FontSize=\"Large\"
                                Text=\"{Binding Name}\"
                                VerticalOptions=\"Center\"/>
                                <Label
                                FontSize=\"Small\"
                                Text=\"{Binding Id}\"
                                VerticalOptions=\"Center\"/>
                            </StackLayout>
                        </StackLayout>
                    </Frame>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MyNotePage.xaml.cs

namespace MyApp
{

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyNotePage
    {
        public MyNotePage()
        {
            InitializeComponent();
        }


    }
}

MyNoteViewModel.cs

namespace MyApp.ViewModels
{
    public class MyNoteViewModel : ViewModelBase
    {

        public ObservableRangeCollection<Note> Note { get; }
        public AsyncCommand RefreshCommand { get; }

        public AsyncCommand AddCommand { get; }

        public AsyncCommand<Note> RemoveCommand { get; }
        public new bool IsBusy { get; private set; }

        public MyNoteViewModel()
        {

            Note = new ObservableRangeCollection<Note>();

            RefreshCommand = new AsyncCommand(Refresh);
            AddCommand = new AsyncCommand(Add);
            RemoveCommand = new AsyncCommand<Note>(Remove);
        }

        async Task Add()
        {
            var name = await App.Current.MainPage.DisplayPromptAsync(\"Notes\", \"Enter your notes here\");
            await NoteService.AddNote(name);
            await Refresh();
        }

        async Task Remove(Note note)
        {
            await NoteService.RemoveNote(note.Id);
            await Refresh();
        }


        async Task Refresh()
        {
            IsBusy = true;
            await Task.Delay(2000);
            Note.Clear();
            var notes = await NoteService.GetNote();
            Note.AddRange(notes);
            IsBusy = false;
        }
    }
}

和 NoteService.cs

namespace MyApp.Services
{
    public static class NoteService
    {

        static SQLiteAsyncConnection db;
        static async Task Init()
        {

            if (db != null)
                return;
            {
                var databasePath = Path.Combine(FileSystem.AppDataDirectory, \"MyData.db\");

                db = new SQLiteAsyncConnection(databasePath);

                await db.CreateTableAsync<Note>();
            }
        }

        public static async Task AddNote(string name)
        {
            await Init();
            var note = new Note()
            {
                Name = name,
            };

            await db.InsertAsync(note);

        }
        public static async Task RemoveNote(int id)
        {
            await Init();
            await db.DeleteAsync<Note>(id);

        }
        public static async Task<IEnumerable<Note>> GetNote()
        {
            await Init();

            var note = await db.Table<Note>().ToListAsync();
            return note;

        }

    }
}

也许这与 IsBusy 有关?我不知道我是否正确设置了它。 bool 确实从 false 变为 true,但看起来它并没有做相反的事情来停止刷新。谢谢您的帮助。

    标签: c# sqlite xamarin xamarin.forms


    【解决方案1】:

    我在你之前的问题中向你指出了这一点。 IsBusy 不会引发 PropertyChanged 事件,因此永远不会通知 UI

    【讨论】:

    • 你能解释一下我会怎么做吗
    • 它(可能)内置在 ViewModelBase 中,但我不知道您使用什么特定包来获取它,所以我无法提供具体示例
    • 即,请告诉我您从哪个包(或其他)获得该课程
    • 我修复了它,public new bool IsBusy { get; private set; } 行是不必要的,它实际上禁止 IsBusy 正常运行。感谢您的帮助。
    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多