Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Passing a View as a parameter in XAML returns null

albertoha94albertoha94 Member ✭✭✭

So, I intend to create an entry where i can define the returnType of the Entry with a Bindable property and when set, set the command automatically.
For this I added 2 bindable properties KeyboardButtonAction & NextActionParameter.
The most important is the second as thats where I pass the Entry view i will focus next. For some reason, I get it null even though inside the XAML I have NextActionParameter set, am I passing the argument wrong?

Code of the Entry:

internal class ImprovedEntry : Entry
{

        ...

        #region BindableProperty: NextActionParameter.

        internal static readonly BindableProperty NextActionParameterProperty = BindableProperty.Create(nameof(NextActionParameter), typeof(View), typeof(ImprovedEntry));

        public View NextActionParameter
        {
            get { return (View)GetValue(NextActionParameterProperty); }
            set { SetValue(NextActionParameterProperty, value); }
        }

        #endregion

        ...

        protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);

            if (propertyName == nameof(KeyboardButtonAction))
            {
                switch (KeyboardButtonAction)
                {
                    case KeyboardButtonActionOptions.Done:
                        ReturnType = ReturnType.Done;
                        break;
                    case KeyboardButtonActionOptions.Next:
                        ReturnType = ReturnType.Next;
                        ReturnCommand = new Command(() =>
                        {
                            var x = NextActionParameter;
                            // x.Focus();
                        });
                        break;
                }
            }
        }

}

Inside the XAML:

<views:ImprovedEntry
                    x:Name="entryTitle"
                    BindingContext="{x:Reference entryComment}"
                    KeyboardButtonAction="Next"
                    NextActionParameter="{Binding .}"
                    Text="{Binding TitleCombo}"
                    TextChanged="Handle_TextChanged" />

...

<Entry
                    x:Name="entryComment"
                    d:Text="Item comment"
                    Text="{Binding Comment}" />

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    if (propertyName == nameof(KeyboardButtonAction))
    {
    switch (KeyboardButtonAction)
    {
    case KeyboardButtonActionOptions.Done:
    ReturnType = ReturnType.Done;
    break;
    case KeyboardButtonActionOptions.Next:
    ReturnType = ReturnType.Next;
    ReturnCommand = new Command(() =>
    {
    var x = NextActionParameter;
    // x.Focus();
    });
    break;
    }
    }

    What's the code of KeyboardButtonAction? Could you please post more code snippet or a basic demo so that we can help you better?

  • albertoha94albertoha94 Member ✭✭✭

    @jezh Thanks for the reply. Here's the snippet requested:

    internal enum KeyboardButtonActionOptions { Next, Done };
    
            #region BindableProperty: KeyboardButtonAction.
    
            internal static readonly BindableProperty KeyboardButtonActionProperty = BindableProperty.Create(nameof(KeyboardButtonAction), typeof(KeyboardButtonActionOptions), typeof(ImprovedEntry), defaultValue: KeyboardButtonActionOptions.Done);
    
            public KeyboardButtonActionOptions KeyboardButtonAction
            {
                get { return (KeyboardButtonActionOptions)GetValue(KeyboardButtonActionProperty); }
                set { SetValue(KeyboardButtonActionProperty, value); }
            }
    
            #endregion
    
Sign In or Register to comment.