2011-11-28 29 views
12

我有一組非常簡單的樣式,我在幾個不同的WPF應用程序中使用樣式。我將這種風格存儲在一個通用項目的Xaml文件中,然後通過合併到ResourcesApp.xaml中添加到每個項目中。樣式TargetType在未連接到調試器時導致XamlParseException

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"> 
    <Style TargetType="dxe:ComboBoxEdit"> 
     <Setter Property="AutoComplete" Value="True" /> 
     <Setter Property="IncrementalFiltering" Value="True" /> 
     <Setter Property="ImmediatePopup" Value="True" /> 
     <Setter Property="IsTextEditable" Value="True" /> 
     <Setter Property="ClearSelectionOnBackspace" Value="True" /> 
    </Style> 
    <Style TargetType="dxe:ComboBoxEditSettings"> 
     <Setter Property="AutoComplete" Value="True" /> 
     <Setter Property="IncrementalFiltering" Value="True" /> 
     <Setter Property="ImmediatePopup" Value="True" /> 
     <Setter Property="IsTextEditable" Value="True" /> 
    </Style> 
</ResourceDictionary> 

不幸的是,一些關於這導致XamlParseException關於TargetType屬性,但只有當沒有連接到調試器。如果我在調試器中啓動應用程序,一切都很好。如果我「開始不調試」,我得到這個作爲App.xaml被加載:

System.Windows.Markup.XamlParseException: 'Failed to create a 'TargetType' from the text 'dxe:ComboBoxEdit'.' Line number '5' and line position '12'. ---> System.Xaml.XamlParseException: Type reference cannot find type named '{http://schemas.devexpress.com/winfx/2008/xaml/editors}ComboBoxEdit'. 
    at MS.Internal.Xaml.Context.ObjectWriterContext.ServiceProvider_Resolve(String qName) 
    at MS.Internal.Xaml.ServiceProviderContext.System.Windows.Markup.IXamlTypeResolver.Resolve(String qName) 
    at System.Xaml.Replacements.TypeTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) 
    at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateObjectWithTypeConverter(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value) 
    at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateFromValue(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value, XamlMember property) 
    at System.Xaml.XamlObjectWriter.Logic_CreateFromValue(ObjectWriterContext ctx, XamlValueConverter`1 typeConverter, Object value, XamlMember property, String targetName, IAddLineInfo lineInfo) 
    --- End of inner exception stack trace --- 
    at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) 
    at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
    at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) 
    at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) 
    at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) 
    at Shell.App.InitializeComponent() in c:\DevProjects\CoreApplication\Shell\App.xaml:line 1 
    at Shell.App.Main() in C:\DevProjects\CoreApplication\Shell\obj\x86\Debug\App.g.cs:line 0 

如果我註釋掉兩Style節點,則一切正常。有任何想法嗎?

+0

對於'TargetType =「{x:Type }」語法怎麼樣? – Jay

+0

@Jay:不幸的是,結果相同。 –

+0

您是否將您的自定義控件的樣式放入Themes/Generic.xaml中? –

回答

28

我有同樣的問題,對我來說,這是資源文件添加到項目的方式。

我不得不確保我的xaml樣式資源文件中的每一個都被設置爲「Page」(在「build action」屬性中)。默認情況下,它們並不是全部處於這種模式(有些稱爲「內容」,也就是「編譯」或「嵌入的資源」或「資源」),並且引起這種問題。

或許這對你一樣...

編輯:從我可以收集,它有事情做與XAML代碼是如何嵌入到項目和具體的訂單其中它是由WPF解析:如果源文件被設置爲「頁」,在字典中被合併時不考慮順序,因此這將在釋放模式工作:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <!-- let's say this dictionary contains a call to "MyButtonStyle" ... --> 
      <ResourceDictionary Source="resources/Common.xaml" /> 
      <!-- ... and this one contains the definition of "MyButtonStyle" --> 
      <ResourceDictionary Source="resources/GeneralResources.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

,但這不會與其他選項一起工作(不是所有的人都是,我沒有因爲該調用在定義之前將被解析。

至於它爲什麼在調試模式下工作,而不是在發佈(或者你的情況下,當你開始沒有調試),我猜是因爲WPF根據你所在的模式不同地解析和存儲資源到內存中調試或不)。我認爲WPF在調試模式下將所有內容存儲在內存中,而它只在釋放模式下存儲它需要的內容(例如調用代碼而不是定義代碼),但是再一次,這只是我的猜測...

編輯2:對我來說,調試是一場噩夢,因爲它更糟糕:它在某些發佈配置中運行,而不是其他配置,因爲根據用戶採取的操作順序,他會得到錯誤或不會(該資源可能已經在被調用時已經被記入內存或者不被調用,這取決於WPf在這一點上已經需要什麼),當然由於調試模式一直工作,所以我不能「調試」每一個說法......我花了一段時間才發現這一點解決了......很高興幫助

+0

好的電話!這解決了這個問題。然而,公平地說,我在賞金描述中表明,我想解釋爲什麼*就是這樣;有任何想法嗎? –

+0

看到我的編輯,但作爲書面,沒有任何證明,這只是我推斷:) – David

+1

謝謝;它不符合我的經驗(我從來沒有真正在發佈模式下構建,它始終以調試模式編譯,並且可以與調試器*附加*一起使用,但不能與它分離*)。在任何情況下,這都是我能夠找到的解釋,所以我會在定時器(從現在開始大約5分鐘) –

1

你有沒有參考devexpress dll的proj哪些有app.xaml.cs?如果不嘗試添加參考..

還檢查程序集加載通過使用 fuslogvw.exe並確保devexpress dll被加載。

+0

我還沒有嘗試過你的第二步,但是,通用項目(其中包含Xaml文件)和主項目(包含App.xaml)都包含對包含這些對象的DLL的引用。 –

+0

+1是一個好主意,但另一個答案解決了我的問題。謝謝! –

4

對我來說,工作是有點不同。我確實已經將構建操作設置爲「頁面」,但是我必須刪除任何目錄。例如:

<ResourceDictionary Source="resources/Common.xaml" /> 

將成爲

<ResourceDictionary Source="Common.xaml" /> 

值得注意的是,我被鏈接的文件,因爲我是在Silverlight

0

我正在與消息XamlParseException「無法創建一個「共享它們在文本中鍵入'ScatterViewItem'「,但只能在直接運行應用程序時,而不是從Visual Studio調試器中運行。

This external answer告訴我,無論什麼原因,必要的程序集尚未加載。 This StackOverflow answer向我展示瞭如何爲我的WPF應用程序創建Main()函數。我只是把「新的ScatterViewItem();」在我的Main()的頂部,問題就消失了!

我不知道這裏發生了什麼,特別是因爲ScatterViewItem被來自同一個程序集的ScatterView所包圍。這聽起來像XAML解析中的一個討厭的錯誤。

相關問題