将ColorItem添加到列表时发生灾难性故障

时间:2013-10-16 18:57:12

标签: c# .net xaml windows-phone-8 telerik

我正在尝试构建一个选择器,让用户从某些颜色中进行选择。

我正在使用RadListPicker组件,代码如下:

<telerikInput:RadListPicker IsEnabled="True" Grid.Row="4" Header="Tile color: " InlineModeThreshold="3" x:Name="listPicker2" ItemsSource="{Binding Items, Source={StaticResource ColorItems}}" SelectedItem="{Binding TileColor, Mode=TwoWay}">
    <telerikInput:RadListPicker.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <Rectangle Fill="{Binding ColorName}" Width="24" Height="24"/>
                 <TextBlock Text="{Binding ColorName}" Margin="12 -3 0 0"/>
             </StackPanel>
         </DataTemplate>
    </telerikInput:RadListPicker.ItemTemplate>
</telerikInput:RadListPicker>

其中ColorItems是这个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Microsoft.Phone.Net.NetworkInformation;

namespace Utils
{
    public class ColorItems
    {            
        private static readonly ColorItem[] _colorItems =
        {                                 
            new ColorItem(GetFromRGB(240, 163, 10), "Amber"),
            new ColorItem(GetFromRGB(130, 90, 44), "Brown"),
            //new ColorItem(GetFromRGB(162, 0, 37), "Crimson"),
            //new ColorItem(GetFromRGB(0, 80, 239), "Cobalt"),
            new ColorItem(GetFromRGB(27, 161, 226), "Cyan"),
            //new ColorItem(GetFromRGB(0, 138, 0),"Emerald"),
            new ColorItem(GetFromRGB(96, 169, 23),"Green"),
            new ColorItem(GetFromRGB(106, 0, 255),"Indigo"),
            new ColorItem(GetFromRGB(164, 196, 0),"Lime"),
            new ColorItem(GetFromRGB(216, 0, 115),"Magenta"),
            //new ColorItem(GetFromRGB(118, 96, 138),"Mauve"),
            new ColorItem(GetFromRGB(109, 135, 100),"Olive"),
            new ColorItem(GetFromRGB(250, 104, 0),"Orange"),
            new ColorItem(GetFromRGB(244, 114, 208),"Pink"),
            new ColorItem(GetFromRGB(229, 20, 0),"Red"),
            //new ColorItem(GetFromRGB(100, 118, 135),"Steel"),
            //new ColorItem(GetFromRGB(135, 121, 78),"Taupe"),
            new ColorItem(GetFromRGB(0, 171, 169),"Teal"),
            new ColorItem(GetFromRGB(170, 0, 255),"Violet"),
            new ColorItem(GetFromRGB(227, 200, 0),"Yellow"),
        };

        private static Color GetFromRGB(byte r, byte g, byte b)
        {
            return new Color {R = r, B = b, G = g, A = 255};
        }

        public static ColorItem GetFromColor(Color color)
        {            
            return _colorItems.First(c => c.Color.Equals(color));
        }

        public static ColorItem[] Items
        {
            get
            {
                return _colorItems;
            }
        }
    }
}

ColorItem是:

public class ColorItem
{
    public ColorItem(Color color, string name)
    {
        this.Color = color;
        this.ColorName = name;
    }

    public Color Color
    {
        get;
        set;
    }

    public string ColorName
    {
        get;
        set;
    }
}

如你所见,在ColorItems._colorItems我有一些注释行。 如果没有这些界限,一切都会顺利进行。

请注意,错误和消息是由意大利语翻译过来的,因此它们可能不会像您预期的那样准确

但是当我启用其中一个时,它会抛出一个异常说:

  

System.Windows.ni.dll中的第一次机会异常'System.Exception'   灾难性故障(HRESULT异常:0x8000FFFF   (E_UNEXPECTED))

而且我不知道为什么它会抛出如此可怕的异常!

这是完整的堆栈跟踪。

System.Windows.ni.dll!MS.Internal.XcpImports.CheckHResult(uint hr)  Unknown
System.Windows.ni.dll!MS.Internal.XcpImports.ConvertStringToTypedCValue(System.IntPtr pContext, uint cClrTypeName, string clrTypeName, uint cValue, string value, ref MS.Internal.CValue outVal, out int typeIndex) Unknown
System.Windows.ni.dll!MS.Internal.SilverlightTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)    Unknown
System.Windows.ni.dll!MS.Internal.Data.DefaultValueConverter.ConvertHelper(object o, System.Type destinationType, System.Globalization.CultureInfo culture, bool isForward) Unknown
System.Windows.ni.dll!MS.Internal.Data.TargetDefaultValueConverter.Convert(object o, System.Type type, object parameter, System.Globalization.CultureInfo culture)  Unknown
System.Windows.ni.dll!MS.Internal.Data.DynamicValueConverter.Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)  Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.ConvertToTarget(object value = "Amber") Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.GetValue(System.Windows.DependencyObject d, System.Windows.DependencyProperty dp)   Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.EvaluateExpression(System.Windows.DependencyProperty property = {System.Windows.CoreDependencyProperty}, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry = {System.Windows.EffectiveValueEntry}) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.EvaluateBaseValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.EvaluateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation = Reevaluate)   Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property = {System.Windows.CoreDependencyProperty}, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry = {System.Windows.EffectiveValueEntry}, System.Windows.DependencyObject.ValueOperation operation) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.RefreshExpression(System.Windows.DependencyProperty dp)   Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.SendDataToTarget()  Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.SourceAcquired()    Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e)    Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.DataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnDataContextChanged(System.Windows.DataContextChangedEventArgs e)    Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e = {System.Windows.DataContextChangedEventArgs}) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnTreeParentUpdated(System.Windows.DependencyObject newParent, bool bIsNewParentAlive)    Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.UpdateTreeParent(MS.Internal.IManagedPeer oldParent = null, MS.Internal.IManagedPeer newParent = {System.Windows.Controls.ContentPresenter}, bool bIsNewParentAlive, bool keepReferenceToParent = true)   Unknown
System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(System.IntPtr oldParentElement, System.IntPtr parentElement, System.IntPtr childElement, byte bIsParentAlive, byte bKeepReferenceToParent, byte bCanCreateParent)    Unknown
[From native to managed]    
[From managed to native]    
System.Windows.ni.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element = {Telerik.Windows.Controls.RadDataBoundListBoxItem}, System.Windows.Size availableSize)  Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize)    Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, double inWidth, double inHeight, out double outWidth = 0.0, out double outHeight = 0.0)   Unknown
[From native to managed]    
[From managed to native]    
System.Windows.ni.dll!MS.Internal.XcpImports.UIElement_Measure(System.Windows.UIElement element = {Telerik.Windows.Controls.RadDataBoundListBoxItem}, System.Windows.Size availableSize)    Unknown
System.Windows.ni.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize)   Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.StackVirtualizationStrategy.MeasureContainer(Telerik.Windows.Controls.RadVirtualizingDataControlItem container = {Telerik.Windows.Controls.RadDataBoundListBoxItem})   Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.VirtualizationStrategy.GetContainerForItem(Telerik.Windows.Data.IDataSourceItem item = {Telerik.Windows.Data.DataSourceItem}, int insertAt = 0)    Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.StackVirtualizationStrategy.GetContainerForItem(Telerik.Windows.Data.IDataSourceItem item = {Telerik.Windows.Data.DataSourceItem}, int insertAt = 0)   Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.VirtualizationStrategy.GetContainerForItem(Telerik.Windows.Data.IDataSourceItem item = {Telerik.Windows.Data.DataSourceItem}, bool insertLast = true)  Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.VirtualizationStrategy.ManageLowerViewport(bool recycle = true)    Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.RadVirtualizingDataControl.ManageViewport()    Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.RadVirtualizingDataControl.BalanceVisualSpace()    Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.RadVirtualizingDataControl.OnLoaded(object sender = {Telerik.Windows.Controls.RadDataBoundListBox}, System.Windows.RoutedEventArgs e = {System.Windows.RoutedEventArgs})   Unknown
telerik.windows.controls.primitives.DLL!Telerik.Windows.Controls.RadDataBoundListBox.OnLoaded(object sender = {Telerik.Windows.Controls.RadDataBoundListBox}, System.Windows.RoutedEventArgs e = {System.Windows.RoutedEventArgs})  Unknown
System.Windows.ni.dll!MS.Internal.CoreInvokeHandler.InvokeEventHandler(int typeIndex, System.Delegate handlerDelegate, object sender, object args)  Unknown
System.Windows.ni.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj, System.IntPtr unmanagedObjArgs, int argsTypeIndex, int actualArgsTypeIndex, string eventName)    Unknown

有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

Fill属性绑定到ColorName属性(可能代替Color)。

最有可能发生的是,不是使用自定义颜色,而是使用Predifined Colors填充矩形(由名称标识)。

所以在大多数情况下,它都有效,因为给定制颜色的名称与预定颜色相匹配!但是当它不匹配时(被评论的颜色),找不到要显示的颜色,并且可能发生灾难性故障或其他异常异常;)