gpt4 book ai didi

WPF XAML 编辑器总是说 Dictionary with custom key type is not a collection

转载 作者:行者123 更新时间:2023-12-04 10:55:48 26 4
gpt4 key购买 nike

我正在尝试清除我们项目的 XAML 中的一些曲线。我面临的问题是我们使用使用自定义类型作为键而不是字符串的字典。
这在运行时工作正常,但 XAML 编辑器使用它们时会出现错误:

Type 'Dictionary`2' is not a Collection



例子:

<Window x:Class="DictionaryCustomKey.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:DictionaryCustomKey"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800" Height="450"
DataContext="{x:Static local:MainWindow.TheData}"
mc:Ignorable="d">
<StackPanel>
<ListView ItemsSource="{Binding StringKeyDictionary}" />
<TextBlock Text="String First is:" />
<TextBlock Text="{Binding StringKeyDictionary[First]}" />
<ListView ItemsSource="{Binding MyKeyDictionary}" />
<TextBlock Text="MyKey First is:" />
<TextBlock Text="{Binding MyKeyDictionary[First]}" />
</StackPanel>
</Window>

“First”的最后一个实例是波浪形的。

这是代码隐藏,我尝试了所有我能想象到的让编辑器高兴的键类型“MyKey”适合作为字典键:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows;

namespace DictionaryCustomKey
{
public class Data
{
public class MyKeyTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
return new MyKey((string)value);
}
return null;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value == null)
{
return null;
}
if (destinationType == typeof(string))
{
return ((MyKey)value).String;
}
return null;
}
}

[TypeConverter(typeof(MyKeyTypeConverter))]
public class MyKey : IComparable, IComparable<MyKey>, IComparable<string>, IEquatable<MyKey>, IEquatable<string>, IConvertible
{
public MyKey(string stringKey)
{
m_String = stringKey;
}

public static implicit operator MyKey(string v)
{
return new MyKey(v);
}

public static implicit operator string(MyKey v)
{
return v.m_String;
}

public String String => m_String;

private String m_String;

public override string ToString()
{
return String;
}

public override bool Equals(object obj)
{
if (obj is MyKey)
{
return ((MyKey)obj) == this;
}
if (obj is string)
{
return ((string)obj) == this.String;
}
return false;
}

public bool Equals(MyKey other)
{
return this == other;
}

public override int GetHashCode()
{
return String.GetHashCode();
}

public int CompareTo(object obj)
{
return String.CompareTo(obj);
}

public int CompareTo(MyKey other)
{
return String.CompareTo(other);
}

public int CompareTo(string other)
{
return String.CompareTo(other);
}

public bool Equals(string other)
{
return String.Equals(other);
}

public TypeCode GetTypeCode()
{
return String.GetTypeCode();
}

public bool ToBoolean(IFormatProvider provider)
{
return ((IConvertible)String).ToBoolean(provider);
}

public char ToChar(IFormatProvider provider)
{
return ((IConvertible)String).ToChar(provider);
}

public sbyte ToSByte(IFormatProvider provider)
{
return ((IConvertible)String).ToSByte(provider);
}

public byte ToByte(IFormatProvider provider)
{
return ((IConvertible)String).ToByte(provider);
}

public short ToInt16(IFormatProvider provider)
{
return ((IConvertible)String).ToInt16(provider);
}

public ushort ToUInt16(IFormatProvider provider)
{
return ((IConvertible)String).ToUInt16(provider);
}

public int ToInt32(IFormatProvider provider)
{
return ((IConvertible)String).ToInt32(provider);
}

public uint ToUInt32(IFormatProvider provider)
{
return ((IConvertible)String).ToUInt32(provider);
}

public long ToInt64(IFormatProvider provider)
{
return ((IConvertible)String).ToInt64(provider);
}

public ulong ToUInt64(IFormatProvider provider)
{
return ((IConvertible)String).ToUInt64(provider);
}

public float ToSingle(IFormatProvider provider)
{
return ((IConvertible)String).ToSingle(provider);
}

public double ToDouble(IFormatProvider provider)
{
return ((IConvertible)String).ToDouble(provider);
}

public decimal ToDecimal(IFormatProvider provider)
{
return ((IConvertible)String).ToDecimal(provider);
}

public DateTime ToDateTime(IFormatProvider provider)
{
return ((IConvertible)String).ToDateTime(provider);
}

public string ToString(IFormatProvider provider)
{
return String.ToString(provider);
}

public object ToType(Type conversionType, IFormatProvider provider)
{
return ((IConvertible)String).ToType(conversionType, provider);
}

public static bool operator ==(MyKey lhs, MyKey rhs)
{
return lhs.String == rhs.String;
}

public static bool operator !=(MyKey lhs, MyKey rhs)
{
return !(lhs == rhs);
}
}

public Dictionary<string, object> StringKeyDictionary { get; set; } = new Dictionary<string, object>
{
{"First", "zero"},
{"Second", "one"}
};
public Dictionary<MyKey, object> MyKeyDictionary { get; set; } = new Dictionary<MyKey, object>
{
{new MyKey("First"), "zero"},
{new MyKey("Second"), "one"}
};
}

public partial class MainWindow : Window
{
static public Data TheData { get; set; } = new Data();

public MainWindow()
{
InitializeComponent();
}
}
}

但似乎没有任何效果。

编辑 : 给出了一个让波浪消失的答案,但它揭示了实际上有一个更糟糕的问题:无论是否波浪,设计者都无法显示绑定(bind): https://developercommunity.visualstudio.com/content/problem/848331/visual-studio-must-be-restarted-after-every-build.html

最佳答案

这似乎是 XAML 编辑器中的一个错误。使用嵌套元素语法没有错误:

<TextBlock>
<TextBlock.Text>
<Binding Path="MyKeyDictionary[First]" />
</TextBlock.Text>
</TextBlock>

我建议通过 Visual Studio 反馈工具报告它。

关于WPF XAML 编辑器总是说 Dictionary with custom key type is not a collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com