How to clear UWP WebView cache?

I use WebView in my UWP application and I want to clear the cache when closing the application, is there any way? I know that I can disable the cache by adding headers to my HttpRequestMessage, as indicated in this link. However, I want to clear the cache when I exit the application.

I tried WebView.ClearTemporaryWebDataAsync () without any success. Once something is cached, it usually stays in the entire application. Any help is appreciated, thanks.

Edit: add code snippet

var webView = new WebView();  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));  
await WebView.ClearTemporaryWebDataAsync(); //static method  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));

I expect the static method to clear the cache, and when I go to the same page again, its cache should be cleared. Am I something wrong here?

+4
source share
2 answers

UWP (XAML) has a method ClearTemporaryWebDataAsyncthat allows you to view cache data on the Internet and indexed data. And a similar method for JavaScript in MSApp is ClearTemporaryWebDataAsync.

Here is a sample code (based on yours) that works for me:

XAML:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
     <StackPanel>
         <WebView x:Name="webView" Width="800" Height="600"></WebView>
         <Button x:Name="refreshBtn" Content="Refresh" ></Button>
     </StackPanel>
 </Grid>

WITH#:

    public MainPage()
    {
        this.InitializeComponent();
        refreshBtn.Tapped += RefreshBtn_Tapped;

        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));


    }

    private async void RefreshBtn_Tapped(object sender, TappedRoutedEventArgs e)
    {
        await Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync();
        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
    }

When I click the refresh button, the cache is cleared - I see a green image.

+3
source

It was impossible to do this in 8.1 according to Ten Things You Need to Know About WebView - Update for Windows 8.1

, , WebView.Refresh, " , " Pragma: no- " ."
...

0

All Articles