We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SfDataForm throws NullReferenceException on UWP

Hello,
I'm trying to build a login form using the SfDataForm control. On Android platform it works perfectly, but when I run it on UWP I got the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Syncfusion.UWP.DataForm.Editors.DataFormPasswordEditor.OnViewLoaded(Object sender, RoutedEventArgs e)


These are the relevant part of my code.
My page:

<ContentPage mlns:dataform="clr-namespace:Syncfusion.XForms.DataForm;assembly=Syncfusion.SfDataForm.XForms" >
     <dataform:SfDataForm x:Name="dataForm"
                                     Grid.Row="1"
                                     Margin="0,30,0,0"
                                     CommitMode="Explicit"
                                     DataObject="{Binding Login}"
                                     IsReadOnly="{Binding IsBusy}"
                                     LabelPosition="Left"
                                     ValidationMode="Explicit">
                    <dataform:SfDataForm.Behaviors>
                        <behaviors:SfDataFormLoginBehavior />
                    </dataform:SfDataForm.Behaviors>
                </dataform:SfDataForm>
</ContentPage>

My model:
[Display(Name = "StringEmail", Prompt = "EnterEmailAddress", ResourceType = typeof(Resx.AppResources))]
        [EmailAddress(ErrorMessageResourceName = "EmailErrorMessage", ErrorMessageResourceType = typeof(Resx.AppResources))]
        public string Email
        {
            get { return email; }
            set
            {
                email = value;
                RaisePropertyChanged(nameof(Email));
                RaiseErrorChanged(nameof(Email));
            }
        }

        [Display(Name = "StringPassword", Prompt = "EnterPassword", ResourceType = typeof(Resx.AppResources))]
        [Required(AllowEmptyStrings = false, ErrorMessage = "", ErrorMessageResourceName = "PasswordErrorMessage", ErrorMessageResourceType = typeof(Resx.AppResources))]
        [DataType(DataType.Password)]
        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged(nameof(Password));
                RaiseErrorChanged(nameof(Password));
            }
        }


The behavior is taken from the demo code download when installing the framework.
Syncfusion nuget packages version is 16.3.0.29

Can you please help me?

4 Replies

JN Jayaleshwari N Syncfusion Team November 16, 2018 06:49 AM UTC

Hi Matteo, 
 
Thanks for using Syncfusion Products. 
 
We have checked the reported query “SfDataForm throws NullReferenceException on UWP” from our side. Unfortunately the reported issue does not reproduced at our end. We have prepared the sample based on the provided code snippet which is working fine as expected while loading. 
 
We have attached the tested sample(16.3.0.29) for your reference and you can download the same from the following location. 
 
 
Could you please check with the above sample whether the reported issue reproduced in our sample also ? if not , we would request you to modify the attached sample or could you please share the following details which would be better to provide appropriate solution. 
 
  • Replictaion procedure to reproduce the issue.
  • SfDataFormloginBehvaior used in your application
 
Regards, 
Jayaleshwari N. 



MC Matteo Comi November 16, 2018 08:41 AM UTC

Hi Jayaleshwari,
thanks for your support. I tried your example and it is working fine.
I saw from the code you don't initialize SfDataForm nowhere in the project. Is it correct? You don't even include the assemblies in Xamarin Forms initialization.

Here's my behavior:

public class SfDataFormLoginBehavior : Behavior<SfDataForm>
    {
        private SfDataForm dataForm;

        protected override void OnAttachedTo(SfDataForm bindable)
        {
            base.OnAttachedTo(bindable);

            dataForm = bindable;
            dataForm.AutoGeneratingDataFormItem += OnAutoGeneratingDataFormItem;
            dataForm.BindingContextChanged += OnBindingContextChanged;

            dataForm.ValidationMode = ValidationMode.LostFocus;
            dataForm.LayoutManager = new DataFormLayoutManagerExt(dataForm);
        }

        private void OnBindingContextChanged(object sender, EventArgs e)
        {
            (dataForm.DataObject as INotifyPropertyChanged).PropertyChanged += OnPropertyChanged;
        }

        private void OnAutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
        {
            if (e.DataFormItem != null)
            {
                if (e.DataFormItem.Name.Equals("HasErrors"))
                    e.Cancel = true;
                else if (e.DataFormItem.Name.Equals("Email"))
                    (e.DataFormItem as DataFormTextItem).KeyBoard = Keyboard.Email;

                if (e.DataFormItem.Name == "Email")
                {
                    if (Device.RuntimePlatform == Device.UWP)
                        e.DataFormItem.ImageSource = ImageSource.FromFile("Images\\icon_input_mail.png");
                    else
                        e.DataFormItem.ImageSource = ImageSource.FromFile("icon_input_mail.png");
                }

                if (e.DataFormItem.Name == "Password")
                {
                    if (Device.RuntimePlatform == Device.UWP)
                        e.DataFormItem.ImageSource = ImageSource.FromFile("Images\\icon_input_password.png");
                    else
                        e.DataFormItem.ImageSource = ImageSource.FromFile("icon_input_password.png");
                }

                if (e.DataFormItem.Name == "DeviceId" || e.DataFormItem.Name == "DeviceName")
                    e.DataFormItem.IsVisible = false;
            }
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        protected override void OnDetachingFrom(SfDataForm bindable)
        {
            base.OnDetachingFrom(bindable);
            dataForm.AutoGeneratingDataFormItem -= OnAutoGeneratingDataFormItem;
            (dataForm.DataObject as INotifyPropertyChanged).PropertyChanged -= OnPropertyChanged;
            dataForm.BindingContextChanged -= OnBindingContextChanged;
            dataForm = null;
        }
    }

    public class DataFormLayoutManagerExt : DataFormLayoutManager
    {
        public DataFormLayoutManagerExt(SfDataForm dataForm) : base(dataForm)
        {

        }

        protected override View GenerateViewForLabel(DataFormItem dataFormItem)
        {
            var label = base.GenerateViewForLabel(dataFormItem);

            if (label is Label)
                (label as Label).TextColor = Color.White;

            return label;
        }

        protected override void OnEditorCreated(DataFormItem dataFormItem, View editor)
        {
            if (editor is Entry)
            {
                (editor as Entry).TextColor = Color.White;
                (editor as Entry).PlaceholderColor = FunctionsHelper.GetColorFromResource("PlaceholderColor");
            }
        }
    }

Thanks in advance,
Matteo


JN Jayaleshwari N Syncfusion Team November 16, 2018 12:07 PM UTC

Hi Matteo, 
 
Thanks for the update. 
 
We have checked the reported crash with code snippet you have provided. We are able to reproduce the reported issue when customize the PlaceHolderText. The reported issue has been fixed in our source and the fix will be included in our 2018 Volume 4 main release which is scheduled to be rolled out by the middle of the month December, 2018. We appreciate your patience until then. 
 
Regards, 
Jayaleshwari N 



JN Jayaleshwari N Syncfusion Team December 11, 2018 07:23 AM UTC

Hi Dave,   
   
We are glad to announce that our Essential Studio 2018 Volume 4 beta Release v16.4.0.40 is rolled out and is available for download under the following link.    
    
     
The reported issue “The NullReferenceException will be thrown when customizing the placeholder text” has been resolved and included in this release .We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance.    
    
Regards,    
Jayaleshwari N. 


Loader.
Live Chat Icon For mobile
Up arrow icon