Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ListView PullToRefresh doesn't have RefreshCommandParameter - how I got around it.

$
0
0

So I implemented Pull-To-Refresh in my App today, but in the process, I've discovered that there isn't a RefreshCommandParameter that I could bind to. The reason I was looking for said property is because I wanted to bind ListView.IsRefreshing to my ViewModel.IsBusy property.

Since I couldn't pass the view model in as I wanted to (<ListView .... RefreshCommandParameter="{Binding}" />), I had to come up with another way... by exposing statics on my ViewModel (yuck).

What I did was setup my ViewModel like this

public class MyViewModel {
    private static MyViewModel _context;
    private readonly RefreshProjectListCommand _refreshProjectListCommand;

    public MyViewModel(RefreshProjectListCommand refreshProjectListCommand) {
        _context = this;
        _refreshProjectListCommand = refreshProjectListCommand;
    } 

    public string IsBusyPropertyName= @"IsBusy";
    private bool _isBusy;
    public bool IsBusy {
        get {return _isBusy}
        set { SetField(ref _isBusy, value, IsBusyPropertyName);}
    }

    public string ProjectsPropertyName = @"Projects";
    provate ProjectModel _projects;
    public ObservableCollection<ProjectModel> Projects
    {
        get { return _projects; }
        set { SetField(ref _projects, value, ProjectsPropertyName); }
    }

    public Command RefreshProjectListCommand
    {
        get
        {
            return new Command(
                parameter => _refreshProjectListCommand.Execute(parameter),
                parameter => _refreshProjectListCommand.CanExecute(parameter)
                );
        }
    }

    public static void SetIsBusy(bool isBusy) {
        // this exists because the ListView doesn't expose a RefreshCommandParameter
        _context.IsBusy = isBusy;
    }

    public static void SetProjects(ObservableCollection<ProjectModel> projects) {
        // this exists because the ListView doesn't expose a RefreshCommandParameter
        _context.Projects = projects;
    }
}

Then my command looks like this.

    public override bool CanExecute(object parameter)
    {
        return true;
    }

    public override async void Execute(object parameter)
    {
        MenuPageViewModel.SetIsBusy(true);

        await _syncEngine.ProjectSyncAsync();

        var p = await _projectService.FindByUserIdAsync(App.CurrentUser.Id);
        var projectsToReturn = _menuPageViewModelMapper.BuildListFrom(p).ToObservableCollection();

        MyPage.SetProjects(projectsToReturn);
        MyPage.SetIsBusy(false);
    }

Now I'm able to update the ObservableCollection<ProjectModel> AND update the IsRefreshing property even though I'm missing the RefreshCommandParameter.

@TheRealJasonSmith, any chance we can see that property in the future?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>