Permalink
Cannot retrieve contributors at this time
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?
6002CEM_ObadaNasr/Behaviors/FocusBehavior.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (33 sloc)
1.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |