c# - Windows Phone 应用程序的首次测试

标签 c# silverlight windows-phone-7

我下载了适用于 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/

相关文章:

c# - 在 C# 中为函数名称创建别名

c# - 将项目绑定(bind)到列表框

c# - 将音频文件流式传输到另一台计算机

windows-phone-7 - 如何检测对象类型和标签

windows-phone-7 - 您真的需要一部手机来开发 Windows Phone 7 应用程序吗?

c# - 检测用户何时从使用 EmailComposeTask 返回?

c# - int.TryParse 输出到一个对象数组

c# - 使用 Entity Framework 向模型添加方法

c# - object.ReferenceEquals 或 == 运算符?

c# - 提交试用版WP7应用