C# 当触发某些事件时,如何更改listboxitem中按钮中的图像?
我正在用C# 当触发某些事件时,如何更改listboxitem中按钮中的图像?,c#,silverlight,windows-phone-7,button,C#,Silverlight,Windows Phone 7,Button,我正在用LongListboxSelector制作一个显示音频曲目的页面。每个项目都有带有艺术家和标题信息的按钮“播放”和文本块。我正在使用BackgroundAudioPlayer。我的愿望是使下一个:播放曲目时,按钮的图像是“暂停”,当曲目结束时,使其按钮图像“播放”,并使下一首曲目的按钮图像“暂停”(下一首曲目自动播放)。我试过这样的想法 public AudioListPage() { InitializeComponent(); Back
LongListboxSelector
制作一个显示音频曲目的页面。每个项目都有带有艺术家和标题信息的按钮
“播放”和文本块。我正在使用BackgroundAudioPlayer
。我的愿望是使下一个:播放曲目时,按钮的图像是“暂停”,当曲目结束时,使其按钮
图像“播放”,并使下一首曲目的按钮
图像“暂停”(下一首曲目自动播放)。我试过这样的想法
public AudioListPage()
{
InitializeComponent();
BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged;
}
void Instance_PlayStateChanged(object sender, EventArgs e)
{
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Stopped)
{
IEnumerable buttons = AudioLLS.Descendants<Coding4Fun.Phone.Controls.RoundButton>();
var test = new BitmapImage(new Uri(@"/Images/appbar.transport.pause.rest.png", UriKind.Relative));
foreach (Coding4Fun.Phone.Controls.RoundButton item in buttons)
{
if (item.ImageSource == test)
{
MessageBox.Show("in new image");
item.ImageSource = new BitmapImage(new Uri(@"/Images/appbar.transport.play.rest.png", UriKind.Relative));
buttons.GetEnumerator().MoveNext();
item.ImageSource = new BitmapImage(new Uri(@"/Images/appbar.transport.pause.rest.png", UriKind.Relative));
}
}
}
}
public AudioListPage()
{
初始化组件();
BackgroundAudioPlayer.Instance.PlayStateChanged+=Instance_PlayStateChanged;
}
无效实例\u PlayStateChanged(对象发送方,事件参数e)
{
if(BackgroundAudioPlayer.Instance.PlayerState==PlayState.Stopped)
{
IEnumerable buttons=AudioLLS.subjects();
var test=new-BitmapImage(新Uri(@“/Images/appbar.transport.pause.rest.png”,UriKind.Relative));
foreach(在按钮中编码4fun.Phone.Controls.RoundButton项)
{
如果(item.ImageSource==测试)
{
MessageBox.Show(“在新图像中”);
item.ImageSource=新的位图图像(新的Uri(@“/Images/appbar.transport.play.rest.png”,UriKind.Relative));
buttons.GetEnumerator().MoveNext();
item.ImageSource=新的位图图像(新的Uri(@“/Images/appbar.transport.pause.rest.png”,UriKind.Relative));
}
}
}
}
PlayState.Stopped
在曲目结束时激发,下一个将开始。
我尝试使用IEnumerable buttons=AudioLLS.substands()
中的LinqToVisualTree
helper获取LLS中的所有按钮,并将它们的图像与test
进行比较。但正如我所看到的,我做错了,因为在blockif(item.ImageSource==test)
中,我的应用程序从未出现过。请告诉我,我做错了什么?如果我解决问题的方法不好,请告诉我如何用简单的方法解决。我在音频模型中添加了两个属性
private bool isPlaing = false;
public bool IsPlaing
{
get { return isPlaing; }
set
{
isPlaing = value;
OnPropertyChanged("IsPlaing");
if (IsPlaing) Image = @"/Images/appbar.transport.pause.rest.png";
else if (!IsPlaing) Image = @"/Images/appbar.transport.play.rest.png";
}
}
private string image = @"/Images/appbar.transport.play.rest.png";
public string Image { get { return image; } set { image = value; OnPropertyChanged("Image"); } }
在playerStateChanged
中,我添加了
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Stopped)
{
for (int i = 0; i < AudioList.Count; i++)
{
if (AudioList[i].Url1 == currTrack)
{
AudioList[i].IsPlaing = false;
currTrack = AudioList[i + 1].Url1;
AudioList[i + 1].IsPlaing = true;
break;
}
}
}
BackgroundAudioPlayer.Instance.Play();
}
if(BackgroundAudioPlayer.Instance.PlayerState==PlayState.Stopped)
{
对于(int i=0;i
就这样我解决了我的问题。正如我所看到的,一切都很好。没有XAML代码,很难说出任何事情。你没有把代码贴在你真正开始或停止播放器的地方。