gpt4 book ai didi

c# - 值不能为空。参数名称 : key (only happens on XAML Designer's design view)

转载 作者:太空狗 更新时间:2023-10-30 00:38:19 26 4
gpt4 key购买 nike

XAML Designer 设计 View 展示

抛出异常

ArgumentNullException:值不能为空。

参数名称:key

StackTrace(见照片后)

内部异常:无

我已经为以下问题苦苦挣扎了几天,这使我无法在每个受影响的 View 上使用 XAML Designer 的设计 View 。

昨天,我最终设法隔离了这种特别难以追踪的奇怪行为,因为它只发生在设计时,而且这似乎是泛型类型和 DataGrid 的 ItemsSource 属性之间的冲突(系统.Windows.控件)。

所以,这就是设计 View 上显示的内容

enter image description here

at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at
System.Collections.Generic.Dictionary
2.TryGetValue(TKey key, TValue& value) at System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(Object item, DependencyProperty property, Object& value) at System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject objectWithProperty, DependencyProperty property) at System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells) at System.Windows.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid) at System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item) at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item) at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container) at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled) at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized, Boolean isBeforeViewport) at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren) at System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable1& lastPageSafeOffset, List1& previouslyMeasuredOffsets, Nullable`1& lastPagePixelSize, Boolean remeasure) at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) at System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.UIElement.UpdateLayout()

Sample project source code

MyViewModelbase.cs(这是我的通用 View 模型库)

namespace BugProof.ViewModels
{
using System.Collections.Generic;

public class MyViewModelBase<TItem> where TItem : class
{
public List<TItem> Items { get; set; }
public MyViewModelBase() { }
}
}

MyExtendedViewModel.cs(这是我的扩展 View 模型,它将基于string 类型)

namespace BugProof.ViewModels
{
public class MyExtendedViewModel : MyViewModelBase<string>
{
public MyExtendedViewModel()
{
}
}
}

MainWindow.xaml

<Window x:Class="BugProof.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:BugProof.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BugProof"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance {x:Type vm:MyExtendedViewModel}, IsDesignTimeCreatable=False}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock>This is what you should se in the designer</TextBlock>
<!--Try replacing following DataGrid by a ListBox or ListView-->
<DataGrid ItemsSource="{Binding Items}"/>
</StackPanel>
</Window>

MainWindow.xaml.cs(MainWindow 背后的代码)

using System.Windows;

namespace BugProof
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

补充细节:

  • 如果用 ListboxListViewItemsControl 替换 DataGrid 控件,则不会发生此行为。<
  • 我正在使用 Visual Studio 2015,版本 14.0.25431.01 更新 3
  • 项目针对 .Net Framework 4.5

最佳答案

MainWindows.xaml 上设置 IsDesignTimeCreatable=True 就成功了,尽管它需要特别小心地实现 View 模型的无参数构造函数,检查代码是否在设计中运行-时间与否。

According to Microsoft ,设置 IsDesignTimeCreatable=True,“指定设计实例是根据您的类型创建的,而不是设计器生成的替代类型”。

令人惊讶的是,还有according to Microsoft ,如果 IsDesignTimeCreatable 未设置或设置为 False,“所有设计工具所做的就是解析类的可绑定(bind)属性”。

我想我们有两个相反的事实。这甚至可能是两者都是真的,这取决于上下文。可能是,第二个来源不知道,在编写文档时,XAML Designer 在 IsDesignTimeCreatable 设置为默认 False 值后自动生成的 3 个示例,每当它找到集合(IEnumerable)属性

除非另有证明,否则当 ItemsSource 绑定(bind)到通用集合源和 IsDesignTimeCreatable=False 时,这是一个 WPF DataGrid 控件错误,因为如果我们用 ListBoxListViewItemsControls 替换 DataGrid 控件,则不会出现此问题。

关于c# - 值不能为空。参数名称 : key (only happens on XAML Designer's design view),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42810814/

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