Recently, a company project needs to write a custom control and then have the following control generated.
The style is not defined and the basic functionality has been implemented.
1. Create a XAML page for your custom control.
may need to use the WP source: http://code.662p.com/list/14_1.html
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;using System.windows.threading;namespace EZ. appplatform.app.videosummary.usercontrol{//<summary>//Advertpiccontrol.xaml interactive logic///</SUMMARY> ; Public partial class AdvertPicControl:System.Windows.Controls.UserControl {#region load list data///<SU mmary>///Current picture address playlist///</summary> private static list<string> currentlist; public static DependencyProperty advertpiclist = Dependencyproperty.register ("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>///photo player address///</summary>//<param name= "Sender" ></par am>//<param name= "E" ></param> private static void Loadadvertpic (DependencyObject sender, D Ependencypropertychangedeventargs e) {Advertpiccontrol Advertpiccontrol = (advertpiccontrol) sender; if (E.property = = advertpiclist) {advertpiccontrol.advertpiclist = (list<string> ) E.newvalue; Currentlist = advertpiccontrol.advertpiclist; }} #endregion #region loading picture time///<summary>//Current picture address playlist///</summ ary> private static list<int> currenttimelist; Public statIC 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>///Picture player photo Stay time///</summary>//<param name= "Sender" >< /param>//<param name= "E" ></param> private static void Loadadvertstaytime (DependencyObject sender, DependencyPropertyChangedEventArgs e) {Advertpiccontrol Advertpiccontrol = (advertpiccontrol) se NDEr; if (E.property = = advertpicstaytime) {advertpiccontrol.advertpicstaytime = (list<int>) E.N Ewvalue; Currenttimelist = Advertpiccontrol.advertpicstaytime; }} #endregion #region register custom events and parameters public static readonly RoutedEvent advertpicplays Tatechangedevent; public class Advertpicplayeventargs:routedeventargs {public int playstate { Get Set } public int Playlength {get; Set } public int Playindex {get; Set }} static Advertpiccontrol () {advertpicplaystatechangedevent = Eventmanager.registerrout Edevent ("advertpicplaystatechanged", Routingstrategy.bubble, typeof (Advertpicplaystatechangedhandler), type Of (Advertpiccontrol)); public delegate void Advertpicplaystatechangedhandler (object sender, Advertpicplayeventargs e); Public event Advertpicplaystatechangedhandler advertpicplaystatechanged {add {AddHandler (AdVertpiccontrol.advertpicplaystatechangedevent, value); } remove {RemoveHandler (advertpiccontrol.advertpicplaystatechangedevent, value);} #endregion #region dynamically load the corresponding toggle Picture button, click Respond to load the corresponding picture. #endregion public Advertpiccontrol () {InitializeComponent (); } dispatchertimer Switchpictimer = new DispatcherTimer (); int i = 0; private void Usercontrol_loaded (object sender, RoutedEventArgs e) {//default 1 seconds Toggle one picture//Switchpic timer.isenabled = false; Switchpictimer.tick + = switchpicevent; for (int j = 0; J < Currentlist.count; J + +) {button Btn=new button (); Btn. Width = 20; Btn. Height = 20; Btn. Content = j+1; Btn. Tag = j; Btn. Click+=new Routedeventhandler (Btn_click); PICCOUNTNUM.CHILDREN.ADD (BTN); }} voidBtn_click (object sender, RoutedEventArgs e) {button btn = (button) sender; BitmapImage bitmap = new BitmapImage (The New Uri (Currentlist[convert.toint32 (btn. TAG)] (Urikind.absolute)); Imgadvertpic.stretch = Stretch.fill; Imgadvertpic.source = bitmap; }///<summary>///start playing///</summary>/<param name= "interval" > Picture 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 () {SWI tchpictimer.isenabled = false; Switchpictimer.stop (); }//<summary> Toggle Picture Events///</summary>//<param name= "sender" ></param>//<param name= "E" ></param> private void Switchpicevent (object sender, EventArgs e) {if (null! = Curre Ntlist) {//Console.WriteLine ("Start switching ~ ~ ~"); if (i <= currentlist.count-1)//Modify to implement loop playback. {dohandlerstop (image.opacityproperty, 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, 0, 4, imgadvertpic, switchpic); } if (null! = currenttimelist) {Thread.Sleep (Currentti Melist[i]); Picture dwell Time}}///<summary>///animation finished toggle picture///</SUMMARY&G T <param name= "Sender" ></param>//<param name= "E" ></param> private void Switchpi C (object sender, EventArgs e) {BitmapImage bitmap = new BitmapImage (new Uri (Currentlist[i], urikind.abs Olute)); 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 doubleanimation = New DoubleAnimation ();//create double-precision Animated object doubleanimation.from = from; Doubleanimation.to = to;//Sets the end value of the animation doubleanimation.duration = Timespan.fromseconds (Duration);//Set Animation timeline length Doubleanimation.fillbehavior = fillbehavior.stop;//Sets the action performed after the animation is complete doubleanimation.completed + = Complatehand Er Element. BeginAnimation (DP, DoubleAnimation);//Set the properties of the animation app and start the animation}}}
Detailed Description: http://wp.662p.com/thread-8108-1-1.html
WPF implementation Picture Auto-Carousel custom Controls