繁体   English   中英

使用C#更改XAML中按钮的图像

[英]Changing the image of a button in XAML using C#

我最近开始使用C#学习windows phone 8.1开发。 直到上周我还没有C#或XAML的经验。

我正在尝试更改按钮中的图像,如下所示(XAML)

<Button x:Name="playButton"
                    VerticalAlignment="Stretch"
                    Click="Button_Click">
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="controlImg" Source="/Assets/Media-Play.png" />
                    <TextBlock Text=" Play" />
                </StackPanel>
            </Button>

初始图像是一个“播放”图标和一个表示播放的文本。 这适用于正常工作。 但是,当按钮被单击为“暂停”图标和显示暂停的文本时,我想更改该图像。

到目前为止,这是我的C#代码

    private mediaState medState = mediaState.Stopped;

    public BitmapImage iconStop = new BitmapImage(new Uri("ms-appx:///Assets/Media-Stop.png", UriKind.RelativeOrAbsolute));
    public BitmapImage iconPause = new BitmapImage(new Uri("ms-appx:///Assets/Media-Pause.png", UriKind.RelativeOrAbsolute));
    public BitmapImage iconPlay = new BitmapImage(new Uri("ms-appx:///Assets/Media-Play.png", UriKind.RelativeOrAbsolute));


    private void Button_Click(object sender, RoutedEventArgs e)
    {


        switch (medState)
        {
            case mediaState.Playing:
                medState = mediaState.Paused;
                mediaE.Pause();
                playButton.Content = " Play";
                controlImg.Source = this.iconPlay;
                break;

            case mediaState.Paused:
                medState = mediaState.Playing;
                mediaE.Play();
                playButton.Content = " Pause";
                controlImg.Source = this.iconPause;
                break;

            case mediaState.Stopped:                    
                medState = mediaState.Playing;
                mediaE.Play();
                playButton.Content = " Pause";
                controlImg.Source = this.iconPause;
                break;
            default:
                break;
        }


    }

    public enum mediaState
    {
        Playing,
        Paused,
        Stopped
    }

mediaE是mediaElement对象。 当前单击该按钮时,文本会更改,但按钮中的图像不会消失

我到底做了什么:

我检查了MSDN文章中使用BitmapImage和其他一些问题,通常会这样说

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

但是我的visual studio甚至没有将BitmapImage.BeginInit()识别为有效函数。

我还检查了这个问题: BitmapImage缺少BeginInit()和EndInit()函数? 答案说,该类没有以同样的方式实现Windows手机。 查看该课程的Windows手机文档( http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.media.imaging.bitmapimage%28v=vs.105%29.aspx )没有任何明确的方式来实现我想要的东西。

注意:我正在为Windows Phone 8.1开发。 请任何帮助将不胜感激。 谢谢

事情在Windows手机运行时应用程序中表现得很奇怪可能是因为我们没有过多地探索它。 但是会这样做。

我也尝试过使用Binding的东西,这也显示了更新中的一些问题,所以我提出的不是一个好的解决方案,而是一个有效的解决方案。 当我完成这项工作时,我将在这里更新一个很好的解决方案。

解决方案 : -

更新你的xaml,因为你不应该使用ContentTemplate来改变任何控件的显示。 并检查控件和内容模板之间的区别。

Xaml: -

    <Button x:Name="PlayButton" Grid.Row="1"
                Click="PlayButton_OnClick" >
        <Button.ContentTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="ControlImg" 
                   Stretch="Fill" Width="100"
                   />
                    <TextBlock Text=" Play" />
                </StackPanel>
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>

通过添加一些代码更改您的Button Click事件 : -

 private async void PlayButton_OnClick(object sender, RoutedEventArgs e)
    {
        var btn = sender as Button;
        // For TextBlock change the children collection index to 1
        var img = (Image)(btn.ContentTemplateRoot as StackPanel).Children[0];

        switch (medState)
        {
            case mediaState.Playing:
                medState = mediaState.Paused;
             //   mediaE.Pause();
                PlayButton.Content = " Play";
                img.Source = this.iconPlay;
                break;

            case mediaState.Paused:
                medState = mediaState.Stopped;
               // mediaE.Play();
                PlayButton.Content = " Pause";
                img.Source = this.iconPause;
                break;

            case mediaState.Stopped:
                medState = mediaState.Playing;
               // mediaE.Play();
                PlayButton.Content = " Stop";
                img.Source = this.iconStop;
                break;
            default:
                break;
        }
    }

对于Windows手机SilverLight,我没有删除此信息,因为它很有用。

Here I used the Windows phone 8.1 Silverlight app template

您可以检查ImageFailed EventHandler ExceptionRoutedEventArgs e参数,然后您将看到与Silverlight cross-scheme security问题相关的“ AG_E_NETWORK_ERROR ”。 你可以谷歌AG_E_NETWORK_ERROR然后你会得到很多相关的东西。

解决方案代码 : -

Change images

public BitmapImage iconStop = new BitmapImage();
public BitmapImage iconPause = new BitmapImage();
public BitmapImage iconPlay = new BitmapImage();

private void PlayButton_OnClick(object sender, RoutedEventArgs e)
{
    switch (medState)
    {
        case mediaState.Playing:
            medState = mediaState.Paused;
            //  mediaE.Pause();
            playButton.Content = " Play";

            var sri = Application.GetResourceStream(new Uri("Assets/download(1).jpg", UriKind.Relative));
            iconStop.SetSource(sri.Stream);
            ControlImg.Source = iconStop;
            break;

        case mediaState.Paused:
            medState = mediaState.Stopped;
            //  mediaE.Play();
            playButton.Content = " Pause";
            var sri1 = Application.GetResourceStream(new Uri("Assets/download(2).jpg", UriKind.Relative));
            iconPlay.SetSource(sri1.Stream);
            ControlImg.Source = iconPlay;
            break;

        case mediaState.Stopped:
            medState = mediaState.Playing;
            // mediaE.Play();
            playButton.Content = " stopped";
            var sri2 = Application.GetResourceStream(new Uri("Assets/download(1).jpg", UriKind.Relative));
            iconStop.SetSource(sri2.Stream);
            ControlImg.Source = iconStop;
            break;
        default:
            break;
    }

希望它会对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM