Using the WPF framework to implement automatic picture Carousel custom Control Source code

Source: Internet
Author: User

1. Create a XAML page that is a custom control.

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>
The interactive logic of Advertpiccontrol.xaml
</summary>
public partial class AdvertPicControl:System.Windows.Controls.UserControl
{
#region Load List data
<summary>
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>
Picture Player Address
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private static void Loadadvertpic (DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Advertpiccontrol Advertpiccontrol = (advertpiccontrol) sender;
if (E.property = = advertpiclist)
{
Advertpiccontrol.advertpiclist = (list<string>) e.newvalue;
Currentlist = advertpiccontrol.advertpiclist;
}
}
#endregion

#region Load Picture Stay time
<summary>
Current picture Address playlist
</summary>
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 picture Stay time
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private static void Loadadvertstaytime (DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Advertpiccontrol Advertpiccontrol = (advertpiccontrol) sender;
if (E.property = = advertpicstaytime)
{
Advertpiccontrol.advertpicstaytime = (list<int>) e.newvalue;
Currenttimelist = Advertpiccontrol.advertpicstaytime;
}
}
#endregion

#region registering 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 (Advertpicplaystatechangedhandler), typeof (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 the response 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 to toggle a picture
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. Content = 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 (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 ()
{
switchpictimer.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!= currentlist)
{
Console.WriteLine ("Start switching ~ ~ ~ ~");
if (i <= currentlist.count-1)//modify 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 (Currenttimelist[i]); Picture Stay Time
}
}
}

<summary>
Animation Playback finished switch pictures
</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 doubleanimation = new DoubleAnimation ();//Create a double animation 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 to perform after the animation completes
doubleanimation.completed + = Complatehander;
Element. BeginAnimation (DP, DoubleAnimation);//Set the properties of the animation application and start the animation
}
}
}

Foreground 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;
Using System.Windows.Threading;

Namespace EZ. AppPlatform.App.VideoSummary.UserControl
{
<summary>
The interactive logic of Advertpiccontrol.xaml
</summary>
public partial class AdvertPicControl:System.Windows.Controls.UserControl
{
#region Load List data
<summary>
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>
Picture Player Address
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private static void Loadadvertpic (DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Advertpiccontrol Advertpiccontrol = (advertpiccontrol) sender;
if (E.property = = advertpiclist)
{
Advertpiccontrol.advertpiclist = (list<string>) e.newvalue;
Currentlist = advertpiccontrol.advertpiclist;
}
}
#endregion

#region Load Picture Stay time
<summary>
Current picture Address playlist
</summary>
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 picture Stay time
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private static void Loadadvertstaytime (DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Advertpiccontrol Advertpiccontrol = (advertpiccontrol) sender;
if (E.property = = advertpicstaytime)
{
Advertpiccontrol.advertpicstaytime = (list<int>) e.newvalue;
Currenttimelist = Advertpiccontrol.advertpicstaytime;
}
}
#endregion

#region registering 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 (Advertpicplaystatechangedhandler), typeof (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 the response 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 to toggle a picture
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. Content = 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 (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 ()
{
switchpictimer.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!= currentlist)
{
Console.WriteLine ("Start switching ~ ~ ~ ~");
if (i <= currentlist.count-1)//modify 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 (Currenttimelist[i]); Picture Stay Time
}
}
}

<summary>
Animation Playback finished switch pictures
</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 doubleanimation = new DoubleAnimation ();//Create a double animation 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 to perform after the animation completes
doubleanimation.completed + = Complatehander;
Element. BeginAnimation (DP, DoubleAnimation);//Set the properties of the animation application and start the animation
}
}
}

2. Create the main form:

Call section:

XAML section, a Grid control

<window x:class= "EZ. AppPlatform.App.VideoSummary.TestPicscroll "
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
"Testpicscroll" height= "title=" width= "594" loaded= "window_loaded" >
<grid width= "579" name= "Grdcontent" >
<!--<button content= "Start" height= "horizontalalignment=" "left" margin= "92,51,0,0" Name= "Button1" Verticalalignment= "Top" width= "a" click= "Button1_Click"/>
<button content= "Stop" height= "horizontalalignment=" "Right" margin= "0,51,263,0" name= "Button2" Verticalalignment= "Top" width= "a" click= "button2_click"/>-->

</Grid>
</Window>

Service-side Code section.

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Text;
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.Imaging;
Using System.Windows.Shapes;
Using EZ. AppPlatform.App.VideoSummary.UserControl;

Namespace EZ. AppPlatform.App.VideoSummary
{
<summary>
The interactive logic of Testpicscroll.xaml
</summary>
public partial class Testpicscroll:window
{
Public Testpicscroll ()
{
InitializeComponent ();
}
<summary>
Gets the picture in the current user's picture folder (does not contain subfolders)
</summary>
<returns> return to Picture path list </returns>
Private list<string> GetUserImages (string path)
{
list<string> images = new list<string> ();
Environment.getfolderpath ();//environment.specialfolder.mypictures
DirectoryInfo dir = new DirectoryInfo (path);
fileinfo[] files = getpicfiles (path, "*.jpg,*.png,*.bmp,*.gif,", searchoption.topdirectoryonly);//dir. GetFiles ("*.jpg", searchoption.alldirectories);

if (Files!= null)
{
foreach (FileInfo file in files)
{
Images. ADD (file. FullName);
}
}
return images;
}

Public fileinfo[] Getpicfiles (String picpath, String searchpattern, SearchOption searchoption)
{
system.collections.generic.list<fileinfo> ltlist = new list<fileinfo> ();
DirectoryInfo dir = new DirectoryInfo (Picpath);
string[] Spattern = Searchpattern.replace ('; ', ', '). Split (', ');
for (int i = 0; i < spattern.length; i++)
{
fileinfo[] files = null;
Try
{
Files = dir. GetFiles (Spattern[i], searchoption);
}
catch (System.Exception ex)
{
Files = new fileinfo[] {};
}

Ltlist.addrange (files);
}
return Ltlist.toarray ();
}
Advertpiccontrol advertpic = new Advertpiccontrol ();
string path = @ "E:\ProjectSource\AppDesk\EZ. Appplatform.app.videosummary\assets\images\tem ";
private void Window_Loaded (object sender, RoutedEventArgs e)
{
list<string> imageList = getuserimages (path);
Advertpic.advertpiclist = imagelist;//this. GetUserImages ();
GRDCONTENT.CHILDREN.ADD (Advertpic);
Advertpic.advertpicplaystatechanged + = Playstatehandler;
Advertpic.play (2);
}


private void Playstatehandler (object sender, Advertpiccontrol.advertpicplayeventargs args)
{
MessageBox.Show ("finished playing, triggering events ...");
Advertpic.stop ();
Advertpic.play (2);
}

<summary>
Start playing
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, RoutedEventArgs e)
//  {
Advertpic.play (Convert.ToInt32 (2)); Set Default switch Time
//  }
<summary>
Stop playing
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button2_Click (object sender, RoutedEventArgs e)
//  {
Advertpic.stop ();
//  }
}
}

Then set the App.xaml startup entry as the main form, and then run, the default is 2s switch once.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.