转换十进制值

时间:2017-11-08 20:34:49

标签: uwp uwp-xaml

我使用UWP平台使用MVVM Light创建了一个简单的XAML表单。到目前为止,我从未遇到数据绑定问题,但在创建了一些绑定到十进制属性的文本框之后,它会在代码生成的视图文件中抛出以下异常。

  

发生了System.AccessViolationException     的HResult = 0x80004003     Message =尝试读取或写入受保护的内存。这通常表明其他内存已损坏。     来源=     堆栈跟踪:   

错误仅发生在案例12中,但不会发生在案例11中.ConvertValue似乎有错。

                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;

这是我的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" />

这是我的观点模型:         private bool _signatureRequiredOnDelivery;         public bool SignatureRequiredOnDelivery         {             get =&gt; _signatureRequiredOnDelivery;             set =&gt;设置(ref _signatureRequiredOnDelivery,value);         }

    private decimal _insuredValue = decimal.Zero;
    public decimal InsuredValue
    {
        get => _insuredValue;
        set => Set(ref _insuredValue, value);
    }

1 个答案:

答案 0 :(得分:0)

它的设计。你可以使用float而不是使用十进制,就像Matt Small在这个帖子WinRT XAML : Binding text to decimal causes

中提到的那样

你也可以创建一个IValueConverter来解决这个问题,比如上面提到的Darrel Miller。