感谢Entlib 支持的Randy Levy,我得到了我需要的答案,那里:
看起来你还没有配置容器。如果您不想调用服务器来检索配置,那么您需要嵌入并加载配置。
string stringWithXAMLConfiguration = @"<?xml version=""1.0"" encoding=""utf-8""?>
<el:ConfigurationDictionary xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:el=""http://schemas.microsoft.com/practices/2011/entlib"">
<el:CachingSettings DefaultCache=""In-Memory Cache"" x:Key=""cachingSilverlightConfiguration"">
<el:CachingSettings.Caches>
<el:InMemoryCacheData ExpirationPollingInterval=""00:02:00"" Name=""In-Memory Cache"" />
<el:IsolatedStorageCacheData MaxSizeInKilobytes=""5120"" PercentOfQuotaUsedBeforeScavenging=""50"" PercentOfQuotaUsedAfterScavenging=""20"" ExpirationPollingInterval=""00:01:00"" Name=""Isolated Storage Cache"" />
</el:CachingSettings.Caches>
</el:CachingSettings>
</el:ConfigurationDictionary>";
var configDictionary = (IDictionary)XamlReader.Load(stringWithXAMLConfiguration);
var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
或者,如果您不想在代码中包含字符串但更喜欢在 XAML 文件中,请确保 XAML 文件(例如 cacheConfig.xaml)的构建操作是嵌入式资源,然后您可以使用以下代码:
string xaml;
using (Stream s = this.GetType().Assembly.GetManifestResourceStream("SilverlightApplicationCache.cacheConfig.xaml"))
using (StreamReader sr = new StreamReader(s))
xaml = sr.ReadToEnd();
var configDictionary = (IDictionary)XamlReader.Load(xaml);
var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
SilverlightApplicationCache
上面是 XAML 文件的命名空间(例如项目的默认命名空间)。