0

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>

そして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 がロードされません。アプリの実行中にボタン red.png をクリックすると、ロードされますが、ボタンの境界線の外側まで引き伸ばされます。

4

1 に答える 1

1

マウスダウンではなくクリックイベントを使用してみてください。電話にはマウスがありません。

<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>
于 2013-01-01T10:36:35.717 に答える