Git Product home page Git Product logo

xamarinbehaviorstoolkit's Introduction

The Behaviors Toolkit for Xamarin

The Behaviors Toolkit for Xamarin is an easy-to-use means of adding common and reusable interactivity to your Xamarin applications with minimal code. Use of the Behaviors Toolkit for Xamarin is governed by the MIT License.

Build Status

Build status

Getting Started

As a primary Windows (XAML) developer, I've always enjoyed using the behaviors to easily add, with just some lines of code, some great features to any controls. Since I work with Xamarin, I've always thought it could be a good idea to get this functionality in the Xamarin projects.

So welcome to the home of the Behaviors Toolkit for Xamarin, a custom implementation of the XAML behaviors for the Xamarin (Android / iOS) projects!

Current Release

Current release (0.0.3) is available from Nuget: https://www.nuget.org/packages/Xamarin.Behaviors.Toolkit/

Code Example

Using a behavior

To attach a behavior to a control (Button, EditText, etc.), you just have to use the AttachBehavior method.

Take a look at the following code sample (for Xamarin.Android) showing how to use a behavior in an application, just with a snippet of code:

var mySecondEditText = FindViewById<EditText>(Resource.Id.MySecondEditText);
mySecondEditText.AttachBehavior(new SelectAllOnFocusBehavior());

Creating a behavior

The Behaviors Toolkit for Xamarin use the same architecture as the Microsoft behaviors which can be used in any XAML application. To create your own behavior, you just have to inherit from the Behavior<T> class and override the methods OnAttached and/or OnDetaching. To reference the control on which the behavior is attached, you can access the property AssociatedObject:

/// <summary>
/// Behavior used on an EditText object and used to hide the software keyboard when one of the following key is pressed: "Done", "Search" or "Go"
/// </summary>
public class HideKeyboardOnEnterKeyBehavior : Behavior<EditText>
{
    /// <summary>
    /// Gets or sets the application context.
    /// </summary>
    /// <value>
    /// The application context.
    /// </value>
    public Context ApplicationContext { get; set; }

    private EventHandler<TextView.EditorActionEventArgs> _editorActionEventHandler;

    /// <summary>
    /// Method to override when the behavior is attached to the view.
    /// </summary>
    /// <exception cref="System.InvalidOperationException">ApplicationContext property needs to be set in order to use this behavior.</exception>
    protected override void OnAttached()
    {
        if (this.ApplicationContext == null)
        {
            throw new InvalidOperationException("ApplicationContext property needs to be set in order to use this behavior.");
        }

        _editorActionEventHandler = (sender, args) =>
        {
            if (args.ActionId == ImeAction.Done || args.ActionId == ImeAction.Search || args.ActionId == ImeAction.Go || args.ActionId == ImeAction.Next)
            {
                var inputManager = (InputMethodManager)this.ApplicationContext.GetSystemService(Context.InputMethodService);
                inputManager.HideSoftInputFromWindow(this.AssociatedObject.WindowToken, HideSoftInputFlags.None);

                args.Handled = true;
            }
        };

        this.AssociatedObject.EditorAction += _editorActionEventHandler;
    }

    /// <summary>
    /// Method to override when the behavior is removed from the view.
    /// </summary>
    protected override void OnDetaching()
    {
        if (_editorActionEventHandler != null)
        {
            this.AssociatedObject.EditorAction -= _editorActionEventHandler;
        }
    }
}

Here is another sample behavior, for Xamarin.iOS applications:

/// <summary>
/// Behavior used on an EditText object and used to select all the text within it when it has focus
/// </summary>
public class SelectAllOnFocusBehavior : Behavior<UITextField>
{
    private EventHandler _editingDidBeginEventHandler;

    /// <summary>
    /// Method to override when the behavior is attached to the view.
    /// </summary>
    protected override void OnAttached()
    {
        _editingDidBeginEventHandler = (sender, e) =>
        {
            this.AssociatedObject.PerformSelector(new Selector("selectAll"), null, 0.0f);
        };

        this.AssociatedObject.EditingDidBegin += _editingDidBeginEventHandler;
    }

    /// <summary>
    /// Method to override when the behavior is removed from the associatedObject.
    /// </summary>
    protected override void OnDetaching()
    {
        if (_editingDidBeginEventHandler != null)
        {
            this.AssociatedObject.EditingDidBegin -= _editingDidBeginEventHandler;
        }
    }
}

Todo

  • Add more built-in behaviors for Xamarin.Android projects
  • Add more built-in behaviors for Xamarin.iOS projects

More Info

xamarinbehaviorstoolkit's People

Contributors

thomaslebrun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.