Windows phone 8 如何在xaml.cs中更改wp8中椭圆的颜色
我正在尝试创建一个windows phone 8应用程序。在此应用程序中,我在一个页面上有4个椭圆,我希望它们每15秒根据某个整数的值更改颜色。我已经把计时器的部件拆了,但我一直在想如何让它们改变颜色。我真的很感谢你的帮助 我在谷歌上搜索了很多,但找不到明确的解决方案。我是应用程序新手,请告诉我每一步 下面是我在xaml中创建它们的地方:Windows phone 8 如何在xaml.cs中更改wp8中椭圆的颜色,windows-phone-8,colors,Windows Phone 8,Colors,我正在尝试创建一个windows phone 8应用程序。在此应用程序中,我在一个页面上有4个椭圆,我希望它们每15秒根据某个整数的值更改颜色。我已经把计时器的部件拆了,但我一直在想如何让它们改变颜色。我真的很感谢你的帮助 我在谷歌上搜索了很多,但找不到明确的解决方案。我是应用程序新手,请告诉我每一步 下面是我在xaml中创建它们的地方: <Ellipse x:Name="l1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100
<Ellipse x:Name="l1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="42,339,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2"/>
<Ellipse x:Name="l2" Grid.Column="1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="111,339,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
<Ellipse x:Name="l3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="42,471,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2"/>
<Ellipse x:Name="l4" Grid.Column="1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="111,471,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
尝试实现这一点
yourEllipsesName.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255(R), 255(G), 255(B)));
有了它,您可以以RGB格式传递颜色代码。希望这有帮助。提供的是最简单的(+1),在大多数情况下已经足够了
当你开始学习时,你可能想了解一些关于。在这种情况下,您的代码可能如下所示:
在XAML中:
<Ellipse x:Name="l1" Fill="{Binding FirstBrush}" HorizontalAlignment="Left" Height="100" Margin="42,339,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2"/>
// other similar
你肯定会发现很多博客、教程等等,所以我不会在这里发布所有内容。只需说几句话——让它发挥作用最重要的是:
- 如您所见,在XAML中,定义
to属性(名称非常重要)Binding
- 您的
需要实现页面
-事件,并且您应该提供触发事件的方法INotifyPropertyChanged
- 设置
页面的
DataContext
- 绑定到的属性必须是具有合适getter的公共属性(对于单向绑定)
对我的解决方案和@Vyas_27有一点意见-如果您的
计时器在UI上运行(那么它可能是Dispatchermer
),这将起作用。如果在其他线程上运行计时器,那么必须通过调度程序调用的颜色更改
我应该将其添加到xaml或xaml.cs中吗?如果是在xaml中,那么我应该在cs代码中写入什么?就像我说的,我在这方面是新手,所以要非常明确。谢谢你的帮助。这段代码是C代码,所以它放在.cs文件中。它应该从你的计时器中调用。是的@ErtayShashko是正确的,这是一个c代码,应该从你的计时器中调用。(+1)只是一个备注-取决于OP的计时器的类型(如果它的dispatchermer
(可能是OP正在启动)或其他),那么可能需要通过Dispatcher
(如果不是Dispatchermer
)。
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseProperty(string propName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName)); }
private Color firstColor = Color.FromArgb(0xFF, 0xF4, 0xF4, 0xF5);
public Color FirstColor
{
get { return firstColor; }
set { firstColor = value; RaiseProperty("FirstBrush"); }
}
public SolidColorBrush FirstBrush { get { return new SolidColorBrush(firstColor); } }
public MainPage()
{
InitializeComponent();
this.DataContext = this; // set the DataContext so binding can work
}
// Then you change the color like this, and the color of your ellipse is updated
FirstColor = Color.FromArgb(255, 120, 120, 0);
}