【问题标题】:PropertyChangedEventArgs FormatExceptionPropertyChangedEventArgs 格式异常
【发布时间】:2013-03-07 18:25:02
【问题描述】:

我有下一个 XAML:

<TextBlock Text="{Binding Path=Parameter.LocalizedName}"/>

我的视图模型中的这段代码:

public FilterParameter Parameter
{
  get
  {
    return this._Parameter;
  }
  set
  {
    this._Parameter = value;
    base.RaisePropertyChanged("Parameter");
  }
}

我愿意

this.Parameter = newParameter

我为什么会得到

FormatException(输入字符串的格式不正确。)

拨打PropertyChangedEventHandler之后?

http://oi45.tinypic.com/11trceg.jpg


内部异常为空。 堆栈跟踪看起来像

mscorlib.dll!System.Number.StringToNumber(string str, System.Globalization.NumberStyles options, ref System.Number.NumberBuffer number, System.Globalization.NumberFormatInfo info, bool parseDecimal) + 0xf7 bytes 
mscorlib.dll!System.Number.ParseInt32(string s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) + 0x92 bytes   
mscorlib.dll!string.System.IConvertible.ToInt32(System.IFormatProvider provider) + 0x2f bytes   
mscorlib.dll!System.Convert.ChangeType(object value, System.Type conversionType, System.IFormatProvider provider) + 0x1ff bytes 
PresentationFramework.dll!MS.Internal.Data.SystemConvertConverter.Convert(object o, System.Type type, object parameter, System.Globalization.CultureInfo culture) + 0x11 bytes  
PresentationFramework.dll!MS.Internal.Data.DynamicValueConverter.Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) + 0x51 bytes 
PresentationFramework.dll!System.Windows.Controls.Primitives.Selector.VerifyEqual(object knownValue, System.Type knownType, object itemValue, MS.Internal.Data.DynamicValueConverter converter) + 0x64 bytes    
PresentationFramework.dll!System.Windows.Controls.Primitives.Selector.FindItemWithValue(object value, out int index) + 0x1f7 bytes  
PresentationFramework.dll!System.Windows.Controls.Primitives.Selector.SelectItemWithValue(object value) + 0x3e bytes    
PresentationFramework.dll!System.Windows.Controls.Primitives.Selector.CoerceSelectedValue(System.Windows.DependencyObject d, object value) + 0x91 bytes 
WindowsBase.dll!System.Windows.DependencyObject.ProcessCoerceValue(System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, ref System.Windows.EntryIndex entryIndex, ref int targetIndex, ref System.Windows.EffectiveValueEntry newEntry, ref System.Windows.EffectiveValueEntry oldEntry, ref object oldValue, object baseValue, object controlValue, System.Windows.CoerceValueCallback coerceValueCallback, bool coerceWithDeferredReference, bool coerceWithCurrentValue, bool skipBaseValueChecks) + 0x55 bytes   
WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) + 0x247 bytes   
WindowsBase.dll!System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty dp, bool preserveCurrentValue) + 0xd1 bytes    
PresentationFramework.dll!System.Windows.StyleHelper.InvalidateDependents(System.Windows.Style ownerStyle, System.Windows.FrameworkTemplate frameworkTemplate, System.Windows.DependencyObject container, System.Windows.DependencyProperty dp, ref MS.Utility.FrugalStructList<System.Windows.ChildPropertyDependent> dependents, bool invalidateOnlyContainer) + 0x79 bytes   
PresentationFramework.dll!System.Windows.StyleHelper.OnBindingValueInTemplateChanged(object sender, MS.Internal.Data.BindingValueChangedEventArgs e) + 0xe2 bytes   
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.ChangeValue(object newValue, bool notify) + 0x94 bytes  
PresentationFramework.dll!System.Windows.Data.BindingExpression.TransferValue(object newValue, bool isASubPropertyChange) + 0x40c bytes 
PresentationFramework.dll!System.Windows.Data.BindingExpression.ScheduleTransfer(bool isASubPropertyChange) + 0x3a bytes    
PresentationFramework.dll!MS.Internal.Data.ClrBindingWorker.NewValueAvailable(bool dependencySourcesChanged, bool initialValue, bool isASubPropertyChange) + 0x60 bytes 
PresentationFramework.dll!MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(int k, System.ComponentModel.ICollectionView collectionView, object newValue, bool isASubPropertyChange) + 0x211 bytes 
PresentationFramework.dll!MS.Internal.Data.ClrBindingWorker.OnSourcePropertyChanged(object o, string propName) + 0x85 bytes 
PresentationFramework.dll!MS.Internal.Data.PropertyPathWorker.OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + 0x68 bytes   
WindowsBase.dll!System.Windows.WeakEventManager.ListenerList<System.ComponentModel.PropertyChangedEventArgs>.DeliverEvent(object sender, System.EventArgs e, System.Type managerType) + 0x12c bytes 
WindowsBase.dll!System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args) + 0x40c bytes   
>   MyApp.Framework.Core.dll!MyApp.Framework.Core.NotifyObject.RaisePropertyChanged(string propertyName) Line 45 + 0x32 bytes   C# 

【问题讨论】:

  • 查看内部异常。它一定是别的东西。
  • FilterParameter.LocalizeName 是什么样的?
  • 堆栈跟踪是什么?也许附加到 PropertyChanged 事件的东西会抛出该异常
  • @Roman -- 没有足够的信息来解决这个问题 -- 我们需要查看内部异常和堆栈跟踪。

标签: c# .net xaml formatexception


【解决方案1】:

WPF 正在尝试将 System.String 转换为 System.Int32,但失败并出现 FormatException。

您应该将自定义 IValueConverter 设置为您的 Binding 以处理这种情况,例如,如果字符串无法解析为 int,则返回一些默认 int 值。

【讨论】:

  • 嗯,但为什么 WPF 这样做(试图转换)?调用 mscorlib.dll!System.Number.StringToNumber 的原因是什么?我只想用字符串参数调用 RaisePropertyChanged。
  • WPF 将 PropertyChangedEventHandler 附加到 PropertyChanged 事件,以便它可以在您的属性更改时更新 UI。您可能将此属性(或其子属性)绑定到除 TextBlock 之外的某个其他控件(使用 Int32 DependencyProperty)。在 Google 或 StackOverflow 中搜索“调试和跟踪 WPF 绑定”。 WPF 跟踪基础结构将在 Visual Studio 调试窗口中为您提供比此异常堆栈跟踪更多的信息(它将为您提供有关控制哪些数据绑定失败的信息)。
猜你喜欢
  • 2015-01-18
  • 1970-01-01
  • 2014-07-11
  • 2021-11-11
  • 2013-08-11
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多