当枢轴选择更改时,如何在枢轴项目中加载图像后加载音频文件
Posted
技术标签:
【中文标题】当枢轴选择更改时,如何在枢轴项目中加载图像后加载音频文件【英文标题】:How to load audio file after image loaded in pivot items when pivot selection changed 【发布时间】:2014-09-24 13:10:40 【问题描述】:最近,我尝试为 windows phone 平台创建名为“Learn ABCD”的应用程序。
这个应用程序的主要逻辑是,最初我想在枢轴项目中加载所有字母字符图像,如苹果的 A、球的 B 等图像,并且在滑动每个枢轴项目时,相应的字母图像和音频文件将显示如下如果我滑动到 B 表示球图像和 B 语音文件对应的 B 应该在后台播放。
当使用枢轴控件滑动时,我已成功加载图像并播放了相应的音频文件,但问题是在图像显示给用户之前播放了音频文件。我需要的是在图像之后播放音频文件。
这是我在 Pivot 控件中加载图像的代码:
XAML 代码
<phone:Pivot x:Name="pivot" ItemsSource="Binding" SelectionChanged="pivot_SelectionChanged" >
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Name="HeaderText" Text="" Foreground="White" ></TextBlock>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<Grid Margin="0,-50,0,0">
<Image Name="img" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="Binding _images" />
</Grid>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
我的 CS 文件是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using LearnABCD.Resources;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using Microsoft.Xna.Framework.Media;
using System.Threading;
namespace LearnABCD
public partial class MainPage : PhoneApplicationPage
String[] HeadText = new String[] "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ;
String[] alpha = new String[] "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy", "zz" ;
String[] Galtones1 = "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18", "a19", "a20", "a21", "a22", "a23", "a24", "a25" ;
String[] Galtones2 = "aa0", "aa1", "aa2", "aa3", "aa4", "aa5", "aa6", "aa7", "aa8", "aa9", "aa10", "aa11", "aa12", "aa13", "aa14", "aa15", "aa16", "aa17", "aa18", "aa19", "aa20", "aa21", "aa22", "aa23", "aa24", "aa25" ;
private List<TList> _pivotList;
// Constructor
public MainPage()
InitializeComponent();
Loaded += Pivot_Loaded;
private void Pivot_Loaded(object sender, RoutedEventArgs e)
_pivotList = new List<TList>();
for (int i = 0; i < alpha.Length; i++)
_pivotList.Add(new TList
_images = "image/" + alpha[i] + ".png",
);
pivot.DataContext = _pivotList;
private void initialize_audio()
int noteID1 = pivot.SelectedIndex;
for (int i = 0; i < 3; i++)
if (i == 0)
Initialize();
Song song = Song.FromUri("Petzold", new Uri("AudioABCD/" + Galtones1[noteID1] + ".mp3", UriKind.Relative));
PlaySong(song);
else if (i == 1)
Initialize();
Song song = Song.FromUri("Petzold", new Uri("image/fr.mp3", UriKind.Relative));
PlaySong(song);
else if (i == 2)
Initialize();
Song song = Song.FromUri("Petzold", new Uri("AudioABCD/" + Galtones2[noteID1] + ".mp3", UriKind.Relative));
PlaySong(song);
Thread.Sleep(1000);
private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
initialize_audio();
void Initialize()
MediaPlayer.Stop();
void PlaySong(Song song)
MediaPlayer.Play(song);
class TList
public string _images get; set;
【问题讨论】:
【参考方案1】:使用Image
的Loaded
事件来触发您的声音。
即
<Image Loaded="OnImageLoaded"/>
然后在你的 .cs 文件中:
public void OnImageLoaded(object sender, EventArgs e)
PlaySong(song);
【讨论】:
在图像加载第一次加载图像时播放相应的音频..但是如果我反向滑动已经加载图像的音频不应该播放..请解决此错误... 添加布尔列表并设置它们以标记它们已经播放过。以上是关于当枢轴选择更改时,如何在枢轴项目中加载图像后加载音频文件的主要内容,如果未能解决你的问题,请参考以下文章