BackgroundCreation Crashing the Designer in Visual Studio? STOP IT!
Join the DZone community and get the full member experience.
Join For Free
windows phone mango introduced a new enum value to bitmap creation
that will actually load and decode the image on a background thread
instead of on the ui thread. in some cases, that gives noticeable perf
benefits, but there are a few things that are problematic about it.
default is everything
for some very good reasons , this behavior is off by default. developers need to opt-in to it.
that also means though, that 99% of the apps out there are just not
using it. that’s not a big deal in most cases, but in some, it can make
your app feel much snappier.
originally authored by social ebola
so where’s the crash?
in visual studio, if you try to use this property, under certain
circumstances, the designer will crash (show an error on the designer
surface):
system.reflection.targetinvocationexception exception has been thrown by the target of an invocation. at system.runtimemethodhandle._invokemethodfast(iruntimemethodinfo method, object target, object[] arguments, signaturestruct& sig, methodattributes methodattributes, runtimetype typeowner) at system.runtimemethodhandle.invokemethodfast(iruntimemethodinfo method, object target, object[] arguments, signature sig, methodattributes methodattributes, runtimetype typeowner) at system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture, boolean skipvisibilitychecks) at system.delegate.dynamicinvokeimpl(object[] args) at system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) at ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) system.invalidoperationexception layout measurement override of element 'microsoft.windows.design.platform.silverlightviewproducer+silverlightcontenthost' should not return positiveinfinity as its desiredsize, even if infinity is passed in as available size. at system.windows.uielement.measure(size availablesize) at ms.internal.designer.deviceskinviewpresenter.devicedesignerbackground.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at microsoft.windows.design.interaction.designerview.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at ms.internal.designer.viewport.measureoverride(size availablesize) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at ms.internal.helper.measureelementwithsinglechild(uielement element, size constraint) at system.windows.controls.scrollcontentpresenter.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at system.windows.controls.grid.measurecell(int32 cell, boolean forceinfinityv) at system.windows.controls.grid.measurecellsgroup(int32 cellshead, size referencesize, boolean ignoredesiredsizeu, boolean forceinfinityv) at system.windows.controls.grid.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at system.windows.controls.scrollviewer.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at system.windows.controls.grid.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at ms.internal.helper.measureelementwithsinglechild(uielement element, size constraint) at system.windows.controls.contentpresenter.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at system.windows.controls.control.measureoverride(size constraint) at system.windows.frameworkelement.measurecore(size availablesize) at system.windows.uielement.measure(size availablesize) at system.windows.interop.hwndsource.setlayoutsize() at system.windows.interop.hwndsource.set_rootvisualinternal(visual value) at ms.internal.deferredhwndsource.processqueue(object sender, eventargs e)
so what’s the problem?
the issue is with the runtime used in visual studio to display the
wp7 xaml. visual studio 2010 uses the silverlight engine for this and
the silverlight engine apparently does not have that enum value
implemented – and so, if there’s a design time datacontext applied in
xaml, silverlight craps out and crashes, causing the error message you
see.
how do you solve it?
there may be a bunch of ways of solving it, but mine involves a
converter. use the <image> xaml element as you usually would, but
in the binding of the source, apply the attached converter with the
relevant parameters:
<image source="{binding path=myimage, converter={staticresource runtimeimageloaderconverter1}, converterparameter=bd}"/>
the converter obviously needs to be declared in the resources of the
page or control. the interesting bit is the use of the converter
parameters:
converterparameter=bd
“b” will make sure the image gets loaded in the background (the aforementioned backgroundcreation flag) and “d” will make sure that it’s delay loaded.
that’s it – once you use the designer, the system will load the image
normally (no funky flags) when in the designer and with the relevant
flags when actually running on the phone.
how does it work?
it’s pretty simple, the converter checks to see if it’s in the design
environment and if it is, it will default-create the image. otherwise,
it will use the relevant flags (it also makes sure it knows how to load
string uris and regular uris):
public class runtimeimageloaderconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { uri uri = null; if (value is uri) { uri = (uri)value; } else if (value is string) { string s = (string)value; if (s.startswith("/")) { uri.trycreate(s, urikind.relative, out uri); } else { uri.trycreate(s, urikind.absolute, out uri); } } bitmapcreateoptions options = bitmapcreateoptions.none; string p = parameter as string; if (p != null && getisindesignmode()) { if (p.contains("b") || p.contains("b")) { options |= bitmapcreateoptions.backgroundcreation; } if (p.contains("d") || p.contains("d")) { options |= bitmapcreateoptions.delaycreation; } } bitmapimage result = null; if (uri != null) { result = new bitmapimage(); result.createoptions = options; result.urisource = uri; } return result; }
that’s about it. hope this helps someone.
source: http://socialebola.wordpress.com/2012/01/25/backgroundcreation-crashes-the-designer/
Opinions expressed by DZone contributors are their own.
Comments