C# WPF中的脱机图像缓存

C# WPF中的脱机图像缓存,c#,wpf,image,caching,bitmap,C#,Wpf,Image,Caching,Bitmap,我有一个WPF应用程序,当有互联网连接时从URL加载图像&当没有活动的互联网连接时,应该从缓存加载这些图像 我的问题是-在没有任何活动互联网连接的情况下,图像不会被缓存以在后续应用程序发布中显示 我搜索了几个小时,尝试了几乎所有可能的CreateOptions和CacheOption组合,但结果都是一样的 XAML代码如下所示: <Image Width="{Binding MY_TILE_W}" MaxWidth="380

我有一个WPF应用程序,当有互联网连接时从URL加载图像&当没有活动的互联网连接时,应该从缓存加载这些图像

我的问题是-在没有任何活动互联网连接的情况下,图像不会被缓存以在后续应用程序发布中显示

我搜索了几个小时,尝试了几乎所有可能的
CreateOptions
CacheOption
组合,但结果都是一样的

XAML代码如下所示:

              <Image
                   Width="{Binding MY_TILE_W}" MaxWidth="380" 
                   Height="{Binding MY_TILE_H}" MaxHeight="240" 
                   Stretch="UniformToFill">

                    <Image.CacheMode>
                        <BitmapCache  EnableClearType="False"
                              RenderAtScale="1"
                              SnapsToDevicePixels="False" />
                    </Image.CacheMode>

                    <Image.Source>
                        <BitmapImage UriSource="{Binding ImageUrl}" 
                             CreateOptions="IgnoreImageCache"
                             CacheOption="OnLoad"/>
                    </Image.Source>

                </Image>
然后,VS 2017的直接构建在脱机映像缓存中运行良好,但生成的安装文件(使用Inno setup)在以下日志中崩溃:

Set property 'System.Windows.Controls.ItemsControl.ItemTemplate' threw an exception.
Stack Trace:    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 MyApp.MyWindow.InitializeComponent() in E:\w_Development\DotNet\MyApp\MyWindow.xaml:line 1
   at MyApp.MyWindow..ctor(Window window) in E:\w_Development\DotNet\MyApp\MyWindow.xaml.cs:line 54
   at MyApp.MainWindow.<NavigateWithDelay>d__3.MoveNext() in E:\w_Development\DotNet\MyApp\MainWindow.xaml.cs:line 35
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at MyApp.App.Main()
Set属性“System.Windows.Controls.ItemsControl.ItemTemplate”引发异常。
堆栈跟踪:位于System.Windows.Markup.WpfXamlLoader.Load(XamlReader-XamlReader、IXamlObjectWriterFactory-writerFactory、Boolean-skipJournaledProperties、对象根对象、XamlObjectWriterSettings设置、Uri-baseUri)
位于System.Windows.Markup.wpfxamloader.LoadBaml(XamlReader XamlReader、Boolean skipJournaledProperties、Object rootObject、XamlAccessLevel accessLevel、Uri baseUri)
位于System.Windows.Markup.XamlReader.LoadBaml(Stream-Stream,ParserContext-ParserContext,Object-parent,Boolean-closeStream)
位于System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)
在E:\w\u Development\DotNet\MyApp\MyWindow.xaml中的MyApp.MyWindow.InitializeComponent()处:第1行
在E:\w\u Development\DotNet\MyApp\MyWindow.xaml.cs中的MyApp.MyWindow..ctor(窗口窗口窗口)处:第54行
在E:\w\u Development\DotNet\MyApp\MainWindow.xaml.cs中的MyApp.MainWindow.d\u 3.MoveNext()中:第35行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.CompilerServices.AsyncMethodBuilderCore.c.b_uu6_0(对象状态)中
位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、Int32 numArgs)
位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)
在System.Windows.Threading.DispatcherOperation.InvokeImpl()中
位于System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)
位于MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(对象obj)
位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext,ContextCallback回调,对象状态)
在System.Windows.Threading.DispatcherOperation.Invoke()中
在System.Windows.Threading.Dispatcher.ProcessQueue()中
位于System.Windows.Threading.Dispatcher.WndProcHook(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)上
位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、Int32 numArgs)
位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)
位于System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority优先级、TimeSpan超时、委托方法、对象参数、Int32 numArgs)
位于MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam)
位于MS.Win32.unsafentivemethods.DispatchMessage(MSG&MSG)
位于System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)
在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)上
位于System.Windows.Application.RunDispatcher(对象忽略)
位于System.Windows.Application.RunInternal(窗口)
在System.Windows.Application.Run(窗口)中
在MyApp.App.Main()中
我已经发布了。

使用符合标准,如中所示

我将
图像
块替换为库的this ui元素:

<cachedImage:Image Grid.Row="0" Grid.ColumnSpan="2"
        Width="{Binding MY_TILE_W}" MaxWidth="380" 
        Height="{Binding MY_TILE_H}" MaxHeight="240" 
        Stretch="UniformToFill"
        ImageUrl="{Binding ImageUrl}"/>
<cachedImage:Image Grid.Row="0" Grid.ColumnSpan="2"
        Width="{Binding MY_TILE_W}" MaxWidth="380" 
        Height="{Binding MY_TILE_H}" MaxHeight="240" 
        Stretch="UniformToFill"
        ImageUrl="{Binding ImageUrl}"/>


然后在使用InnoSetup脚本生成安装文件后出现异常,因为没有在我的InnoSetup脚本中添加
DLL引用。现在一切都很顺利。

@Clemens,你能再提供一些细节吗?比如一个片段、算法或到这种实现的链接?可能是重复的