在Windows Phone中使用c#检测按钮上下事件

本文关键字:检测 按钮 上下 事件 Windows Phone | 更新日期: 2023-09-27 17:50:10

我正在学习用c#和XAML编程Windows Phone应用程序。然而,我有问题检测按钮向下/向上事件。

My XAML code

<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>

和c#代码

    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;
    }

发生的事情是图像green.png在启动时没有加载。当应用程序运行时,我点击了按钮,红色。png被加载,但它被拉伸到按钮的边界之外。

在Windows Phone中使用c#检测按钮上下事件

尝试使用Click events而不是mouse down。电话上没有鼠标

<Button x:Name="buttonMain" Click="buttonMain_MouseLeftButtonDown" Grid.Row="1" Margin="74,240,84,117">
    <Button.Content>
        <Image x:Name="theImage" Stretch="Uniform" Source="/Images/green.png"/>
    </Button.Content>
</Button>