WebView Pull to reload

jb_tcjb_tc USMember ✭✭

Hi,

is there any way to reload the webview by pulling down?

I only find solutions for Scroll or Listview.

Thanks for you help!

Best Answer

Answers

  • RohitSingh.6601RohitSingh.6601 USMember ✭✭

    Have you tried telling the Webveiw to just reload/refresh its current page?

    webView.Source = (webView.Source as UrlWebViewSource).Url;

    Or do you mean that within your Webpage's code (javascript?), you need to instruct the webpage to refresh, and not the Xamarin Forms Webview?

    Project Sample:-

    using System;
    using Xamarin.Forms;
    using System.Diagnostics;

    namespace Forms_WindowsPhone
    {

    public class myContentPage : ContentPage
    {
        WebView webView;
    
        public myContentPage()
        {
            // 
            webView = new WebView(){
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Source = "https://xxxxxxx/xxx62849286/xxxxxx.html"
            };
    
            var refreshButton = new Button (){Text= "Refresh the Webview"};
            refreshButton.Clicked += tryWebviewRefresh;
    
            Content = new StackLayout
            {
                Padding = new Thickness(0, 20, 0, 0),
                Children = {
                    webView,
                    refreshButton
                }
            };
        }
    
        void tryWebviewRefresh (object sender, EventArgs e)
        {
            Debug.WriteLine ("Trying to refresh the webview...");
            webView.Source = (webView.Source as UrlWebViewSource).Url;
        }
    }
    
    public class App : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = new myContentPage();    
        }
    }
    

    }

  • jb_tcjb_tc USMember ✭✭
  • jb_tcjb_tc USMember ✭✭

    No solution for that?

  • juandrnjuandrn Member ✭✭

    Works with https://montemagno.com/pull-to-refresh-anyish-xamarinforms-view/
    but IsBusy = false doesn't stop the Loading and Loading is freeze, to solve it I've added an event to viewmodel and set IsRefreshing property directly from the control

    MainViewModel
    `public class MainViewModel : INotifyPropertyChanged
    {
    bool isBusy;
    public bool IsBusy
    {
    get { return isBusy; }
    set
    {
    isBusy = value;
    OnPropertyChanged("IsBusy");
    }
    }

        ICommand refreshCommand;
        public ICommand RefreshCommand
        {
            get { return refreshCommand ?? (refreshCommand = new Command(async () => await ExecuteRefreshCommand())); }
        }
    
        async Task ExecuteRefreshCommand()
        {
            await Task.Run(() =>
            {
                if (IsBusy)
                    return;
    
                this.Refreshing?.Invoke(null, this);
    
                IsBusy = false;
            });
        }
    
        public event EventHandler<MainViewModel> Refreshing;
    
        #region INotifyPropertyChanged implementation
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged == null)
                return;
    
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }`
    

    MainPage
    `public MainPage()
    {
    InitializeComponent();

            var mainViewModel = new MainViewModel();
            mainViewModel.Refreshing += MainViewModel_Refreshing;
            this.BindingContext = mainViewModel;
        }
    
        private void MainViewModel_Refreshing(object sender, MainViewModel e)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                this.web.Reload();
                this.pr.IsRefreshing = false;
            });
        }`
    

    Xaml
    xml <controls:PullToRefreshLayout IsPullToRefreshEnabled="True" RefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}" RefreshColor="GreenYellow" RefreshBackgroundColor="Black" x:Name="pr" > <WebView x:Name="web" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"></WebView> </controls:PullToRefreshLayout>

    NOTE: Why code keep solaped : S and xaml code is a disaster How to fix display here?

Sign In or Register to comment.