This is a bit likeBugNot quite likeBugThe process of things is as follows:
Inherited fromContentcontrolWriteMycontentcontrol, Which definesIconpropertyDependencies and their correspondingCLRAttribute and called in its static structureDefaultstylekeyproperty. overridemetadataMethod,CodeVery few. It looks like this:
Class mycontentcontrol: contentcontrol {static mycontentcontrol () {defastystylekeyproperty. overridemetadata (typeof (mycontentcontrol), new frameworkpropertymetadata (typeof (mycontentcontrol);} public imagesource icon {get {return (imagesource) getvalue (iconproperty);} set {setvalue (iconproperty, value) ;}} public static readonly dependencyproperty iconproperty = dependencyproperty. register ("icon", typeof (imagesource), typeof (mycontentcontrol ));}
TheIconThe attribute declaration type isImagesourceThe purpose is simple and clear. Of course, it is to add an icon to this control.
And then define a place for the custom controlGeneric. XAMLInTemplateIt's as simple as usingStackpanelPut itsIconAndContentThe Code is as follows:
<Style targettype = "{X: type local: mycontentcontrol}"> <style. setters> <setter property = "template"> <setter. value> <controltemplate targettype = "{X: type local: mycontentcontrol} "> <stackpanel> <Image Source =" {templatebinding icon} "stretch =" fill "/> <contentpresenter content =" {templatebinding content} "/> </stackpanel> </controltemplate> </setter. value> </setter> </style. setters> </style>
Define a form with oneCanvasAnd oneButton, ClickButtonLoadCanvasEmpty and then add to it500Random custom controls, this part of the code is as follows:
Canvas. children. clear (); random = new random (); For (INT I = 0; I <500; I ++) {mycontentcontrol marker = new mycontentcontrol {content = I, icon = bitmap}; marker. setvalue (canvas. leftproperty, (double) random. next (0, (INT) canvas. actualwidth); marker. setvalue (canvas. topproperty, (double) random. next (0, (INT) canvas. actualheight); canvas. children. add (Marker );}
TheBitmapIs a private field of the form, which is associated with a small arrowPNGImage (thisBitmapIt is a private field of the form or a local variable in the method that will affect the result, which will be described later ).
Then runProgramClick it.ButtonThe following is the result:
It looks pretty normal.
But try multiple times.ButtonThen I found something wrong. Why is it slower than the last time?
So I joined500The time is monitored, and the code is as follows:
Stopwatch watch = new stopwatch (); watch. start (); this. dispatcher. begininvoke (new action () => {This. title = watch. elapsedmilliseconds. tostring (); If (count <= 10) {clearandaddmarkers () ;}else {COUNT = 0 ;}count ++ ;}), dispatcherpriority. loaded );
the timer runs on canvas children after filling, dispatcher loaded this ensures that the recorded time is used render is not the time when the set is filled. The process of clearing, filling, and timing is run ten times in a row, and the recorded time is written to the title .
ClickButton, ObserveWindowOfTitleFirst400Multiple milliseconds, and then600Many,800Multiple......Last time I used1300Yes. Of course, if your machine sub-configuration is too good, you have to increase the number of custom controls.
It's strange. I thought it was a problem with code writing (but it was not the key), but I couldn't find it. So I triedGeneric. XAMLChange the name so that it cannot be automatically applied, and then reference the resource dictionary with the changed name in the form. As a result, a strange thing happened, and every time was stable400Milliseconds.
It looks likeWPFPairGeneric. XAMLThis method may be suspected to be a problem.Bug. Why is the title not similar?BugWhat about it?
This involves the aforementionedBitmapIf this private field is removed, it is being filled.CanvasOfChildrenEvery timeNewA newBitmapimageTo assign a value to each custom controlIconYou can maintain400In milliseconds, so it is not very similar.Bug.
If someone encounters a similar problem, try again.Generic. XAML, Use a common resource dictionary named by yourself, or do not let the formHoldHousingBitmapThis resource is not released, every timeNewOneBitmapimageTry it.
HoweverTemplateDefined inGeneric. XAMLAutomatic application and definition in the general resource dictionary manually referencing these two methods will lead to different program performance is a strange problem after all, hope to have a good expert to give a better solution and explanation.
PS: I tried. Net 3.5And. Net 4UseDebugAndReleaseCompile and try again inVSRunning and detachingVSRunning independently is problematic.
In addition, I have tried to launch a program by Microsoft.Kb981107This patch is useless.
Download Code