DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

BackgroundCreation Crashing the Designer in Visual Studio? STOP IT!

Chris Smith user avatar by
Chris Smith
·
Jan. 31, 12 · Interview
Like (0)
Save
Tweet
Share
3.38K Views

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. :)

download



source: http://socialebola.wordpress.com/2012/01/25/backgroundcreation-crashes-the-designer/


IT

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • HTTP vs Messaging for Microservices Communications
  • Cucumber.js Tutorial With Examples For Selenium JavaScript

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: