Any method in xamarin forms equivalent to ShouldOverrideUrlLoading method of android? - xamarin.forms

Any method in xamarin forms equivalent to shouldoverride method of android?
I am looking for a method that I can call when a button is clicked on webview. this is the android sample code that is working.
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new HybridWebViewClient(this));
webView.Settings.DomStorageEnabled = true;
on HybridWebViewClient class
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
}

Xamarin.Forms is a platform that integrates Android and iOS and Windows. For platform-specific features, you can use Custom renderer to call code on Android platform.
For details you can refer to: What is Xamarin.Forms and Custom renderer.
Using the OnPageStarted method, the OnPageFinished method, and the OnReceivedError method is also a good choice.
Details can be found in the answer at this link:Xamarin.Forms ShouldOverrideUrlLoading Trigger

Related

Where put and how call method common to several Xamarin Forms pages?

If I want to write a method that I can call from the code of any of my Xamarin Forms pages, and that will be able to do things on the UI -- a Popup specifically -- where do I put the method (App? AppShell?) and how do I call it?
VS 2019 Windows. Xamarin Forms app created from the New Solution Flyout template.
You could define a method in the App.xaml.cs,below is a sample for display a alertdialog:
in App.xaml.cs:
public static async void Alert()
{
await App.Current.MainPage.DisplayAlert("Warning", "this is a alert", "Cancel");
}
call in other pages:
App.Alert();

Unable to override OnApplyTemplate

I'm trying to override the NavigationView behavior:
public partial class CustomizableNavigationView : NavigationView
{
public CustomizableNavigationView()
{
// This gets called
}
protected override void OnApplyTemplate()
{
// This doesn't
}
}
It works on UWP, but not on Android. On Android, it doesn't call OnApplyTemplate and the screen remains blank, there's not content. Questions:
Why doesn't OnApplyTemplate get called on Android? I see that here: https://platform.uno/docs/articles/implemented/windows-ui-xaml-frameworkelement.html it says OnApplyTemplate() is on all platforms
There's no error or anything displayed in the Output panne in VS while running with debugger. Should there be any in this case? Do I need to enable something to log errors?
I noticed that if I don't use partial it gaves me error saying partial is required. This is required only on Android, why is that? A more in-depth explanation would help a lot to understand how things work.
Once I figure out why OnApplyTemplate is not called, I want to do this:
base.OnApplyTemplate();
var settingsItem = (NavigationViewItem)GetTemplateChild("SettingsNavPaneItem");
settingsItem.Content = "Custom text";
My hunch is this won't work on Android. Am I correct? :)
Jerome's answer explains why OnApplyTemplate() was not getting called, to address your other questions:
You can configure logging filters for Uno, this is normally defined in App.xaml.cs. Warnings should be logged by default.
The partial is required because Uno does some code-gen behind the scenes to create plumbing methods used by the Xamarin runtime. Specifically because the control is ultimately inheriting from ViewGroup on Android, it's a native object, and requires special constructors that are used only by Xamarin's interop layer. There's some documentation in progress on this.
Try it and see. :) GetTemplateChild() is supported, and setting ContentControl.Content in this way is supported, so I would expect it to work.
At current version (1.45 and below), the application of styles is behaving differently from UWP. We're keeping track of this in this issue.
The gist of the issue is that Uno resolves the style using the current type and not DefaultStyleKey, and cannot find an implicit style for CustomizableNavigationView.
A workaround for this is to either create a named style from the default NavigationView style, or create an implicit style that uses CustomizableNavigationView as the TargetType instead of NavigationView.

Xamarin Forms with MvvmCross 5.7 First View not loaded

I have an Xamarin Application together with MvvmCross 5.7 and wanted to moved it completly to Xamarin Forms. It builds and starts as expected, but the first page isn't loaded.
I created the projects based this template: https://github.com/martijn00/MvxForms
Also I created a test project to see if something is wrong with my existing project: https://github.com/NPadrutt/XFTestProject
Can anyone point out what I am missing?
Either add a SplashScreen Activity who inherits from MvxSplashScreenActivity and with the method override:
protected override void TriggerFirstNavigate()
{
StartActivity(typeof(MainActivity));
base.TriggerFirstNavigate();
}
Or add these lines to the OnCreate Method in the MainActivity:
var startup = Mvx.Resolve<IMvxAppStart>();
startup.Start();
InitializeForms(bundle);
You don't neet to call startup.Start() in your MainActivity nor you need to init xamarin forms. It's done for you now (check RunAppStart method in mvvmcross sources for MvxFormsAppCompatActivity class).
From a quick glimpse at your GitHub repo, it looks like you're not decorating your view (i.e. WelcomView) with [MvxContentPagePresentation()] attribute (e.g. example from MvvmCross Playground). Add it in your WelcomeView.xaml.cs file and check if that helped
If it's a fresh project, you might want to consider using latest version of MvvmCross (v6). There's an awesome step by step guide to setup Xamarin.Forms with it by Nick Randolph

Why is my custom renderer not working?

I have the following class in my Droid-Project:
using MyProject.Droid;
using Xamarin.Forms.Platform.Android;
[assembly: Xamarin.Forms.ExportRenderer(typeof(Android.Widget.Button), typeof(ArrowButtonRenderer))]
namespace MyProject.Droid
{
public class ArrowButtonRenderer : Xamarin.Forms.Platform.Android.ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var btn = this.Control as Android.Widget.Button;
btn.SetBackgroundColor(global::Android.Graphics.Color.Green);
}
}
}
I know, you dont need a custom renderer for changing a button's color, but I just wanna try things out :)
Cheers!
Edit: Here's the code about how I add the button's to the UI:
MyStackLayout.Children.Add(new Button
{
Text = "My Button Text"
});
And some buttons through xaml:
<Button x:Name="btnIdk" Text="something" />
After an extensive chat and looking at your sources I've seen that the PCL has version 2.3.1.114 of Xamarin.Forms installed.
Your Droid project has version 1.5.x installed, so it's much older!
Now the next part will be tricky, you need to update your Xamarin.Forms package for Android. But if you update the Xamarin.Android.Support.* packages first, you'll get an error saying that your can't update Xamarin.Forms because no matching version of Forms is found.
Because Xamarin.Forms for Android depends heavily on the support packages they are linked to a specific version, but they're not always in sync. As of right now the Xamarin.Forms version (2.3.2.127) seems to miss the corresponding Android support packages. So don't upgrade to that, upgrade to the same version as your PCL which is 2.3.1.114. Then the Android packages will be updated to 23.3.0 and everything will work nicely.

Xamarin Forms Hardware Button CrossPlateform

I don't find any solution or explaination to use the BackButton hardware (Android & WinPhone 8.1) from MyPage, defined in the PCL part.
Is it possible to handle that button from a Portable Class Library?
Thank
Yes it is. You can ovveride default behaviour for back button in pcl. For example if you want to deactivate it, you can use in the page you want:
protected override bool OnBackButtonPressed()
{
return true;
}

Resources