2012-09-24 32 views
0

所有內容都發生在同一VS項目中。我有一個自己的資源字典文件。當我嘗試以編程方式加載時,出現錯誤無法使用轉換器加載資源字典

「無法創建未知類型'{clr-namespace:MyAssembly.Helpers} IsNullConverter」。

這是我如何加載:

StreamResourceInfo stream = Application.GetResourceStream(new Uri(@"MyAssembly;component/Resources/Resources.xaml", UriKind.Relative)); 

this.dynamicResources = XamlReader.Load(stream.Stream) as ResourceDictionary; 

這裏是資源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:helpers="clr-namespace:MyAssembly.Helpers"> 

<helpers:IsNullConverter x:Key="IsNullConverter" /> 

Styles go here... 

注意它綁定到代碼隱藏文件,但沒有什麼它。資源文件的Build-Action設置爲「Resource」。這從今天早上開始讓我瘋狂起來,至今仍不知道究竟是怎麼回事......

幫助。 謝謝。

+0

哪裏'MyAssembly.Helpers.IsNulLConverter'定義?解析器是,提供路徑下尋找類和未能找到預期的元數據... – code4life

回答

0

組件是否被您的項目引用?如果沒有嘗試添加引用 - 如果你不想依賴你可以嘗試加載程序集的更多信息:

http://www.dreamincode.net/forums/topic/78974-using-reflection-to-load-unreferenced-assemblies-at-runtime/

另外,您可以添加斧:類定義的ResourceDictionary中,並從組件實例化類而不是加載xaml,並記得從構造函數調用生成的InitializeComponent(),它將加載。

Is it possible to set code behind a resource dictionary in WPF for event handling?

你的榜樣將正常工作,如果在資源字典和轉換器是在同一個組件,你來自哪裏加載,據我所看到:)

+0

感謝您的回答,但這並沒有解決問題。想通了,我會發布答案。 – Mohamed

1

Halelujah我fugured出來。所有我需要做的就是加載資源字典直接

開放的URI =新的URI(@MyAssembly;組件/資源/ Resources.xaml」,UriKind.Relative); this.dynamicResources.Source = URI;

而且使資源字典文件確保生成操作設置爲「頁」

\ M/