简体   繁体   中英

Xamarin.Forms adding Effect to all Entries in XAML

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?

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM