我正在创建一个 windows phone 7 应用程序,因为我创建了图像幻灯片,图像随着时间间隔而变化,但我想给它们滑动效果这是我的代码:
xml代码:
<Grid Background="Red" HorizontalAlignment="Left" Height="242"
Margin="10,12,0,0"
VerticalAlignment="Top" Width="458">
<Image x:Name="myImg" Stretch="UniformToFill" Margin="0,0,0,28"/>
</Grid>
.cs 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Windows.Navigation;
namespace test
{
public partial class MainPage : PhoneApplicationPage
{
private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private List<string> texts = new List<string>();
private int imageIndex = 0;
private int textIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
//FirstStoryBoard.Begin();
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
tmr.Interval = TimeSpan.FromSeconds(2);
tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
LoadText();
ShowNextImage();
}
private void LoadImages()
{
images.Add("/img/11.jpg");
images.Add("/img/ee.jpg");
images.Add("/img/images.jpg");
images.Add("/img/20130820080826_F2.jpg");
}
private void LoadText()
{
texts.Add("image 1");
texts.Add("image2");
texts.Add("image3");
texts.Add("image3");
}
private void ShowNextImage()
{
var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = bi;
imageIndex = (imageIndex + 1) % images.Count;
}
private void ShowNextText()
{
var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
//var ti = new BitmapSource(new Uri(texts[textIndex], UriKind.Relative));
var ti = texts[textIndex];
mytext.Text = ti;
//mytext.Source = ti;
textIndex = (textIndex + 1) % texts.Count;
}
void tmr_Tick(object sender, EventArgs e)
{
ShowNextImage();
ShowNextText();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (!tmr.IsEnabled)
{
tmr.Start();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
tmr.Stop();
base.OnNavigatedFrom(e);
}