Skip to content
Permalink
d90c5b6631
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
44 lines (33 sloc) 1.32 KB
using System;
using System.Runtime.CompilerServices;
namespace app.Behaviors {
public class FocusBehavior : Behavior<View> {
private View currentView;
public static BindableProperty IsFocusedProperty = BindableProperty.Create(nameof(IsFocused), typeof(bool), typeof(FocusBehavior));
public bool IsFocused {
get => (bool)GetValue(IsFocusedProperty);
set => SetValue(IsFocusedProperty, value);
}
public FocusBehavior() {
}
protected override void OnAttachedTo(View bindable) {
base.OnAttachedTo(bindable);
currentView = bindable;
currentView.Unfocused += CurrentView_Unfocused;
}
private void CurrentView_Unfocused(object sender, FocusEventArgs e) {
IsFocused = false;
}
protected override void OnDetachingFrom(View bindable) {
base.OnDetachingFrom(bindable);
currentView.Unfocused -= CurrentView_Unfocused;
currentView = null;
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null) {
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(IsFocused) && IsFocused && currentView != null) {
currentView.Focus();
}
}
}
}