Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms adding Effect to all Entries in XAML

Tags:

I'm using the FormsCommunityToolkit NuGet Package to make it so all of my Entries in my Xamarin.Forms app select all of their text when a user clicks on them. The example on their GitHub for using this effect on an Entry in XAML is this:

<Entry Placeholder="focus this entry." VerticalOptions="Start" Text = "FOCUS THIS!">
  <Entry.Effects>
    <effects:SelectAllTextEntryEffect />
  </Entry.Effects>
</Entry>

This works if you put it on each individual Entry in your code, but I have a lot of Entries and would like to set it as the default in my App.xaml folder. I tried this:

<Style TargetType="Entry">
    <Setter Property="Keyboard" Value="Text"/> <!--Defaults to capitalize first word-->
    <Setter Property="Effects" Value="effects:SelectAllTextEntryEffect" />
</Style>

This method works for setting the default Keyboard for all entries, but setting the Effects in this way crashes the app with this error:

Can't resolve EffectsProperty on Entry

Does anybody know a way of doing this so I don't need to add the code to all of my Entries?

like image 271
cvanbeek Avatar asked Mar 24 '17 17:03

cvanbeek


People also ask

What is TableView in Xamarin forms?

TableView is a view for displaying scrollable lists of data or choices where there are rows that don't share the same template. Unlike ListView, TableView does not have the concept of an ItemsSource , so items must be manually added as children.

What is trigger in Xamarin forms?

Triggers allow you to express actions declaratively in XAML that change the appearance of controls based on events or property changes. In addition, state triggers, which are a specialized group of triggers, define when a VisualState should be applied.

What is effect in Xamarin forms?

Effects simplify this process, allowing the native controls on each platform to be more easily customized. Effects are created in platform-specific projects by subclassing the PlatformEffect control, and then the effects are consumed by attaching them to an appropriate control in a Xamarin. Forms .

What is custom renderer in Xamarin forms?

Custom renderers for a given type can be added to one application project to customize the control in one place while allowing the default behavior on other platforms; or different custom renderers can be added to each application project to create a different look and feel on iOS, Android, and the Universal Windows ...


1 Answers

Why don't you create your own Entry which has the Effect?

So create an inheritance of the Entry, I will call it EffectEntry. You can do this by creating a new XAML file and put the contents of your Entry in there. Yo probably want to remove the properties like PlaceHolder and Text, but if there is a property that you would like on all your entries as a default, apply them here.

<Entry>
  <Entry.Effects>
    <effects:SelectAllTextEntryEffect />
  </Entry.Effects>
</Entry>

Go to the code-behind and make sure your EffectEntry inherits the Entry.

namespace MyApp.Controls
{
    public partial class EffectEntry : Button
    {
        public EffectEntry ()
        {
            InitializeComponent ();
        }
    }
}

Now, in the rest of your app you can just use your Entry with the effect already added like this:

<controls:EffectEntry Placeholder="focus this entry." VerticalOptions="Start" Text = "FOCUS THIS!" />

Note that controls is a namespace that you have to add yourself. The name could be different to. In context with a whole page it could look like this:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:MyApp"
    xmlns:controls="clr-namespace:MyApp.Controls" 
    x:Class="MyApp.MyAppPage">

    <controls:EffectEntry Placeholder="focus this entry." VerticalOptions="Start" Text = "FOCUS THIS!" />
</ContentPage>

Notice how there are multiple xmlns (XML Namespace) entries at the top. I have added the controls one, you could also name it any other way you like. When you look at the code-behind of our EffectEntry, you will see that the namespace there and here match. This way the app knows where to find the control. If you decide that you want to move the controls to their own assembly, you can also define this like xmlns:controls="clr-namespace:MyApp.Controls;assembly=MyProject.Example".

like image 161
Gerald Versluis Avatar answered Sep 22 '22 10:09

Gerald Versluis