开发者

Exception causing break during debug instead of validation for binding in Silverlight on Windows Phone

开发者 https://www.devze.com 2023-03-22 22:34 出处:网络
I\'m trying to implement bin开发者_如何学Pythonding validation using Silverlight on Windows Phone, but all I get is the debugger breaking on an unhandled exception instead, and the validation not happ

I'm trying to implement bin开发者_如何学Pythonding validation using Silverlight on Windows Phone, but all I get is the debugger breaking on an unhandled exception instead, and the validation not happening.

I'm following the recommendations from an MSDN article on Data Binding.

Here is what I have tried to do to set it up:

Debug -> Exceptions -> Common Language Runtime Exceptions - User-unhandled is unchecked

On the data template for the ListBoxItem, I declare two-way binding with validations:

<TextBox x:Name="LastReadingValue" Text="{Binding LastReadingValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />

In the setter I raise an exception if the TextBox data is incorrect:

public string LastReadingValue
        {
            get
            {
                return _lastReadingValue;
            }
            set
            {
                try
                {
                    double reading = System.Convert.ToDouble(value);
                    _lastReadingValue = String.Format("{0:0.00}", reading);
                }
                catch (FormatException fe)
                {
                    throw new Exception("The meter reading must be a number like 7561.41");
                }
                NotifyOfPropertyChanged("LastReadingValue");
            }
        }

On the ListBox I declare the handler for the BindingValidationError event:

<ListBox x:Name="NewMeterReadingsListBox" ItemsSource="{Binding Meters}" ItemTemplate="{StaticResource NewMeterReadingDataTemplate}" BindingValidationError="ListBox_BindingValidationError"/>

The handler for the BindingValidationError never gets called:

private void ListBox_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            Debug.WriteLine("BindingValidationError");
        }


Ok - 2nd try at an answer.

A couple of interesting things happened when I tried this myself. I started from the WindowsPhoneDataboundApplication then added a TextBox and validation to the app's ListBox. I'm using the WP Mango Beta 2 tools (not the Beta 2 update from yesterday).

  1. When debugging: if an invalid value is entered I get a debug break and prompt from VS on the throw line says that the exception is unhandled. HOWEVER: the BindingValidationError handler is called fine, the application keeps running and Application.UnhandledException isn't called.
  2. In the _BindingValidationError function the Debug.WriteLine text is often difficult to spot with the other exception text in the output window. If you make it more obvious by forcing a new line you should spot the text:

    Debug.WriteLine("\n\n*** BindingValidationError ***\n");

So basically it's working for me, but there are a couple of things to watch out for when debugging.

If you're targetting WP Mango I'd recommend you consider the Silverlight 4 way of doing data validation which doesn't involve throwing and catching exceptions - I agree with Claus insofar as using exceptions is a really ugly way of doing validation. The SL4 alternative involves implementing the IDataErrorInfo and INotifyDataErrorInfo interfaces in your bound class. I haven't tried this approach personally myself though.

Edit:

If you really want to turn off the break on exceptions you can do it from the VS2010 "Debug" menu, "Exceptions" item. Then de-select the exception type you don't want VS to do break on if it is unhandled.

If you create your own custom Exception class just for binding validation and disable the debug break only for that Exception type, then you will be able to have normal VS behaviour for your app's exceptions except when when dealing with binding failures.

0

精彩评论

暂无评论...
验证码 换一张
取 消