2017-05-13 16 views
0

を応答するために取得する方法を私は実際に動作するように私のコードの大部分を得ました。私は入力を感謝し、それは正しい方向に私を指摘した。私が今問題を抱えているのは、ボタンを最初のクリックで動作させることです。C#&XAMLボタンが最初のクリックで

私は私が私のコードを改善する任意の入力に感謝。

以下

は私のXAML(MainPage.xamlを)& C#(MainPage.xaml.cs)コードです。ここで

<Page 
x:Class="Calculator_Application.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Calculator_Application" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

<Grid> 
    <TextBlock x:Name="textBlock_title" TextWrapping="Wrap"  Text="Calculator Application" VerticalAlignment="Top" Height="58" Width="252" Margin="86,30,0,0" SelectionChanged="textBlock_SelectionChanged" HorizontalAlignment="Left" FontSize="24" RenderTransformOrigin="-0.039,0.549" FontFamily="Calibri" Foreground="#FFBCB7B6"/> 
    <Button x:Name="button_info" Content="Information" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="135,561,0,0" Click="button_Click" FontFamily="Global User Interface" Foreground="#FFF05D5D"/> 
    <TextBox x:Name="textBox_number1" HorizontalAlignment="Left" Margin="29,191,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="textBox_number1_TextChanged"/> 
    <TextBox x:Name="textBox_number2" HorizontalAlignment="Left" Margin="29,387,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/> 
    <TextBlock x:Name="textBlock_info" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Please input numbers to calculate:" VerticalAlignment="Top" Height="30" Width="252" FontSize="16" FontStyle="Italic" Margin="29,134,0,0" SelectionChanged="textBlock_info_SelectionChanged" Foreground="#FFE5957F"/> 
    <Button x:Name="button_add" Content="+" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="29,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39" Background="Black"/> 
    <Button x:Name="button_subtract" Content="-" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="106,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/> 
    <Button x:Name="button_multiply" Content="*" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="179,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/> 
    <Button x:Name="button_divide" Content="/" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="257,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/> 
    <TextBlock x:Name="textBlock_equals" HorizontalAlignment="Left" TextWrapping="Wrap" Text="=" VerticalAlignment="Top" Height="39" Width="38" Margin="164,387,0,0" FontSize="36"/> 
    <TextBlock x:Name="textBlock_answer" HorizontalAlignment="Left" TextWrapping="Wrap" Text="answer" VerticalAlignment="Center" Height="23" Width="139" Margin="207,387,0,230" FontSize="22"/> 
</Grid> 
</Page> 

はあなたがボタン情報ボタン用に構成されたイベントハンドラを持つC#

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
// Added to ensure popup is availible 
using Windows.UI.Popups; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641 


namespace Calculator_Application 
{ 
/// <summary> 
/// An empty page that can be used on its own or navigated to within a Frame. 
/// </summary> 
public sealed partial class MainPage : Page 
{ 
    private double valHolder1 = 0; 
    private double valHolder2 = 0; 
    private double answer = 0; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     this.NavigationCacheMode = NavigationCacheMode.Required; 
    } 

    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. 
    /// This parameter is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     // TODO: Prepare page for display here. 

     // TODO: If your application contains multiple pages, ensure that you are 
     // handling the hardware Back button by registering for the 
     // Windows.Phone.UI.Input.HardwareButtons.BackPressed event. 
     // If you are using the NavigationHelper provided by some templates, 
     // this event is handled for you. 
    } 

    private void textBlock_SelectionChanged(object sender, RoutedEventArgs e) 
    { 

    } 



    private void textBlock_info_SelectionChanged(object sender, RoutedEventArgs e) 
    { 

    } 

    private async void button_Click(object sender, RoutedEventArgs e) 
    { 
     //Creating instance for the MessageDialog Class 
     //and passing the message in it's Constructor 
     MessageDialog msgbox = new MessageDialog("Page Lynn Potter, MBL 410, 05-15-2017, Robin Deitsch"); 
     //Calling the Show method of MessageDialog class 
     //which will show the MessageBox 
     await msgbox.ShowAsync(); 
    } 

    private void textBox_number1_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 


    private void button_add_Click(object sender, RoutedEventArgs e) 
    { 
     //Make sure out text box has a value 
     if (textBox_number1.Text.Length != 0) 
     { 
      //Assign the value in text box to holder 
      valHolder1 = System.Double.Parse(textBox_number1.Text); 
      //Empty the text box   
      textBox_number1.Text = string.Empty; 

      if (textBox_number2.Text.Length != 0) 
      { 
       //Assign the value in text box to holder 
       valHolder2 = System.Double.Parse(textBox_number2.Text); 
       //Empty the text box   
       textBox_number2.Text = string.Empty; 
      } 
     } 

     else 
     { 
      //Calculate answer 
      answer = valHolder1 + valHolder2; 
      //Assign answer to text block 
      textBlock_answer.Text = answer.ToString(); 
     } 
    } 

    private void button_subtract_Click(object sender, RoutedEventArgs e) 
    { 
     //Make sure out text box has a value 
     if (textBox_number1.Text.Length != 0) 
     { 
      //Assign the value in text box to holder 
      valHolder1 = System.Double.Parse(textBox_number1.Text); 
      //Empty the text box   
      textBox_number1.Text = string.Empty; 

      if (textBox_number2.Text.Length != 0) 
      { 
       //Assign the value in text box to holder 
       valHolder2 = System.Double.Parse(textBox_number2.Text); 
       //Empty the text box   
       textBox_number2.Text = string.Empty; 
      } 
     } 

     else 
     { 
      //Calculate answer 
      answer = valHolder1 - valHolder2; 
      //Assign answer to text block 
      textBlock_answer.Text = answer.ToString(); 
     } 
    } 

    private void button_multiply_Click(object sender, RoutedEventArgs e) 
    { 
     //Make sure out text box has a value 
     if (textBox_number1.Text.Length != 0) 
     { 
      //Assign the value in text box to holder 
      valHolder1 = System.Double.Parse(textBox_number1.Text); 
      //Empty the text box   
      textBox_number1.Text = string.Empty; 

      if (textBox_number2.Text.Length != 0) 
      { 
       //Assign the value in text box to holder 
       valHolder2 = System.Double.Parse(textBox_number2.Text); 
       //Empty the text box   
       textBox_number2.Text = string.Empty; 
      } 
     } 

     else 
     { 
      //Calculate answer 
      answer = valHolder1 * valHolder2; 
      //Assign answer to text block 
      textBlock_answer.Text = answer.ToString(); 
     } 
    } 

    private void button_divide_Click(object sender, RoutedEventArgs e) 
    { 
     //Make sure out text box has a value 
     if (textBox_number1.Text.Length != 0) 
     { 
      //Assign the value in text box to holder 
      valHolder1 = System.Double.Parse(textBox_number1.Text); 
      //Empty the text box   
      textBox_number1.Text = string.Empty; 

      if (textBox_number2.Text.Length != 0) 
      { 
       //Assign the value in text box to holder 
       valHolder2 = System.Double.Parse(textBox_number2.Text); 
       //Empty the text box   
       textBox_number2.Text = string.Empty; 
      } 
     } 

     else 
     { 
      //Calculate answer 
      answer = valHolder1/valHolder2; 
      //Assign answer to text block 
      textBlock_answer.Text = answer.ToString(); 
      } 
     } 
    } 
} 
+0

スタックオーバーフローは、ここで必要なチュートリアル情報の適切なソースではありません。 WPF APIについて説明している多くのチュートリアルとその使い方を読んでください。あなたの最初のステップは、表示するためのUI要素のプロパティにバインドされたパブリックプロパティを持つ "ビューモデル"データ構造を使用して、データをUIから分離して快適に保つことです。 'ICommand'、' Button.Command'プロパティを実装してバインドする方法、電卓のボタンのユーザ入力を処理する方法についても学びたいと思うでしょう。 –

+0

MVVMの使用をお勧めします。あなたが興味があれば教えてください。 – Coding4Fun

+0

としては、あなたがMVVM、WPF、INotifyPropertyChangedの、のICommandを使用している場合、それは良いだろう、上記と述べました。コードにジャンプする前に読む必要があることはほとんどありません。 –

答えて

0

です。

<Button x:Name="button_info" Content="Information" 
     HorizontalAlignment="Left" VerticalAlignment="Top" 
     Margin="135,561,0,0" 
     FontFamily="Global User Interface" Foreground="#FFF05D5D" 
     Click="button_Click"/> 

他のボタンのハンドラをどこで設定したのかわかりません。

XAMLでこのような何かがあるはずです。

<Button x:Name="button_add" Click = "button_add_Click" /> 
<Button x:Name="button_subtract" Click = "button_subtract_Click" /> 
<Button x:Name="button_multiply" Click = "button_multiply_Click" /> 
<Button x:Name="button_divide"Click = "button_divide_Click" /> 
+0

ありがとうございます、私は先に進みイベントハンドラを追加します。私はそれらを入れることを忘れてしまって、それらを完全に見落としたのです。私は助けに感謝します。 – Potter

+0

私は二重の私のコードをチェックし、イベントハンドラが存在することになりますが、ボタンはまだそれが回答TextBlock内の私の答えを置くために二回クリックする必要があります。私はエミュレータを使用しているか、まだ何かを逃してしまったからですか? – Potter

関連する問題