我正在使用Visual C#2010构建Windows Phone应用程序的第一个应用程序。
我用谷歌搜索,但没有得到太多帮助。我试过的是:
void Page1_Loaded(object sender, RoutedEventArgs e)
{
LoadImages();
}
private void LoadImages()
{
Image.Add("Image/aaa.jpg");
Image.Add("Image/bbb.jpg");
Image.Add("Image/ccc.jpg");
Image.Add("Image/ddd.jpg");
}
我在需要图像幻灯片的页面设计页面中编写了这些代码。
检查此样本。
在MainPage.xaml中添加图像
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="Img" Height="400" Tap="Img_OnTap" ></Image>
</Grid>
在MainPage.xaml.cs中执行以下操作
private List<string> _images;
/// <summary>
/// List of images
/// </summary>
public List<string> Images
{
get { return _images; }
set { _images = value; }
}
//selected image index
public int SelectedImageIndex { get; set; }
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
this.DataContext = this;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
LoadImages();
DispatcherTimer t = new DispatcherTimer();
//setting a 5 second interval
t.Interval = new TimeSpan(0, 0, 5);
t.Tick += t_Tick;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
if (SelectedImageIndex == Images.Count-1)
SelectedImageIndex = 0;
else
SelectedImageIndex++;
SetImageSource(Images[SelectedImageIndex]);
}
//Populating image list
private void LoadImages()
{
if (Images == null)
Images = new List<string>();
Images.Add("/Image/aaa.jpg");
Images.Add("/Image/bbb.jpg");
Images.Add("/Image/ccc.jpg");
Images.Add("/Image/ddd.jpg");
SelectedImageIndex = 0;
SetImageSource(Images[SelectedImageIndex]);
}
//setting image source
private void SetImageSource(string imagePath)
{
Img.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
}
//Image tap event
private void Img_OnTap(object sender, GestureEventArgs e)
{
string selectedImagePath = Images[SelectedImageIndex];
//Put your navigation here
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句