user666
user666

Reputation: 332

How to get another View in the Behavior?

Can I use a Behavior to get another View? let me explain

public class EditorBehavior : Behavior<Editor>
    {
        public Editor EditorMessage { get; set; }
}

<Entry >
<Entry.Behavior>
<local:EditorBehavior EditorMessage=""/>
</Entry.Behavior>
</Entry>

<Editor x:Name="EditorMessage"/>

How to use the Editor in other view behavior?

Upvotes: 0

Views: 55

Answers (1)

FreakyAli
FreakyAli

Reputation: 16547

That is fairly easy you will have to get it using a BindableProperty:

 public static readonly BindableProperty BindedViewProperty = BindableProperty.Create(
                nameof(BindedView),
                typeof(Xamarin.Forms.View),
                typeof(EditorBehavior));

    public Xamarin.Forms.View BindedView
    {
        get => (Xamarin.Forms.View)GetValue(BindedViewProperty);
        set => SetValue(BindedViewProperty, value);
    }

Once you do that you can just pass your View using BindableProperty or x:Name

<local:EditorBehavior BindedView="{x:Reference EditorMessage}"/>

But make sure you understand the Behavior lifecycle before using this, Also it would make more sense if you do not share the View itself but the concerned property so you actually do not make changes in the view from somewhere else.

Upvotes: 1

Related Questions