我有一个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>
除了C#
代码以外,没有其他Binding
代码了。
是否有可能实现我正在尝试的目标?如果是,请如何?
编辑: 正如@Wilson的评论所示,我的问题与this SO post并不完全相同-因为在我的用例中每次下载图像都不相同,但是the accepted answer's mentioned CachedImage library已部分解决了该问题
我将此ui元素的Image
块替换为the library's:
<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}"/>
然后从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()
我已经发布了an issue regarding this crash in library's issue-board。
答案 0 :(得分:0)
使用CachedImage library符合标准,如this SO answer所示。
我用库的ui元素替换了Image
块:
<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 reference
。现在一切正常。