DesignTime only Error: WPF “StaticExtension” exception

核能气质少年 提交于 2020-01-01 06:33:10

问题


I have this ComboBox

<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""/>

It runs. It works. Except in the designer, which won't let me do anything because of the error:

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

Detail

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

I have tried several things in the static class to skip the constructor in designtime, none of which fix the error:

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

And returning in the constructor if any of these are true. Still getting the error.

Edit: Not sure if it makes any difference, but the static repository class uses EF4 to get from a database.

Edit2: Also tried ItemsSource {Binding} to the static lists, still get the same error. Note, calling it a repository is a misnomer, the lists are loaded on startup and never changed. Below answer does not work, still trying to figure this out.

Edit3: Thomas' Suggestion to debug design mode wasn't doable. I am using VS2010 Express, and the tools menu does not have an attach to process option. I still don't know why this breaks the designer and works in runtime.


回答1:


In the getter of the Customers property, try to add this code:

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



回答2:


Thomas answer:

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

Works in the static constructor.



来源:https://stackoverflow.com/questions/6988863/designtime-only-error-wpf-staticextension-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!