Coder Social home page Coder Social logo

brminnick / entrycustomreturnplugin Goto Github PK

View Code? Open in Web Editor NEW
81.0 6.0 11.0 11.2 MB

Xamarin.Forms Plugin to customize the Xamarin.Forms.Entry Keyboard Return Button

License: MIT License

C# 100.00%
nuget xamarin xamarin-forms xamarin-android xamarin-ios xamarin-plugin

entrycustomreturnplugin's Introduction

Custom Xamarin.Forms.Entry Keyboard Return Button

NuGet

This SDK was officially merged into Xamarin.Forms v3.1.0.

Because this SDK supports Xamarin.Forms v2.5.0.280555, I will continue to maintain it for teams who have not yet upgraded to Xamarin.Forms v3.1.0 or higher.

For developers using Xamarin.Forms 3.1.0 or higher, I recommend removing this NuGet package from your csproj and using the APIs included in Xamarin.Forms: Xamarin.Forms.Entry.ReturnType & Xamarin.Forms.Entry.ReturnCommand.

ReturnType

ReturnType Android iOS UWP
Default
Done
Go
Next
Search
Send

Platform Support

Platform Supported Version
Xamarin.iOS Yes iOS 8+
Xamarin.iOS Unified Yes iOS 8+
Xamarin.Android Yes API 15+
Windows 10 UWP Yes 10+
Windows Phone Silverlight No
Windows Phone RT No
Windows Store RT No
Xamarin.Mac No

This plugin can be consumed as a CustomRenderer Control or as an Effect.

Setup

iOS

In the FinishedLaunching method of AppDelegate.cs, add CustomReturnEntryRenderer.Init();:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        ...

        global::Xamarin.Forms.Forms.Init();
        
        EntryCustomReturn.Forms.Plugin.iOS.CustomReturnEntryRenderer.Init();

        ...
    }
}

Note: You must call EntryCustomReturn.Forms.Plugin.iOS.CustomReturnEntryRenderer.Init(); after you call global::Xamarin.Forms.Forms.Init();

Android

In the Oncreated method of MainActivity.cs, add CustomReturnEntryRenderer.Init();:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        ...

        global::Xamarin.Forms.Forms.Init(this, bundle);

        EntryCustomReturn.Forms.Plugin.Android.CustomReturnEntryRenderer.Init();

        ...
    }
}

Note: You must call EntryCustomReturn.Forms.Plugin.Android.CustomReturnEntryRenderer.Init(); after you call global::Xamarin.Forms.Forms.Init(this, bundle);

UWP

In the OnLaunched method of App.xaml.cs, add CustomReturnEntryRenderer.Init();:

public partial class App : Application
{
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        ...

        global::Xamarin.Forms.Forms.Init(e);

        EntryCustomReturn.Forms.Plugin.UWP.CustomReturnEntryRenderer.Init();

        ...
    }
}

Note: You must call EntryCustomReturn.Forms.Plugin.UWP.CustomReturnEntryRenderer.Init(); after you call global::Xamarin.Forms.Forms.Init(e);

Usage in Xamarin.Forms Project as a Custom Control

This plugin can be consumed as a CustomRenderer Control or as an Effect.

1. Set the ReturnType Property

The ReturnType property is an enum containing 6 different types: Default, Go, Next, Done, Send, Search.

Coded UI

var goReturnTypeCustomEntry = new CustomReturnEntry
{
    ReturnType = EntryCustomReturn.Forms.Plugin.Abstractions.ReturnType.Go
};

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions">

    <ContentPage.Content>

        <entryCustomReturn:CustomReturnEntry
            x:Name = "MyCustomReturnEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            ReturnType="Go"/>

    </ContentPage.Content>
</ContentPage>

Bindable Property

ReturnType can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new CustomReturnEntry();
customReturnEntry.SetBinding(CustomReturnEntry.ReturnTypeProperty, nameof(MyViewModel.EntryReturnType));

XAML UI

<entryCustomReturn:CustomReturnEntry
    x:Name = "MyCustomReturnEntry"
    HorizontalOptions="Center"
    VerticalOptions="Center"
    ReturnType="{Binding EntryReturnType}"/>

2. Set the ReturnCommand Command

ReturnCommand will fire when the user finalizes the text in an entry with the return key.

Coded UI

goReturnTypeCustomEntry.ReturnCommand = new Command(() => Navigation.PushAsync(new ContentPage()));

XAML UI

Use the Coded UI example above to initialize a Command in the XAML Code Behind

Bindable Property

ReturnCommand can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new CustomReturnEntry();
customReturnEntry.SetBinding(CustomReturnEntry.ReturnCommandProperty nameof(MyViewModel.EntryReturnCommand));

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:SimpleXamlSample"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions"
    BindingContext="{Binding Source={local:MyViewModel}}">

    <ContentPage.Content>

        <entryCustomReturn:CustomReturnEntry
            x:Name = "MyCustomReturnEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            ReturnCommand="{Binding EntryReturnCommand}"/>

    </ContentPage.Content>
</ContentPage>

3. Set the ReturnCommandParameter Property

The ReturnCommandParameter property is an object that can be passed to the ReturnCommand property.

Coded UI

goReturnTypeCustomEntry.ReturnCommand = new Command<string>(async title => await DisplayAlert(title, "", "Ok"));
goReturnTypeCustomEntry.ReturnCommandParameter = "Return Button Tapped";

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions">

    <ContentPage.Content>

        <entryCustomReturn:CustomReturnEntry
            x:Name = "MyCustomReturnEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            ReturnCommandParameter="Return Button Tapped"/>

    </ContentPage.Content>
</ContentPage>

Bindable Property

ReturnCommandParameter can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new CustomReturnEntry();
customReturnEntry.SetBinding(CustomReturnEntry.ReturnCommandParameterProperty, nameof(MyViewModel.EntryReturnCommandParameter));

XAML UI

<entryCustomReturn:CustomReturnEntry
    x:Name = "MyCustomReturnEntry"
    HorizontalOptions="Center"
    VerticalOptions="Center"
    ReturnCommandParameter="{Binding EntryReturnCommandParameter}"/>

Usage in Xamarin.Forms Project as an Effect

This plugin can be consumed as a CustomRenderer Control or as an Effect.

1. Set the ReturnType Property

The ReturnType property is an enum containing 6 different types: Default, Go, Next, Done, Send, Search.

Coded UI

var goReturnTypeEntry = new Entry()
CustomReturnEffect.SetReturnType(goReturnTypeEntry, EntryCustomReturn.Forms.Plugin.Abstractions.ReturnType.Go);

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions">

    <ContentPage.Content>

        <Entry
            x:Name = "GoReturnTypeEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            entryCustomReturn:CustomReturnEffect.ReturnType="{x:Static entryCustomReturn:ReturnType.Default}"/>


    </ContentPage.Content>
</ContentPage>

Bindable Property

ReturnType can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new Entry();
customReturnEntry.SetBinding(CustomReturnEffect.ReturnTypeProperty, nameof(MyViewModel.EntryReturnType));

XAML UI

<Entry
    x:Name = "GoReturnTypeEntry"
    HorizontalOptions="Center"
    VerticalOptions="Center"
    entryCustomReturn:CustomReturnEffect.ReturnType="{Binding EntryReturnType}"/>

2. Set the ReturnCommand Command

ReturnCommand will fire when the user finalizes the text in an entry with the return key.

Coded UI

var goReturnTypeEntry = new Entry()
CustomReturnEffect.SetReturnCommand(goReturnTypeEntry, new Command(() => Navigation.PushAsync(new ContentPage()));

XAML UI

Use the Coded UI example above to initialize a Command in the XAML Code Behind

Bindable Property

ReturnCommand can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new Entry();
customReturnEntry.SetBinding(CustomReturnEffect.ReturnCommandProperty, nameof(MyViewModel.EntryReturnCommand));

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="SimpleXamlSample"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions"
    BindingContext="{Binding Source={local:MyViewModel}}">

    <ContentPage.Content>

        <entryCustomReturn:CustomReturnEntry
            x:Name = "MyCustomReturnEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            entryCustomReturn:CustomReturnEffect.ReturnCommand="{Binding EntryReturnCommand}"/>

    </ContentPage.Content>
</ContentPage>

3. Set the ReturnCommandParameter Property

The ReturnCommandParameter property is an object that can be passed to the ReturnCommand property.

Coded UI

var goReturnTypeEntry = new Entry()
CustomReturnEffect.SetReturnCommand(goReturnTypeEntry, new Command<string>(async title => await DisplayAlert(title, "", "Ok")));
CustomReturnEffect.SetReturnCommandParameter(goReturnTypeEntry, "Return Button Tapped");

XAML UI

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SimpleXamlSample.CustomRendererPage"
    xmlns:entryCustomReturn="clr-namespace:EntryCustomReturn.Forms.Plugin.Abstractions;assembly=EntryCustomReturn.Forms.Plugin.Abstractions">

    <ContentPage.Content>

        <entryCustomReturn:CustomReturnEntry
            x:Name = "MyCustomReturnEntry"
            HorizontalOptions="Center"
            VerticalOptions="Center"
            entryCustomReturn:CustomReturnEffect.ReturnCommandParameter="Return Button Tapped"/>

    </ContentPage.Content>
</ContentPage>

Bindable Property

ReturnCommandParameter can also be used as a Bindable Property to bind to a ViewModel

Coded UI

var viewModel = new MyViewModel();
BindingContext = viewModel;

var customReturnEntry = new Entry();
customReturnEntry.SetBinding(CustomReturnEffect.ReturnCommandParameterProperty, nameof(MyViewModel.EntryReturnCommandParameter));

XAML UI

<entryCustomReturn:CustomReturnEntry
    x:Name = "MyCustomReturnEntry"
    HorizontalOptions="Center"
    VerticalOptions="Center"
    entryCustomReturn:CustomReturnEffect.ReturnCommandParameter="{Binding EntryReturnCommandParameter}"/>

entrycustomreturnplugin's People

Contributors

brminnick avatar colbylwilliams avatar dependabot[bot] avatar robgibbens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

entrycustomreturnplugin's Issues

Can't click into field

One of my iOS users is reporting that they can't edit the text in the custom entry field. They tap on it but no cursor appears and no keyboard pops up.

Any ideas?

Thanks!

Respect CanExecute on commands

When binding to commands, the return button should respect the CanExecute value of the command. If possible, it should change the UI, but at a minimum it should not try to call Execute on the command

Rename command properties

SetReturnCommandProperty and GetReturnCommandProperty on ReturnTypeEffect should be renamed to SetReturnCommand and SetReturnCommand.

Breaking change I know, so maybe add new methods and obsolete the existing ones.

Method 'EntryCustomReturn.Forms.Plugin.Abstractions.CustomReturnEntry.set_ReturnCommandParameter' not found.

https://mobile.azure.com/orgs/XamList/apps/XamList.iOS/crashes/groups/a9c68f9675a6434aad9a691872b355d785145a96

Method 'EntryCustomReturn.Forms.Plugin.Abstractions.CustomReturnEntry.set_ReturnCommandParameter' not found.

1
ContactsListPage+<b__8_0>d.MoveNext () /Users/brandonm/Projects/GitHub/XamList/XamList/Pages/ContactsListPage.cs:112
2
ExceptionDispatchInfo.Throw () /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.14/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
3
AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.14/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018
4
UIKitSynchronizationContext+c__AnonStorey0.<>m__0 () /Users/builder/data/lanes/4991/bf350ba5/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24
5
NSAsyncActionDispatcher.Apply () /Users/builder/data/lanes/4991/bf350ba5/source/xamarin-macios/src/Foundation/NSAction.cs:163
6
(wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
7
UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) /Users/builder/data/lanes/4991/bf350ba5/source/xamarin-macios/src/UIKit/UIApplication.cs:79
8
UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) /Users/builder/data/lanes/4991/bf350ba5/source/xamarin-macios/src/UIKit/UIApplication.cs:63
9
Application.Main (System.String[] args) /Users/brandonm/Projects/GitHub/XamList/iOS/Main.cs:12

Add feature for ReturnKey.Next to move to another Entry

The Next key is great, but it's really awesome if it causes the focus to move to another control. I'm wondering if we can add a feature to this so that when Next is pressed, another control get's Focus.

Something like this:

var goReturnTypeCustomEntry = new CustomReturnEntry
{
    ReturnType = ReturnType.Go
};

var nextReturnTypeCustomEntry = new CustomReturnEntry
{
    ReturnType = ReturnType.Next,
    NextViewProperty = goReturnTypeCustomEntry
};
    public static readonly BindableProperty NextViewProperty = 
        BindableProperty.Create("NextView", typeof(View), typeof(CustomReturnEntry));
 
    public View NextView
    {
        get { return (View)GetValue(NextViewProperty); }
        set { SetValue(NextViewProperty, value); }
    }
 
    public void OnNext()
    {
        NextView?.Focus();
    }

Similar to what this blog is explaining. What are your thoughts?

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.