Bug 20931 - Xamarin.Forms Binding Error
Summary: Xamarin.Forms Binding Error
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.1.1
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-26 19:53 UTC by Jon Goldberger [MSFT]
Modified: 2014-07-09 11:27 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Comment 1 Jon Goldberger [MSFT] 2014-06-26 19:55:09 UTC
From case file:

I am getting the following Exception when trying to bind an Entry text field to my view model.

06-25 20:44:17.437 E/mono ( 4081): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
06-25 20:44:17.437 E/mono ( 4081): at Xamarin.Forms.Forms+AndroidPlatformServices.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
06-25 20:44:17.437 E/mono ( 4081): at Xamarin.Forms.Device.BeginInvokeOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
06-25 20:44:17.437 E/mono ( 4081): at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in <filename unknown>:0

This same view model property can be bound to the "Placeholder" field with no problems and to a Label elements Text field with no problems.

After some additional testing it was discovered that the setter for SayItText was being called while the form/xaml was being initialized and when the RaisePropertyChanged method is called the exception is being thrown from whoever is the listener. The second image shows the "SayItText" method modified to fix the error. To replicate remove the "if-check" and always set the _sayItText and call the RaisePropertyChanged method.

The Xamarin.Form class that is listening to the PropertyChanged event has a bug that is not checking for null.

The exception is being thrown here when the PropertyChanged is called:

protected void RaisePropertyChanged([CallerMemberName] string propertyName= "")
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Comment 2 Jon Goldberger [MSFT] 2014-06-26 20:07:14 UTC
In the test project, to reproduce the error, comment out the IF condition in FirstViewModel.SayItText property as indicated below:

public String SayItText
        {
            get { return _sayItText; }
            set
            {
                //if (string.Compare(value, _sayItText, StringComparison.OrdinalIgnoreCase) != 0){
                    _sayItText = value;
                    RaisePropertyChanged();
                //}
            }
        }

Then run the app and the error in Comment 1 will appear. I tested this on VS 2013 Update 2 with the following version information:

Xamarin.Forms 1.1.1.6206

Microsoft Visual Studio Ultimate 2013
Version 12.0.30501.00 Update 2
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Ultimate

Architecture and Modeling Tools   06181-004-0451031-02209
Microsoft Architecture and Modeling Tools
    
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

LightSwitch for Visual Studio 2013   06181-004-0451031-02209
Microsoft LightSwitch for Visual Studio 2013

Office Developer Tools - May 2014 Update ENU   06181-004-0451031-02209
Microsoft Office Developer Tools for Visual Studio 2013 - May 2014 Update ENU

Team Explorer for Visual Studio 2013   06181-004-0451031-02209
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06181-004-0451031-02209
Microsoft Visual Basic 2013

Visual C# 2013   06181-004-0451031-02209
Microsoft Visual C# 2013

Visual C++ 2013   06181-004-0451031-02209
Microsoft Visual C++ 2013

Visual F# 2013   06181-004-0451031-02209
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06181-004-0451031-02209
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06181-004-0451031-02209
Windows Phone SDK 8.0 - ENU

ASP.NET and Web Tools   2013.2.50425
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Windows Azure Web Sites
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Windows Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.1.20409.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.1
Provides common services for use by Azure Mobile Services and Windows Azure Tools.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone
Build 

NuGet Package Manager   2.8.50313.46
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SAPReferenceManager   1.0
Manages References to Shared Projects

SQL Server Data Tools   12.0.40403.0
Microsoft SQL Server Data Tools

Windows Azure Mobile Services Tools   1.1
Windows Azure Mobile Services Tools

Windows Azure Tools   2.3
Windows Azure Tools for Microsoft Visual Studio 2013 - v2.3.20320.1602

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.1.204.0 (e9b5840803bbb501c8109c97e94a9bd5e64c7bce)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android
Comment 3 Eric Maupin 2014-07-09 11:27:49 UTC
Fixed for next update.