UWP XamlBindingHelper.ConvertValue throws an AccessViolationException when converting Decimal Values

0

I created a simple XAML form using MVVM Light using UWP Platform. Up until now, I've never had an issue with databinding, but after creating a few textboxes bound to decimal properties, it throws the following exception in the code generated view file.

System.AccessViolationException occurred HResult=0x80004003 Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source= StackTrace:

The error only occurs in case 12, but not in case 11. The ConvertValue appears to be at fault.

                case 11: // Views\QuotesPage.xaml line 82
                    this.obj11 = (global::Windows.UI.Xaml.Controls.ToggleSwitch)target;
                    (this.obj11).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.ToggleSwitch.IsOnProperty,
                        (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
                        {
                        if (this.initialized)
                        {
                            // Update Two Way binding
                            this.dataRoot.ViewModel.SignatureRequiredOnDelivery = this.obj11.IsOn;
                        }
                    });
                    break;
                case 12: // Views\QuotesPage.xaml line 85
                    this.obj12 = (global::Windows.UI.Xaml.Controls.TextBox)target;
                    (this.obj12).LostFocus += (global::System.Object sender, global::Windows.UI.Xaml.RoutedEventArgs e) =>
                    {
                        if (this.initialized)
                        {
                            // Update Two Way binding
                            this.dataRoot.ViewModel.InsuredValue = (global::System.Decimal) global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof(global::System.Decimal), this.obj12.Text);
                        }
                    };
                    break;

Here's my block of XAML:

                <TextBlock Text="Weight" Grid.Row="1" Grid.Column="2" />
            <TextBox Text="{x:Bind Mode=TwoWay, Path=ViewModel.Weight}" Grid.Column="3" Grid.Row="1" x:Name="weight" />

            <TextBlock Text="Signature Required On Delivery" Grid.Row="2" Grid.Column="0" />
            <ToggleSwitch IsOn="{x:Bind Mode=TwoWay, Path=ViewModel.SignatureRequiredOnDelivery}" Grid.Column="1" Grid.Row="2" x:Name="signatureRequiredOnDelivery" />

            <TextBlock Text="Insured Value" Grid.Row="3" Grid.Column="0" />
            <TextBox Text="{x:Bind Mode=TwoWay, Path=ViewModel.InsuredValue}" Grid.Column="1" Grid.Row="3" x:Name="insuredValue" />

            <TextBlock Text="Is Oversize" Grid.Row="4" Grid.Column="0" />
            <ToggleSwitch IsOn="{x:Bind Mode=TwoWay, Path=ViewModel.IsOversize}" Grid.Column="1" Grid.Row="4" x:Name="isOversize" Height="40" Width="154" />

Here's my view model: private bool _signatureRequiredOnDelivery; public bool SignatureRequiredOnDelivery { get => _signatureRequiredOnDelivery; set => Set(ref _signatureRequiredOnDelivery, value); }

    private decimal _insuredValue = decimal.Zero;
    public decimal InsuredValue
    {
        get => _insuredValue;
        set => Set(ref _insuredValue, value);
    }
uwp
uwp-xaml
asked on Stack Overflow Nov 8, 2017 by ATL_DEV

1 Answer

0

It's by design. You could use float instead of using decimal as Matt Small has mentioned in this thread WinRT XAML : Binding text to decimal causes

You also could create a IValueConverter to solve this issue like Darrel Miller mentioned in above thread.

answered on Stack Overflow Nov 9, 2017 by Xie Steven

User contributions licensed under CC BY-SA 3.0