wpf prism 项目使用IMemoryCache

1.将MemoryCache注入到容器

App.xaml.cs中写入代码:

  protected override IContainerExtension CreateContainerExtension()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddMemoryCache();
            return new DryIocContainerExtension(new Container(CreateContainerRules())
                .WithDependencyInjectionAdapter(serviceCollection));
        }

2.程序中使用:

private readonly IMemoryCache _memoryCache;

public 构造函数(IMemoryCache memoryCache)
{
   _memoryCache = memoryCache;
}

public TestMemoryCache()
{
  _memoryCache.Set($"testCache","hello");
}

你可能感兴趣的:(wpf,c#)