如何在触发某些事件时更改listboxitem中的按钮中的图像?

时间:2013-01-12 20:49:50

标签: c# silverlight windows-phone-7 button

我正在制作一个LongListboxSelector的页面,其中显示了录音带。每个项目都有Button“播放”,TextBlock包含艺术家和标题信息。我正在使用BackgroundAudioPlayer。我的愿望是下一个:当正在播放曲目时 - Button的图像为“暂停”,当曲目结束时 - 使其Button图像“播放”,以及使下一首曲目的Button图像“暂停”(下一首曲目会自动播放)。我尝试过这样的想法

    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));
                }

            }
        }
    }
在赛道结束时

PlayState.Stopped开始,接下来将开始。 我尝试使用LinqToVisualTree中的IEnumerable buttons = AudioLLS.Descendants<Coding4Fun.Phone.Controls.RoundButton>()帮助程序获取LLS中的所有按钮,并将其图像与test进行比较。但正如我所看到的 - 我做错了,因为在if(item.ImageSource == test)区块我的应用程序永远不会到来。请告诉我,我做错了什么?如果我解决问题的方法很糟糕 - 请告诉我如何以简单的方式做到这一点。

1 个答案:

答案 0 :(得分:0)

我在音频模型中添加了两个属性

    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();
        }

通过这种方式我解决了我的问题。我可以看到 - 一切正常。