C# 使用C更改XAML中按钮的图像#
我最近开始学习使用C#开发windows phone 8.1。直到上周,我还没有C#或XAML的经验 我正在尝试更改按钮中的图像,如下图所示(XAML)C# 使用C更改XAML中按钮的图像#,c#,xaml,windows-phone-8,windows-runtime,windows-phone-8.1,C#,Xaml,Windows Phone 8,Windows Runtime,Windows Phone 8.1,我最近开始学习使用C#开发windows phone 8.1。直到上周,我还没有C#或XAML的经验 我正在尝试更改按钮中的图像,如下图所示(XAML) mediaE是mediaElement对象。 当前,当您单击按钮时,文本会更改,但按钮中的图像不会消失 到目前为止,我所尝试的: 我查看了MSDN文章中使用的BitmapImage和其他一些问题,他们通常会说这样的话 // Create source BitmapImage myBitmapImage = new BitmapImage();
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");
但是我的VisualStudio甚至不能将BitmapImage.BeginInit()
识别为有效函数
我还检查了这个问题:答案是该类在windows phone上的实现方式不同。查看该类的windows phone文档()并没有显示任何实现我想要的目标的明确方法
注意:我正在为windows phone 8.1开发。如果您有任何帮助,我们将不胜感激。谢谢在Windows phone运行时应用程序中,事情表现得很奇怪,可能是因为我们没有对它进行太多的探索。但我会做到的 我也尝试过使用绑定,这也显示了更新中的一些问题,所以我建议的不是一个好的解决方案,而是一个可行的解决方案。我将在这里更新一个很好的解决方案后,当我使这项工作 解决方案:- 更新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>
对于Windows phone SilverLight,我没有删除此信息,因为它很有用。
这里我使用了Windows phone 8.1 Silverlight应用程序模板
您可以检查ImageFailed EventHandlerExceptionRoutedEventTargets e
参数,然后您将看到与Silverlight
中的跨方案安全问题相关的“AG\u e\u NETWORK\u ERROR”。
你可以用谷歌搜索AG_E_NETWORK_ERROR
,然后你会得到很多与之相关的东西
解决方案代码:
更改图像
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;
}
希望它能帮助你 @taLhaKhan我尝试过它抛出了一个异常。请尝试此BitmapImage bm=new BitmapImage(新Uri(@“Assets/Media Stop.png”,UriKind.RelativeOrAbsolute))@我试过了。它也抛出了一个参数异常:给定的系统。URI不能转换成一个窗口。您是否尝试过:新位图图像(新Uri(“ms appx:/Assets/Media Play.png”,UriKind.RelativeOrAbsolute))<代码>应用程序。GetResourceStream()在我的命名空间中不存在。完整的定义是什么<代码>Windows.UI.XAML.Application?注意:我不是在使用
silverlight
您是在使用VS 2013制作windows phone 8.1应用程序,对吧。是的,我是在使用VS 2013为windows phone 8.1开发应用程序
<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>
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;
}
}
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;
}