I am using WebView in my UWP application and I want to clear the cache while closing the app, is there a way? I know I can disable cache by adding headers into my HttpRequestMessage as mentioned in this link. However, I want to be able to clear the cache upon app exit.
I did try WebView.ClearTemporaryWebDataAsync() without any success.
Once something is cached it normally remains throughout the app.
Any help is appreciated, thanks.
Edit : Adding 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 cache and when I navigate to same page again its cache should be cleared. Am I doing something wrong here?
In UWP (XAML) there is the ClearTemporaryWebDataAsync method, that allows to webview's cache and IndexedDB data. And there is similar method for JavaScript in MSApp - clearTemporaryWebDataAsync.
Here is code sample (based on your one) 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>
C#:
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 refresh button, cache is cleared -- I see green image.
There was no possibility to do it in 8.1 according
Ten Things You Need to Know About WebView – An Update for Windows 8.1
the closest method you can use now is WebView.Refresh that "reloads the file without forced cache validation by sending a "Pragma:no-cache" header to the server."
... or just clear cache in explorer
Related
I'm running into an issue on android where our TimePicker and DatePicker stay visible when we navigate OnPause(). We need to redirect our users back to the login screen when they background the application, but if the TimePicker or DatePicker is active when they do this it stays on the screen. It appears above the login screen and pressing cancel or ok crashes the app.
We are hooking into the native android lifecycle events (not just using Xamarins built in hooks) and we redirect OnResume(). I've tested this in a barebones app though and it still happens OnPause().
Here is our TimePicker causing us the issue:
<TimePicker x:Name="VitalTimePicker" HorizontalOptions="Fill" VerticalOptions="Fill" IsVisible="false" PropertyChanged="OnTimePickerPropertyChanged"/>
And here is an example of changing screens on a lifecycle event:
protected override void OnSleep()
{
App.Current.MainPage = new NavigationPage(new NotesPage());
}
Any ideas? I was thinking of clearing the Pickers but I can't seem to find how to do that
Edit
Just to add a little more context
The Application class (app.xaml.cs) has lifecycle hooks that we use to catch when our users background the app. In here we call MainPage = new NavigationPage(new LoginPage()); which takes the app back to the login page.
I've added
protected override void OnDisappearing()
{
VitalTimePicker.Unfocus();
VitalDatePicker.Unfocus();
}
to the view i'm working from and it seems to be called when we background the application, but for some reason the TimePicker is staying on the screen when our login page pops up again.
You can programmatically close DatePickers and TimePickers using the method Unfocus(). I'd recommend closing them before you call the next page, as I don't know if they will be able to be referenced and closed after the other screen has been initialized.
Create a method that calls VitalTimePicker.Unfocus() and the same for any other picker you have, and call this method before changing to login screen and you should be good to go.
I want create a Rich Text box with or without web view in xamarin forms. I tried many examples but none of that worked for me. The examples are
https://github.com/XAM-Consulting/TEditor
The problem with this example was, it is too slow for me and it was not editing my text at all. And it was getting crash numerous times. So I tried to follow this example
https://forums.xamarin.com/discussion/95318/rich-text-box-in-xamarin-forms
but the code by Adam.Else gave me lots of bugs and it was not working. I have reported the bug in stackoverflow. As you can see this link below-
Unhandled Exception: Xamarin.Forms.Xaml.XamlParseException: Position 12:21. StaticResource not found for key FromRTFConverter occurred
I don't know how to fix this issue. Any suggestions?
I did it using web view. First I downloaded a Rich text box which was created using HTML/CSS and JavaScript. This is the link for the Rich text box which I'm going to use as the web view in my xamarin forms app.
MainPage.xaml
<StackLayout>
<!-- Place new controls here -->
<WebView x:Name="MyWebView" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
</StackLayout>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
MyWebView.Source = new HtmlWebViewSource();
MyWebView.Source = DependencyService.Get<IBaseUrl>().Get();
}
}
I created a interface called IBaseUrl. This is my code
public interface IBaseUrl { string Get(); }
And I created a class in Native called BaseUrl_Android for calling the Html file which I have put in my Assets folder in android. This is my code for BaseUrl_Android
[assembly: Dependency(typeof(BaseUrl_Android))]
namespace webviewdemo.Droid
{
public class BaseUrl_Android : IBaseUrl
{
public string Get()
{
return "file:///android_asset/fontawesome5.html";
}
}
}
Add your HTML and JavaScript file in assets folder. If you have any doubt in adding and using html file in xamarin forms refer this link. This is all you have to do to get an awesome Rich text box in Xamarin Forms.
This is the screenshot of my Rich text box
Is there any way to get the htlm code of the textarea like the java script does in c# or directly by typing a new code in the html file. capture of the html code retrieved by the javascript function
I tried to retrieve the html code by using a simple implementation in the html file and in the C# file by using the following code:
await webview.EvaluateJavaScriptAsync($"document.getElementById('theText').value;");
Unfortunately, the html code is totally different and the
I am using a PCL project. In this I am loading a url in a Xamarin forms webview container. This url has a button with a telephone number in it. When I load it in the same url in the phone's chrome browser, it brings up the dialer, however, when I load the url in a Xamarin.Forms.WebView control I get an error net::ERR_UNKNOWN_URL_SCHEME
This is my code in the xaml page. This is a Xamarin.Forms.Webview control here.
<WebView x:Name="wvCntr" Source="{Binding TheWebUrl, Mode=TwoWay }"
WidthRequest="1000" HeightRequest="1000" />
Then in my Xaml.cs page, I have this code
public class WContainer : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
{
//What code here?
return true;
}
}
How is supposed to work? The Xaml page has Xamarin.Forms.Webview and the code behind I am using an Android.Webkit.Webview. I am not able to make the connect here and then get the code which actually pulls up the dialer. I am using Xamarin in Visual Studio 2015.
Any help or code appreciated.
I have made a webpage. The IE 7 of windows 7 doesn't execute Javascript. How do I enable Javascript on the Windows Phone?
Windows Phone 7 runs a mobile version of IE9 so there should be no problems executing javascript. The only thing I can think of with the little information is that you are trying to use a webbrowsercontrol from within an application. In that case you should set the IsScriptEnabled property to true to enable scripts within the control.
chirag !
In window phone 7, you can enable Javascript.
in xaml , you can set isScriptEnabled= true.
Examle:
<phone:WebBrowser HorizontalAlignment="Left" Width="480" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Visible" Name="webBrowser" Height="80" VerticalAlignment="Bottom" IsScriptEnabled="True" ScriptNotify="webBrowser_ScriptNotify" Navigated="webBrowser_Navigated" Navigating="webBrowser_Navigating" />
you can use event :
private void webBrowser_ScriptNotify(object sender, Microsoft.Phone.Controls.NotifyEventArgs e)
{
//your code : e is returned from the javascript code if javaScript is active
}
You can down load HTML content by use : webClient or string htmlcontent = webBrowser.SaveToString
Hope this help ! Thongaduka
I want to scroll down or up in the webbrowser control on the windows phone 7 using code behind (no javascript). i mean like using some button to scroll down for example. is that possible?
EDIT:
I tried to call a javascript function using InvokeScript, but it keeps giving me an unknown error 80020006. i tried to do this:
public MainPage()
{
InitializeComponent();
webBrowser1.Navigate(new Uri("http://www.msn.com"));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
webBrowser1.InvokeScript("window.scrollBy(100,100);");
}
something wrong in my code?
There is no way to interact with the scrolling of the page inside a WebBrowser control from outside of it.
In terms of doing it with JavaScript look at windows.scrollBy()
update
Try webBrowser1.InvokeScript("eval", "window.scrollBy(100,100);");
But be aware that the page you're viewing may have overridden eval which coudl prevent this from running.
Note that the WebBrowser control was not intended to be used to view websites directly.
Also, have you tried calling scrollBy directly from within your own page?