OnElementPropertyChanged is not triggered in iOS custom renderer - xamarin

OnElementPropertyChanged is not triggered in iOS custom renderer.
I have created a class library. In my Customer Renderer class i have override a OnElementPropertyChanged and this should be called whenever the Property of my View gets changed.
XamarinForms:
public class CustomControl : View
{
public int PageNumber
{
get { return (int)GetValue(PageNumberProperty); }
set { SetValue(PageNumberProperty, value); }
}
public static readonly BindableProperty PageNumberProperty =
BindableProperty.Create<CustomControl, int>(p => p.PageNumber, 0, BindingMode.Default, null, null);
}
CustomRenderer Class iOS:
[assembly: ExportRenderer(typeof(CustomControl), typeof(CustomRenderer))]
namespace XForms.iOS
{
internal class CustomRenderer : ViewRenderer<CustomControl, UIScrollView>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomControl> e)
{
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
}
}
Can anyone tell what i am doing wrong?

Please double check that it is really using the custom renderer by putting a breakpoint into it.
Next make sure that you change proper (PageNumber) after creation of the UI element. During the creation process, property change notifications will not be sent. A similar case is described in the Xamarin forums.

Related

Removing back swipe gesture from page using xamarin forms

Is there a way i can disable the back swipe to previous page option for iOS on one single page of my project ?
You can achieve this by implementing a custom renderer and setting the right property for this. You can see a sample implementation underneath. The right property, in this case, is InteractivePopGestureRecognizer which you need to set to false.
Do this in the ViewWillAppear so the NavigationController is initialized.
using DisableSwipe.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ContentPage), typeof(NoBackSwipeRenderer))]
namespace DisableSwipe.iOS
{
public class NoBackSwipeRenderer : PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (ViewController?.NavigationController != null)
ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = false;
}
}
}
#Symorp
You could do it like so:
public class YourCustomPageRenderer : PageRenderer
{
private YourCustomPage _yourCustomPage;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
_yourCustomPage = e.NewElement as YourCustomPage;
if (_yourCustomPage != null)
{
_yourCustomPage.PropertyChanged += YourCustomPagePropertyChangedEventHandler;
}
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
SetInteractivePopGestureRecognizerEnabled(isEnabled: false);
}
private void YourCustomPagePropertyChangedEventHandler(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
if (propertyChangedEventArgs.PropertyName == nameof(YourCustomPage.IsInteractivePopGestureRecognizerEnabled))
{
SetInteractivePopGestureRecognizerEnabled(_yourCustomPage.IsInteractivePopGestureRecognizerEnabled);
}
}
private void SetInteractivePopGestureRecognizerEnabled(bool isEnabled)
{
var interactivePopGestureRecognizer = ViewController?.NavigationController?.InteractivePopGestureRecognizer;
if (interactivePopGestureRecognizer != null)
{
//Prevents the back-swipe-gesture when the user wants to swipe a page away (from left edge of the screen)
interactivePopGestureRecognizer.Enabled = isEnabled;
}
}
}
public class YourCustomPage : ContentPage
{
/// <summary>
/// If you need it as bindable property, feel free to create a <see cref="BindableProperty"/>.
/// </summary>
public bool IsInteractivePopGestureRecognizerEnabled { get; set; }
}
Feel free to adjust to your needs! :-)
I omitted the export renderer attribute etc., just for simplicity.

Xamarin.Forms ListView with custom renderer selects more than one row after scroll is triggered

I needed to change the color of the selected item in ListView in my Xamarin.Forms application, so I implemented a custom rendered for ViewCell. This worked as required, untill I filled my list with so many elements, that it had to be scrolled. After scroll occurres on the list, it causes a weird bug, where more than one row gets selected (just as if scrolling the list, would change the scope where list looks for selected items and did not reset the selected item). My best guess is that I need to extend my renderer to count for the scrolling of ListView, but I have no idea how to do this. Has anyone encountered similar issues?
Custom Renderer:
[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace MyApp.Droid.PlatformSpecific.Renderers
{
public class ExtendedViewCellRenderer : ViewCellRenderer
{
private Android.Views.View _cellCore;
private Drawable _unselectedBackground;
private bool _selected;
protected override Android.Views.View GetCellCore(Cell item,
Android.Views.View convertView,
ViewGroup parent,
Context context)
{
_cellCore = base.GetCellCore(item, convertView, parent, context);
_selected = false;
_unselectedBackground = _cellCore.Background;
return _cellCore;
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnCellPropertyChanged(sender, args);
if (args.PropertyName == "IsSelected")
{
_selected = !_selected;
if (_selected)
{
var extendedViewCell = sender as ExtendedViewCell;
_cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
}
else
{
_cellCore.SetBackground(_unselectedBackground);
}
}
}
}
}
Custom ViewCell:
public class ExtendedViewCell : ViewCell
{
public static readonly BindableProperty SelectedBackgroundColorProperty =
BindableProperty.Create("SelectedBackgroundColor",
typeof(Color),
typeof(ExtendedViewCell),
Color.Default);
public Color SelectedBackgroundColor
{
get { return (Color)GetValue(SelectedBackgroundColorProperty); }
set { SetValue(SelectedBackgroundColorProperty, value); }
}
}
Update:
I have managed to trace down the issue to beeing caused by different implementation of caching on newer Xamarin.Forms versions. Even the author of the above solution haven't managed to solve this out at this moment: Link

Checkboxes not visible in release mode UWP (Xamarin Forms)

I'm completely new to xamarin.forms.
I used XLabs library to add checkboxes in my PCL project (Xamarin Forms).
When I run my app UWP ARM in Debug mode there's no error, but when I run the app in Release mode the checkboxes ARE never shown.
Is there any setting that I need to configure?
As #hugo said that the XLabs library is no longer maintained. It may not work with newer versions of Xamarin.Forms. For your requirement, you could use Switch control to replacing checkbox or use custom checkbox control. The following code implemented a simple checkbox. For more please refer to Introduction to Custom Renderers.
CustomCheckBox.cs
public class CustomCheckBox : View
{
public static readonly BindableProperty CheckedProperty =
BindableProperty.Create("Checked", typeof(bool), typeof(CustomCheckBox), default(bool));
public bool Checked
{
get { return (bool)GetValue(CheckedProperty); }
set { SetValue(CheckedProperty, value); }
}
}
CustomCheckBoxRenderer.cs
[assembly: ExportRenderer(typeof(CustomCheckBox), typeof(CustomCheckBoxRenderer))]
namespace LabsTest.UWP
{
public class CustomCheckBoxRenderer : ViewRenderer<CustomCheckBox, Windows.UI.Xaml.Controls.CheckBox>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckBox> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new Windows.UI.Xaml.Controls.CheckBox());
}
if (Control != null)
{
Control.IsChecked = Element.Checked;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(Element.Checked))
{
UpdateStatus();
}
}
private void UpdateStatus()
{
Control.IsChecked = Element.Checked;
}
}
}
Usage
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<local:CustomCheckBox x:Name="MyCheckBox" Checked="True">
</local:CustomCheckBox>
</StackLayout>

GTK# - PropertyNotifyEvent doesn't seem to work

I try to make my app to react on property change of a widget, but the signal PropertyNotifyEvent is never invoke.
For test purpose i make a very simple application containing a label and a button. Pressing the button change text property of label but the PropertyNotifyEvent isn't responding.
using System;
using Gtk;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
label1.AddEvents((int)(Gdk.EventMask.PropertyChangeMask));
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}
protected void OnButton1Clicked(object sender, EventArgs e)
{
label1.Text = "new text";
}
protected void OnLabel1PropertyNotifyEvent(object o, PropertyNotifyEventArgs args)
{
Console.WriteLine("label property change");
}
}
I have no idea what I do wrong, do I missing something?
Found a solution:
label1.AddNotification("label", (o, args) => {Console.WriteLine("label property change");});

EventToCommand using Mvvmlight and xamarin forms

When developing for all three mobile platforms using MvvmLight with Xamarin forms, what is the recommended way of binding an event in the view to a command in the viewmodel for events that do not support the command pattern? Is it possible to use EventToCommand?
Thanks!
Not sure about MVVMLight but what you could do is define the Events in an Interface (IPageLifeCycleEvents) which are implemented in the relevant ViewModel. Within the View you would then set the BindingContext as an instance of type IPageLifeCycleEvents and pass the events from the View to the ViewModel through the interface. E.G.
public interface IPageLifeCycleEvents
{
void OnAppearing ();
void OnDisappearing();
void OnLayoutChanged();
}
public class SampleView : ContentPage
{
public BaseView () {
var lifecycleHandler = (IPageLifeCycleEvents) this.BindingContext;
base.Appearing += (object sender, EventArgs e) => {
lifecycleHandler.OnAppearing();
};
base.Disappearing += (object sender, EventArgs e) => {
lifecycleHandler.OnDisappearing ();
};
base.LayoutChanged += (object sender, EventArgs e) => {
lifecycleHandler.OnLayoutChanged();
};
}
}
public class SampleViewModel : IPageLifeCycleEvents
{
#region IPageLifeCycleEvents Methods
public void OnAppearing ()
{
//Do something here
}
public void OnDisappearing ()
{
//Do something here
}
public void OnLayoutChanged ()
{
//Do something here
}
#endregion
}
in my actual implementations I use a slightly different setup because of the utilisation of IOC and Base models.
Good luck

Resources