|
1 | 1 | # refresh-command-binding-using-mvvm-in-.net-maui-pull-to-refresh |
2 | 2 | This example demonstrates about how to bind the RefreshCommand using MVVM in .NET MAUI PullToRefresh (SfPullToRefresh). |
| 3 | + |
| 4 | +# How to refresh the .NET MAUI PullToRefresh from ViewModel? |
| 5 | + |
| 6 | +`SfPullToRefresh` is fully MVVM compatible and can be refreshed by binding a property in the view model to the [SfPullToRefresh.IsRefreshing]() property. |
| 7 | +`SfPullToRefresh` also provides support for [SfPullToRefresh.RefreshCommand]() that will be executed when the pulling is completed and the pointer is released. You can also pass a desired object as parameter to the `SfPullToRefresh.RefreshCommand` using the [SfPullToRefresh.RefreshCommandParameter](). |
| 8 | +The `SfPullToRefresh.RefreshCommand's` `CanExecute()` will be fired when the pulling action is performed. Returning `false` in the command's `CanExecute()` will cancel the pulling and the `SfPullToRefresh.RefreshCommand` will not be executed. |
| 9 | + |
| 10 | +{% tabs %} |
| 11 | +{% highlight xaml tabtittle="MainPage.xaml" hl_lines="2" %} |
| 12 | + |
| 13 | + <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 14 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 15 | + x:Class="RefreshCommand.MainPage" |
| 16 | + xmlns:syncfusion="clr-namespace:Syncfusion.Maui.PullToRefresh;assembly=Syncfusion.Maui.PullToRefresh" |
| 17 | + xmlns:local="clr-namespace:RefreshCommand"> |
| 18 | + |
| 19 | + <ContentPage.BindingContext> |
| 20 | + <local:ViewModel /> |
| 21 | + </ContentPage.BindingContext> |
| 22 | + |
| 23 | + <ContentPage.Content> |
| 24 | + <syncfusion:SfPullToRefresh x:Name="pullToRefresh" |
| 25 | + IsRefreshing="{Binding IsRefreshing}" |
| 26 | + PullingThreshold="100" |
| 27 | + RefreshCommand="{Binding RefreshCommand}"> |
| 28 | + <syncfusion:SfPullToRefresh.PullableContent> |
| 29 | + <StackLayout BackgroundColor="#00AFF9" |
| 30 | + Orientation="Vertical"> |
| 31 | + <Label Text="New York Temperature" |
| 32 | + FontSize="Large" |
| 33 | + TextColor="White" |
| 34 | + HorizontalTextAlignment="Center" |
| 35 | + Margin="20" /> |
| 36 | + <Label Text="{Binding Temperature}" |
| 37 | + FontSize="Large" |
| 38 | + TextColor="White" |
| 39 | + HorizontalTextAlignment="Center" |
| 40 | + Margin="20" |
| 41 | + HeightRequest="100" /> |
| 42 | + </StackLayout> |
| 43 | + </syncfusion:SfPullToRefresh.PullableContent> |
| 44 | + </syncfusion:SfPullToRefresh> |
| 45 | + </ContentPage.Content> |
| 46 | + </ContentPage> |
| 47 | + |
| 48 | +{% endhighlight %} |
| 49 | +{% highlight c# tabtittle="ViewModel.cs" hl_lines="1" %} |
| 50 | + |
| 51 | + public class ViewModel : INotifyPropertyChanged |
| 52 | + { |
| 53 | + private bool isRefreshing; |
| 54 | + public ICommand RefreshCommand { get; set; } |
| 55 | + |
| 56 | + public bool IsRefreshing |
| 57 | + { |
| 58 | + get |
| 59 | + { |
| 60 | + return isRefreshing; |
| 61 | + } |
| 62 | + set |
| 63 | + { |
| 64 | + isRefreshing = value; |
| 65 | + RaisePropertyChanged(nameof(IsRefreshing)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public ViewModel() |
| 70 | + { |
| 71 | + RefreshCommand = new Command(Refresh); |
| 72 | + } |
| 73 | + |
| 74 | + async private void Refresh(object obj) |
| 75 | + { |
| 76 | + IsRefreshing = true; |
| 77 | + await Task.Delay(1000); |
| 78 | + IsRefreshing = false; |
| 79 | + int index = random.Next(0, 6); |
| 80 | + Temperature = temperatureArray[index]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +{% endhighlight %} |
| 85 | +{% endtabs %} |
0 commit comments