Xamarin.Forms add effect to all records in XAML

I am using the FormsCommunityToolkit NuGet package so that all my entries in a Xamarin.Forms app display all the text when the user clicks on them. Their GitHub example for using this effect for Entry

in XAML is as follows:

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

      

This works if you put it in every single one Entry

in your code, but I have a lot of entries and want to set it as the default in my folder App.xaml

. I've 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 to set the default Keyboard

for all entries, but setting the effects this way crashes the application with this error:

Can't resolve EffectsProperty on Entry

      

Does anyone know a way to do this so I don't have to add code to all of my posts?

+3


source to share


1 answer


Why don't you create your own Entry

with an effect?

So let's create inheritance Entry

, I'll name it EffectEntry

. You can do this by creating a new XAML file and placing your content there Entry

. You probably want to remove properties like PlaceHolder

and Text

, but if there is a property that you would like to use in all of your default entries, apply them here.

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

      

Go to encoding and make sure yours EffectEntry

inherits Entry

.

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

      



Now, in the rest of your application, you can simply use yours Entry

with the effect already added:

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

      

Please note that controls

is is a namespace that you have to add yourself. The name may vary. In context with the entire page, it might 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>

      

Note that there are several entries xmlns

(XML Namespace) at the top . I added controls one, you can also name it any other way. When you look at our code EffectEntry

, you will see that the namespace matches here and there. This way the application knows where to find the control. If you decide you want to move the controls into their own assembly, you can also define that as xmlns:controls="clr-namespace:MyApp.Controls;assembly=MyProject.Example"

.

+2


source







All Articles