在Windows Phone中使用C#检测按钮按下/打开事件

在Windows Phone中使用C#检测按钮按下/打开事件,c#,silverlight,windows-phone-7,xaml,C#,Silverlight,Windows Phone 7,Xaml,我正在学习用C#和XAML编写Windows Phone应用程序。但是,我在检测按钮按下/按下事件时遇到问题 我的XAML代码 <phone:PhoneApplicationPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-names

我正在学习用C#和XAML编写Windows Phone应用程序。但是,我在检测按钮按下/按下事件时遇到问题

我的XAML代码

<phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    x:Class="Tally2.MainPage"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeHuge}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"
    shell:SystemTray.IsVisible="True">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Row 0: The Header -->
        <StackPanel Grid.Row="0" Margin="24,24,0,12">
            <TextBlock Text="tap to count" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!-- Row 1: The text block containing the count -->
        <TextBlock x:Name="CountTextBlock" Grid.Row="1" TextAlignment="Center" Text="0" Height="259" VerticalAlignment="Top"/>

        <!-- Row 2: The reset button -->
        <Button x:Name="buttonMain" MouseLeftButtonDown="buttonMain_MouseLeftButtonDown" MouseLeftButtonUp="buttonMain_MouseLeftButtonUp" Grid.Row="1" Margin="74,240,84,117">
            <Button.Content>
                <Image x:Name="theImage" Stretch="Uniform" Source="/Images/green.png"/>
        </Button.Content>
            </Button>
    </Grid>
</phone:PhoneApplicationPage>

实际情况是,启动时未加载image green.png。当应用程序运行并且我单击按钮时,会加载red.png,但会一直拉伸到按钮边框之外。

尝试使用单击事件,而不是鼠标按下。电话上没有鼠标



我可以检测到点击,但是,当点击事件按下和释放按钮时,如何更改按钮图像?啊,那!实际上,您需要更改按钮模板。类似这样的事情:那个么并没有办法通过代码使用事件来检测这些事件,而是我必须使用按钮模板吗?我不熟悉模板:(好的,我会检查链接。只希望有一个更简单的方法。我不知道。按钮基本上有几种状态。你使用expression Blend编辑它们,然后Blend根据你修改的状态生成XAML(在Blend中)。然后你的按钮使用自定义模板进行操作。搜索google“混合修改按钮状态wpf教程”
    private void buttonMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Uri uri = new Uri("/Images/red.png", UriKind.Relative);
        BitmapImage imgSource = new BitmapImage(uri);
        theImage.Source = imgSource;
        theImage.Stretch = Stretch.Uniform;
    }

    private void buttonMain_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Uri uri = new Uri("/Images/green.png", UriKind.Relative);
        BitmapImage imgSource = new BitmapImage(uri);
        theImage.Source = imgSource;
        theImage.Stretch = Stretch.Uniform;
    }