Recently, a company project needs to write a custom control, and then the following controls are generated.
The style is not defined, and basic functions have been implemented.
1. Create a custom control's XAML page.
The following is the background code
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; using system. windows; using system. windows. controls; using system. windows. data; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. media. imaging; using system. windows. navigation; using system. windows. shapes; US Ing system. windows. threading; namespace EZ. appplatform. app. videosummary. usercontrol {// <summary> // advertpiccontrol. interaction logic of XAML // </Summary> Public partial class advertpiccontrol: system. windows. controls. usercontrol {# region load list data /// <summary> /// the stream play list of the current image address /// </Summary> Private Static list <string> currentlist; public static dependencyproperty advertpiclist = dependencyproperty. registe R ("advertpiclist", typeof (list <string>), typeof (advertpiccontrol), new propertymetadata (New propertychangedcallback (loadadvertpic ))); public list <string> advertpiclist {get {return (list <string>) getvalue (advertpiclist);} set {setvalue (advertpiclist, value );}} /// <summary> /// address of the image player /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private Static void loadadver Tpic (dependencyobject sender, dependencypropertychangedeventargs e) {advertpiccontrol = (advertpiccontrol) sender; If (E. property = advertpiclist) {advertpiccontrol. advertpiclist = (list <string>) E. newvalue; currentlist = advertpiccontrol. advertpiclist ;}} # endregion # region load the image stay time /// <summary> /// the current image address playlist /// </Summary> Private Static list <int> currenttimelist; Public sta TIC dependencyproperty advertpicstaytime = dependencyproperty. register ("advertpicstaytime", typeof (list <int>), typeof (advertpiccontrol), new propertymetadata (New propertychangedcallback (loadadvertstaytime ))); public list <int> advertpicstaytime {get {return (list <int>) getvalue (advertpicstaytime) ;}set {setvalue (advertpicstaytime, value );}} /// <summary> /// the duration of the image in the image player /// </Summary> /// <Pa Ram name = "sender"> </param> // <Param name = "E"> </param> Private Static void loadadvertstaytime (dependencyobject sender, dependencypropertychangedeventargs E) {advertpiccontrol = (advertpiccontrol) sender; If (E. property = advertpicstaytime) {advertpiccontrol. advertpicstaytime = (list <int>) E. newvalue; currenttimelist = advertpiccontrol. advertpicstaytime ;}# endregion # region Registration Custom events and parameters: public static readonly routedevent advertpicplaystatechangedevent; public class advertpicplayeventargs: routedeventargs {public int playstate {Get; set;} public int playlength {Get; set ;} public int playindex {Get; Set ;}} static advertpiccontrol () {advertpicplaystatechangedevent = eventmanager. registerroutedevent ("advertpicplaystatechanged", routingstrategy. bubble, typeof (advert Picplaystatechangedhandler), typeof (advertpiccontrol);} Public Delegate void handle (Object sender, advertpicplayeventargs E); public event uploadvertpicplaystatechanged {Add {addhandler (advertpiccontrol. advertpicplaystatechangedevent, value);} remove {removehandler (advertpiccontrol. advertpicplaystatechangedevent, value) ;}# endregion # Region dynamically loads the corresponding switching image button, and click the response to load the corresponding image. # Endregion public advertpiccontrol () {initializecomponent ();} dispatchertimer switchpictimer = new dispatchertimer (); int I = 0; private void usercontrol_loaded (Object sender, routedeventargs E) {// by default, one image is switched in 1 second. // switchpictimer. isenabled = false; switchpictimer. tick + = switchpicevent; For (Int J = 0; j <currentlist. count; j ++) {button BTN = new button (); BTN. width = 20; BTN. height = 20; BTN. con Tent = J + 1; BTN. tag = J; BTN. click + = new routedeventhandler (btn_click); piccountnum. children. add (BTN) ;}} void btn_click (Object sender, routedeventargs e) {button BTN = (button) sender; bitmapimage bitmap = new bitmapimage (New uri (currentlist [convert. toint32 (BTN. tag)], urikind. absolute); imgadvertpic. stretch = stretch. fill; imgadvertpic. source = bitmap;} // <summary> // start playing // </Summary>/ // <Param name = "interval"> image switching time </param> Public void play (INT interval) {int defaultinterval = 0; If (Interval! = 0) defaultinterval = interval; switchpictimer. isenabled = true; switchpictimer. interval = new timespan (0, 0, defaultinterval); switchpictimer. start (); I = 0 ;}/// <summary> /// stop playing /// </Summary> Public void stop () {switchpictimer. isenabled = false; switchpictimer. stop ();} /// <summary> /// switch the image event /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void switc Hpicevent (Object sender, eventargs e) {If (null! = Currentlist) {// console. writeline ("start switching ~~~ "); If (I <= currentlist. Count-1) // modify to implement loop playback. {Dohandlerstop (image. opacityproperty, 20, 0, 4, imgadvertpic, switchpic);} else {// advertpicplayeventargs ARGs = new advertpicplayeventargs (); // args. routedevent = advertpicplaystatechangedevent; // args. playstate = 1; // raiseevent (ARGs); // switchpictimer. stop (); // switchpictimer. isenabled = false; I = 0; dohandlerstop (image. opacityproperty, 20, 0, 4, imgadvertpic, switchpic);} If (null! = Currenttimelist) {thread. sleep (currenttimelist [I]); // picture stop time }}/// <summary> /// after the animation is played, change the picture /// </Summary> /// <Param name = "sender"> </param> // <Param name = "E"> </param> private void switchpic (Object sender, eventargs e) {bitmapimage bitmap = new bitmapimage (New uri (currentlist [I], urikind. absolute); imgadvertpic. stretch = stretch. fill; imgadvertpic. source = bitmap; if (I <currentlist. count) {I ++ ;}} public void clicktopic (INT ID) {}/// <summary> /// animation /// </Summary> /// <Param name = "DP"> </param> /// <Param name = "from"> </param> // <Param name = "to"> </param> // <Param name = "duration"> </param>/ // <Param name = "element"> </param> // <Param name = "complatehander"> </param> Public void dohandlerstop (dependencyproperty DP, double from, double to, double duration, uielement element, eventhandler complatehander) {doubleanimation = new doubleanimation (); // create a double precision animation object doubleanimation. from = from; doubleanimation. to = to; // set the animation end value doubleanimation. duration = timespan. fromseconds (duration); // sets the length of the animation timeline doubleanimation. fillbehavior = fillbehavior. stop; // the operation that is executed after the animation is set. doubleanimation. completed + = complatehander; element. beginanimation (DP, doubleanimation); // set the attributes of the animation application and start the animation }}}
Front-end XAML code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; using system. windows; using system. windows. controls; using system. windows. data; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. media. imaging; using system. windows. navigation; using system. windows. shapes; US Ing system. windows. threading; namespace EZ. appplatform. app. videosummary. usercontrol {// <summary> // advertpiccontrol. interaction logic of XAML // </Summary> Public partial class advertpiccontrol: system. windows. controls. usercontrol {# region load list data /// <summary> /// the stream play list of the current image address /// </Summary> Private Static list <string> currentlist; public static dependencyproperty advertpiclist = dependencyproperty. registe R ("advertpiclist", typeof (list <string>), typeof (advertpiccontrol), new propertymetadata (New propertychangedcallback (loadadvertpic ))); public list <string> advertpiclist {get {return (list <string>) getvalue (advertpiclist);} set {setvalue (advertpiclist, value );}} /// <summary> /// address of the image player /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private Static void loadadver Tpic (dependencyobject sender, dependencypropertychangedeventargs e) {advertpiccontrol = (advertpiccontrol) sender; If (E. property = advertpiclist) {advertpiccontrol. advertpiclist = (list <string>) E. newvalue; currentlist = advertpiccontrol. advertpiclist ;}} # endregion # region load the image stay time /// <summary> /// the current image address playlist /// </Summary> Private Static list <int> currenttimelist; Public sta TIC dependencyproperty advertpicstaytime = dependencyproperty. register ("advertpicstaytime", typeof (list <int>), typeof (advertpiccontrol), new propertymetadata (New propertychangedcallback (loadadvertstaytime ))); public list <int> advertpicstaytime {get {return (list <int>) getvalue (advertpicstaytime) ;}set {setvalue (advertpicstaytime, value );}} /// <summary> /// the duration of the image in the image player /// </Summary> /// <Pa Ram name = "sender"> </param> // <Param name = "E"> </param> Private Static void loadadvertstaytime (dependencyobject sender, dependencypropertychangedeventargs E) {advertpiccontrol = (advertpiccontrol) sender; If (E. property = advertpicstaytime) {advertpiccontrol. advertpicstaytime = (list <int>) E. newvalue; currenttimelist = advertpiccontrol. advertpicstaytime ;}# endregion # region Registration Custom events and parameters: public static readonly routedevent advertpicplaystatechangedevent; public class advertpicplayeventargs: routedeventargs {public int playstate {Get; set;} public int playlength {Get; set ;} public int playindex {Get; Set ;}} static advertpiccontrol () {advertpicplaystatechangedevent = eventmanager. registerroutedevent ("advertpicplaystatechanged", routingstrategy. bubble, typeof (advert Picplaystatechangedhandler), typeof (advertpiccontrol);} Public Delegate void handle (Object sender, advertpicplayeventargs E); public event uploadvertpicplaystatechanged {Add {addhandler (advertpiccontrol. advertpicplaystatechangedevent, value);} remove {removehandler (advertpiccontrol. advertpicplaystatechangedevent, value) ;}# endregion # Region dynamically loads the corresponding switching image button, and click the response to load the corresponding image. # Endregion public advertpiccontrol () {initializecomponent ();} dispatchertimer switchpictimer = new dispatchertimer (); int I = 0; private void usercontrol_loaded (Object sender, routedeventargs E) {// by default, one image is switched in 1 second. // switchpictimer. isenabled = false; switchpictimer. tick + = switchpicevent; For (Int J = 0; j <currentlist. count; j ++) {button BTN = new button (); BTN. width = 20; BTN. height = 20; BTN. con Tent = J + 1; BTN. tag = J; BTN. click + = new routedeventhandler (btn_click); piccountnum. children. add (BTN) ;}} void btn_click (Object sender, routedeventargs e) {button BTN = (button) sender; bitmapimage bitmap = new bitmapimage (New uri (currentlist [convert. toint32 (BTN. tag)], urikind. absolute); imgadvertpic. stretch = stretch. fill; imgadvertpic. source = bitmap;} // <summary> // start playing // </Summary>/ // <Param name = "interval"> image switching time </param> Public void play (INT interval) {int defaultinterval = 0; If (Interval! = 0) defaultinterval = interval; switchpictimer. isenabled = true; switchpictimer. interval = new timespan (0, 0, defaultinterval); switchpictimer. start (); I = 0 ;}/// <summary> /// stop playing /// </Summary> Public void stop () {switchpictimer. isenabled = false; switchpictimer. stop ();} /// <summary> /// switch the image event /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void switc Hpicevent (Object sender, eventargs e) {If (null! = Currentlist) {// console. writeline ("start switching ~~~ "); If (I <= currentlist. Count-1) // modify to implement loop playback. {Dohandlerstop (image. opacityproperty, 20, 0, 4, imgadvertpic, switchpic);} else {// advertpicplayeventargs ARGs = new advertpicplayeventargs (); // args. routedevent = advertpicplaystatechangedevent; // args. playstate = 1; // raiseevent (ARGs); // switchpictimer. stop (); // switchpictimer. isenabled = false; I = 0; dohandlerstop (image. opacityproperty, 20, 0, 4, imgadvertpic, switchpic);} If (null! = Currenttimelist) {thread. sleep (currenttimelist [I]); // picture stop time }}/// <summary> /// after the animation is played, change the picture /// </Summary> /// <Param name = "sender"> </param> // <Param name = "E"> </param> private void switchpic (Object sender, eventargs e) {bitmapimage bitmap = new bitmapimage (New uri (currentlist [I], urikind. absolute); imgadvertpic. stretch = stretch. fill; imgadvertpic. source = bitmap; if (I <currentlist. count) {I ++ ;}} public void clicktopic (INT ID) {}/// <summary> /// animation /// </Summary> /// <Param name = "DP"> </param> /// <Param name = "from"> </param> // <Param name = "to"> </param> // <Param name = "duration"> </param>/ // <Param name = "element"> </param> // <Param name = "complatehander"> </param> Public void dohandlerstop (dependencyproperty DP, double from, double to, double duration, uielement element, eventhandler complatehander) {doubleanimation = new doubleanimation (); // create a double precision animation object doubleanimation. from = from; doubleanimation. to = to; // set the animation end value doubleanimation. duration = timespan. fromseconds (duration); // sets the length of the animation timeline doubleanimation. fillbehavior = fillbehavior. stop; // the operation that is executed after the animation is set. doubleanimation. completed + = complatehander; element. beginanimation (DP, doubleanimation); // set the attributes of the animation application and start the animation }}}
To be updated ......
Custom Controls for automatic image carousel using WPF