2015-12-14 81 views
3

我有我的XAML在設計時的錯誤:WPF轉換器拋出對象未設置在設計時

對象不設置到對象的實例。

它只發生在設計時間,並在運行時間內完美運行。該錯誤來自我的轉換器,下面的代碼。我認爲這可能是由於沒有檢查值是否爲null,或者如果返回null(如果它爲null),但是我改變了它們並沒有區別。

想法被讚賞。由於

public class CouponDataSplitterConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter,  CultureInfo culture) 
    { 
     if (value != null) 
     { 
      List<SelectionArea> selectionAreasList = 
       new List<SelectionArea>((ObservableCollection<SelectionArea>) value); 
      foreach (var area in selectionAreasList) 
      { 
       if (area.AreaName.Contains(parameter.ToString())) 
       { 
        return area.SelectionRows; 
       } 
      } 

      return selectionAreasList; 
     } 
     return ""; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML:

ItemsSource="{Binding Coupon.SelectionAreas, 
     ConverterParameter='Test Parameter', 
     Converter={StaticResource CouponDataSplitterConverter} 

堆棧跟蹤:如果沒有初始化

at App.UI.Converters.CouponDataSplitterConverter.Convert(Object  value, Type targetType, Object parameter, CultureInfo culture) 
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) 
at System.Windows.Data.BindingExpression.Activate(Object item) 
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) 
at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp) 
at System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp) 
at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry) 
at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) 
at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) 
at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry) 
at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot) 
at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot) 
at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject) 
at System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope) 
at System.Windows.FrameworkTemplate.<>c__DisplayClass0.<LoadOptimizedTemplateContent>b__5(Object sender, XamlObjectEventArgs args) 
at System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value) 
at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx) 
at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property) 
at System.Xaml.XamlWriter.WriteNode(XamlReader reader) 
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) 
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) 
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren) 
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) 
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) 
at System.Windows.FrameworkElement.ApplyTemplate() 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.ContextLayoutManager.UpdateLayout() 
at System.Windows.UIElement.UpdateLayout() 
+0

你有沒有指示任何有用的堆棧跟蹤? –

+2

(這也不清楚爲什麼你要創建一個新的'List '而不是迭代現有的集合...) –

+0

我已經添加了堆棧跟蹤。這也是一個好點,我不知道爲什麼我決定使用一個列表,但肯定不會有什麼區別嗎? – Spitfire5793

回答

1

如果你得到一個的NullReferenceException那麼什麼是空的。

您的代碼明確檢查value參數爲空。但是,你有這行代碼在你foreach

if (area.AreaName.Contains(parameter.ToString())) 

這條線可以拋出的NullReferenceException如果任一AreaName酒店有一個空值(Contains方法調用將拋出),或提供的parameter參數該方法爲null(將調用ToString方法調用)。

在你的方法中加入一些調試代碼可能是值得的,所以你可以檢查你正在使用的變量的狀態。我知道你不能使用調試器,因爲這是一個設計時問題,所以你可能必須登錄到臨時文本文件或類似的東西。

+0

這樣做,檢查areaName和參數是否爲空讓它再次工作。似乎很簡單,但我看不到它。非常感謝! – Spitfire5793

1

這是不容易分辨什麼是被設置爲null,值可能是最初爲null在視圖模型構造函數(在這種情況下我假設MarkSenseCoupon)。有關如何在設計時調試控件的步驟,請參閱Debugging Your Custom Control at Design Time,並簡單地在轉換方法的開始處放置一個斷點,以確定哪些對象設置爲null。

這太問題似乎也解決了類似的問題WPF Converter casting causes Visual Studio designer exception ...

相關問題