我下载了适用于 Windows Phone 的 Microsoft Visual Studio 2010 Express,并编写了一个简单的应用程序来对模拟器进行首次测试。在此应用程序中,我只有一个按钮,其属性 Content 绑定(bind)到名为 ButtonText 的字符串,属性 Background 绑定(bind)到名为 FillColor 的 SolidColorBrush。我用这段代码处理了 Click 事件:
void MyButton_Click(object sender, RoutedEventArgs e)
{
if (toggle == true)
{
ButtonText = "Blue";
FillColor = new SolidColorBrush(Colors.Blue);
}
else
{
ButtonText = "Red";
FillColor = new SolidColorBrush(Colors.Red);
}
toggle = !toggle;
}
不幸的是,这不起作用。虽然每次按下按钮时按钮的内容都会改变,但我不能对保持相同颜色的背景说同样的话。
你能告诉我哪里出了问题吗?谢谢。
我还发布了 XAML:
<Grid x:Name="ContentGrid" Grid.Row="1">
<Button Name="MyButton" Width="300" Height="300"
Content="{Binding Path=ButtonText}"
Background="{Binding Path=FillColor}" />
</Grid>
最佳答案
问题在于行中使用“new”:
FillColor = new SolidColorBrush(Colors.Blue);
使用“新建”操作会破坏之前设置的数据绑定(bind)。请尝试使用以下内容:
FillColor.Color = Colors.Blue;
同时替换对蓝色和红色的更改,这应该可以解决问题。
HTH!
克里斯
关于c# - Windows Phone 应用程序的首次测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3025906/