IsGestureEnabled is not working in xamarin forms for iOS 16 - xamarin.forms

I'm changing the orientation(Landscape/Portrait) forcefully for one screen.
For that, I used IsGestureEnabled property.
That property is not working on iOS 16 or above versions.
CODE:
protected override void OnDisappearing()
{
SetGestureEnabled(true);
base.OnDisappearing();
}
protected override void OnAppearing()
{
SetGestureEnabled(false);
base.OnAppearing();
}
private void SetGestureEnabled(bool isSet)
{
if (Application.Current.MainPage is MasterDetailPage masterDetailPage)
{
masterDetailPage.IsGestureEnabled = isSet;
}
}
Please help me to resolve this issue.

MasterDetailPage is obsolete. Try using FlyoutPage instead.
FlyoutPage also has IsGestureEnabled property which could disable or enable the swipe gesture. The default value is true. you could use almost the same code in your question
if (Application.Current.MainPage is FlyoutPage flyoutPage)
{
flyoutPage.IsGestureEnabled = true;
}
For more info, you could refer to Xamarin.Forms FlyoutPage

Related

Detaching Connectivity Changes Xamarin Forms

I am using Xamarin.Essentials to handle connectivity changes, it all works on android
However I have noticed that in iOS it didnt work, I debugged and noticed that
sliding down the screen in iOS Physical device
Put the phone in Plane Mode and remove wifi
Sleep event fired and removed connectivity
//App.Xaml
protected override void OnSleep()
{
Connectivity.ConnectivityChanged -= OnConnectivityChanged;
}
protected override void OnStart()
{
Connectivity.ConnectivityChanged += OnConnectivityChanged;
}
protected override void OnResume()
{
Connectivity.ConnectivityChanged += OnConnectivityChanged;
}
if I comment out
Connectivity.ConnectivityChanged -= OnConnectivityChanged;
it all works.
Am I missing the obvious?
WHere I am supposed to detach the connectivity?
This is because the Forms lifecycle is somewhat different from the declarative lifecycle approach of native platform, and you can do this directly in the native ios lifecycle.
in your ios project AppDelegate.cs :
public override void OnActivated(UIApplication application)
{
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
}
public override void DidEnterBackground(UIApplication uiApplication)
{
Connectivity.ConnectivityChanged -= Connectivity_ConnectivityChanged;
}
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
//do some thing
}
You could refer to the lefecycle.
Note:
on iOS 13 (and later),you need write them to SceneDelegate as well.

OnAppearing not called in prism xamarin forms

I want a process to be called each time I navigated to my view to refresh a list.
I am using Xamarin Forms and prism framework.
I made my ViewModel derivate from ContentPage but the following method is never called :
protected override void OnAppearing()
{
//Do things
}
How am I supposed to do to get the event? Is it better to use OnNavigateTo?
Through the document:
There are times in your application where you may want to invoke code
in your ViewModel based on when the Page Appears or Disappears without
Navigation specific consideration. For these times you can utilize the
IPageLifecycleAware interface to properly respond to the Appearing and
Disappearing events from the Page.
public class ViewAViewModel : IPageLifecycleAware
{
public void OnAppearing()
{
Console.WriteLine("We are appearing");
}
public void OnDisappearing()
{
Console.WriteLine("We are disappearing");
}
}
I found a solution to make my code work, I add to do this in my code behind from the page:
protected override void OnAppearing()
{
(BindingContext as IPageLifecycleAware)?.OnAppearing();
}
Still a mystery why I need to add this and it is not in the sample.
at the time of this post, the sample does it differently. It uses Behaviors to achieve expectation
It has a PageLifeCycleAwareBehavior class
private void OnAppearing(object sender, EventArgs e)
{
MvvmHelpers.InvokeViewAndViewModelAction<IPageLifecycleAware>(AssociatedObject, aware => aware.OnAppearing());
}
private void OnDisappearing(object sender, EventArgs e)
{
MvvmHelpers.InvokeViewAndViewModelAction<IPageLifecycleAware>(AssociatedObject, aware => aware.OnDisappearing());
}
You can see the full implementation here
You can also call Initialize is a good alternative
Add ini to your class
public class HelloViewModel : BindableBase, IInitialize
then add the following method
public void Initialize(INavigationParameters parameters)
{
// Do stuff
}

Xamarin Forms native customrenderer

I have followed James Montemagno's guide on how to make a custom renderer for round images in my Xamarin Forms Shared Project.
https://blog.xamarin.com/elegant-circle-images-in-xamarin-forms/
(being a true copy of the guide it feels redundant to actually add the code itself to my project but please comment if that is not the case)
It is working flawless, however, I need to change the colour of the circle border dynamically with the press of a button when the app is running.
But since the colour of the circle is set natively in each renderer I am uncertain how I could possibly change it from my shared code.
Maybe this snippet can help:
public class CircleImage : Image
{
public static readonly BindableProperty CurvedBackgroundColorProperty =
BindableProperty.Create(
nameof(CurvedBackgroundColor),
typeof(Color),
typeof(CurvedCornersLabel),
Color.Default);
public Color CurvedBackgroundColor
{
get { return (Color)GetValue(CurvedBackgroundColorProperty); }
set { SetValue(CurvedBackgroundColorProperty, value); }
}
}
//Android/iOS
[assembly: ExportRenderer(typeof(CircleImage), typeof(CircleImageRenderer))]
namespace SchedulingTool.iOS.Renderers
{
public class CircleImageRenderer : ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var xfViewReference = (CircleImage)Element;
//Here you can reference xfViewReference.CurvedBackgroundColor to assign what ever is binded.
}
}
}
}
I hope you get the main idea, you can create your own bindable properties and access them on the Native Renderer.
If everything does not go as expected you can always download the NuGet (which has everything you need):
https://github.com/jamesmontemagno/ImageCirclePlugin

iOS 11 large titles in Xamarin.Forms

How do I enable iOS 11 prefersLargeTitles throughout my Xamarin.Forms app?
I tried creating a custom renderer derived from PageRenderer for NavigationPage, setting:
ViewController.NavigationController.NavigationBar.PrefersLargeTitles = true;
This didn't have any effect, however.
Voila
[assembly: ExportRenderer(typeof(NavigationPage), typeof(NavBarRenderer))]
namespace LargeTitleSample.iOS
{
public class NavBarRenderer : NavigationRenderer
{
protected override void OnElementChanged(Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
NavigationBar.PrefersLargeTitles = true;
}
}
}
You have to create a custom renderer for the NavigationPage inheriting the NavigationRenderer. Then set the PrefersLargeTitles property on the NavigationBar to true.
It seems that when you add some scrollable control to the page, it will automatically have to 'big to small' effect when scrolling up, at least for a ListView.
Working example repo is here: https://github.com/jfversluis/LargeTitleSample
For XAML:
<NavigationPage Title="..." xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" ios:NavigationPage.PrefersLargeTitles="true">

How do I disable bouncing on ScrollView in Xamarin Forms for iOS?

I'm using a ScrollView in Xamarin Forms and on iOS, the scrollview bounces (often too far) when you hit the top or the bottom. My understanding is this is default iOS behavior? Is there a way I can disable this so there is no bounce on scroll?
Yes, disabling the bouncing effect is possible. But you will need to create a Custom Renderer.
In your specific case, you have to use ScrollViewRenderer as a base class for your custom renderer. Then, in your custom renderer, you can simply set Bounces to false.
An example of a Custom Renderer can be found here. Your result should look something like this:
PCL:
public class CustomScrollView : ScrollView
{
public CustomScrollView() {}
}
iOS:
[assembly: ExportRenderer(typeof(CustomScrollView), typeof(CustomScrollViewRenderer))]
namespace Test.iOS
{
public class CustomScrollViewRenderer : ScrollViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
Bounces = false;
}
}
}
On Android, you will need to implement a Custom Renderer as well which simply does nothing.

Resources