【问题标题】:DesignTime only Error: WPF "StaticExtension" exception仅设计时错误:WPF“StaticExtension”异常
【发布时间】:2011-08-08 21:30:11
【问题描述】:

我有这个组合框

<ComboBox Name="company" Width="120" 
                  HorizontalAlignment="Right" Margin="5" 
                  IsSynchronizedWithCurrentItem="True" 
                  ItemsPanel="{DynamicResource Virtualized}" 
                  ItemsSource="{x:Static local:Repository.Customers}" 
                  SelectedItem="{Binding Path=SelectedCustomer}" 
                  DisplayMemberPath="CM_FULL_NAME""/>

它运行。有用。除了在设计器中,由于错误,它不会让我做任何事情:

ArgumentException was thrown on "StaticExtention": Exception has been thrown by the target of an invocation.

详情

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

我在静态类中尝试了几件事以在设计时跳过构造函数,但都没有修复错误:

if (LicenseManager.UsageMode == LicenseUsageMode.DesignTime)
if (DesignerProperties.GetIsInDesignMode(this))
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Contains("VisualStudio"))

如果其中任何一个为真,则在构造函数中返回。仍然收到错误。

编辑:不确定是否有任何区别,但静态存储库类使用 EF4 从数据库获取。

Edit2:还尝试将 ItemsSource {Binding} 绑定到静态列表,但仍然出现相同的错误。请注意,将其称为存储库是用词不当,列表在启动时加载并且永远不会更改。下面的答案不起作用,仍在尝试解决这个问题。

Edit3:Thomas 关于调试设计模式的建议不可行。我使用的是 VS2010 Express,工具菜单没有附加到进程选项。我仍然不知道为什么这会破坏设计器并在运行时工作。

【问题讨论】:

    标签: wpf visual-studio-2010


    【解决方案1】:

    Customers属性的getter中,尝试添加这段代码:

    if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
        return null;
    

    【讨论】:

    • 你可以尝试在设计模式下调试,解释here
    • 反正不能通过绑定来设置ItemsSource吗?我会更干净......静态存储库是一种糟糕的代码气味。
    • 我不确定你的意思是什么,我使用的是基于另一个 stackoverflow 答案的静态类。但是, ItemsSource="{Binding Source={x:Static local:Repository.Customers}}" 仍然会导致相同的错误。
    • 我的意思是,您的 ItemsSource 应该来自 DataContext 上的绑定,而不是来自静态类。
    • 为什么?这些项目在启动时被检索,重复使用数十次,并且永远不会改变。
    【解决方案2】:

    托马斯回答:

    if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    return null;
    

    在静态构造函数中工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多