我对编程很陌生。我目前正在尝试创建将计算number1的平方根等的WPF应用程序。我在使用计算平方根(而不仅仅是)的方法时遇到问题。我收到一个错误说:
没有任何论据符合所需的形式参数' sender' ' MainWindow.RadioBT_Root2_Checked(object,RoutedEventArgs)'
RadioBT_Root3_Checked和RadioBT_Root4_Checked发生此错误,因为我有3种方法。
其代码为CS7036。我做得不好?我搜索的很多,但我无法找到它(或者我无法搜索)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Examples {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow: Window {
public MainWindow() {
InitializeComponent();
}
double number1;
double number2;
double result;
private void TXB_1_TextChanged(object sender, TextChangedEventArgs e) {
}
private void TXB_2_TextChanged(object sender, TextChangedEventArgs e) {
}
private void BT_Potegowanie_Click(object sender, RoutedEventArgs e) {
try {
number1 = double.Parse(TXB_1.Text);
number2 = double.Parse(TXB_2.Text);
result = Math.Pow(number1, number2);
MessageBox.Show("WYNIK: " + result.ToString());
} catch (Exception) {
MessageBox.Show("Wystąpił błąd.", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void BT_Root_Click(object sender, RoutedEventArgs e) {
if (RadioBT_Root2_Checked()) ///CS7036
MessageBox.Show("RESULT: " + result.ToString());
if (RadioBT_Root3_Checked()) ///CS7036
MessageBox.Show("RESULT: " + result.ToString());
if (RadioBT_Root4_Checked()) ///CS7036
MessageBox.Show("RESULT: " + result.ToString());
}
public void RadioBT_Root2_Checked(object sender, RoutedEventArgs e) {
number1 = double.Parse(TXB_1.Text);
result = Math.Sqrt(number1);
}
public void RadioBT_Root3_Checked(object sender, RoutedEventArgs e) {
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 3.0);
result = Math.Pow(number1, number2);
}
public void RadioBT_Root4_Checked(object sender, RoutedEventArgs e) {
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 4.0);
result = Math.Pow(number1, number2);
}
}
}
编辑:我已经完成了Ed Plunkett所吮吸的事情,而我现在又遇到了另一个问题。它与XAML有关(我认为)。我还发现我可能需要放置&#39; ??假&#39;在某些地方。错误列表显示:&#39; MainWindow&#39;不包含&#39; RadioBT_Root4_Checked&#39;的定义没有扩展方法&#39; RadioBT_Root4_Checked&#39;接受类型&#39; MainWindow&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Examples
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TXB_1_TextChanged(object sender, TextChan`enter code here`gedEventArgs e)
{
}
private void TXB_2_TextChanged(object sender, TextChangedEventArgs e)
{
}
public void BT_Root_Click(object sender, RoutedEventArgs e)
{
double number1 = 0;
double number2 = 0;
double result = 0;
if (RadioBT_Root2.IsChecked ?? false) ///CS0266
{
number1 = double.Parse(TXB_1.Text);
result = Math.Sqrt(number1);
}
else if (RadioBT_Root3.IsChecked ?? false) ///CS0266
{
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 3.0);
result = Math.Pow(number1, number2);
}
else if (RadioBT_Root4.IsChecked ?? false) ///CS0266
{
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 4.0);
result = Math.Pow(number1, number2);
}
MessageBox.Show("RESULT: " + result.ToString());
}
}
我的XAML代码:
<Window x:Class="Examples.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Examples"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label x:Name="label" Content="First number

First number" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Width="117" Height="31" FontSize="15"/>
<TextBox x:Name="TXB_1" HorizontalAlignment="Left" Height="28" Margin="132,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" FontSize="15" TextChanged="TXB_1_TextChanged"/>
<Label x:Name="label1" Content="Second number

Second number
" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="117" Height="30" FontSize="15"/>
<TextBox x:Name="TXB_2" HorizontalAlignment="Left" Height="28" Margin="132,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TXB_2_TextChanged" FontSize="15"/>
<Separator HorizontalAlignment="Left" Height="19" Margin="0,69,0,0" VerticalAlignment="Top" Width="517"/>
<Button x:Name="BT_Root" Content="ROOT" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" Width="128" Height="48" FontSize="16" Click="BT_Root_Click"/>
<RadioButton x:Name="RadioBT_Root2" Content="Root2" HorizontalAlignment="Left" Margin="143,81,0,0" VerticalAlignment="Top" Checked="RadioBT_Root2_Checked"/>
<RadioButton x:Name="RadioBT_Root3" Content="Root3" HorizontalAlignment="Left" Margin="143,96,0,0" VerticalAlignment="Top" Checked="RadioBT_Root3_Checked"/>
<RadioButton x:Name="RadioBT_Root4" Content="Root4" HorizontalAlignment="Left" Margin="143,111,0,0" VerticalAlignment="Top" Checked="RadioBT_Root4_Checked"/>
</Grid>
</Window>
&#13;
我也试过更改&#39; Checked =&#34; RadioBT_Root4_Checked&#34; /&gt;&#39; to&#39; Checked =&#34; RadioBT_Root4.IsChecked&#34; /&gt;&#39;。它说&#39;错误检查=&#34; RadioBT_Root2.IsChecked&#34;无效。 &#39; RadioBT_Root2.IsChecked&#39;不是有效的事件处理程序方法名称。只有生成的或代码隐藏类的实例方法才有效。&#39;
答案 0 :(得分:2)
您正在调用事件处理程序 - 例如RadioBT_Root2_Checked
- 没有参数,即使它们需要两个参数。你也表现得像他们“回归”bool
,即使他们“没有”回归。
“回归”是什么意思? 1 + 3
返回4
:
int x = 1 + 3;
现在x
等于4.
函数可以返回值:
public bool GreaterThan(int x, int y) { return x > y; }
像这样使用:
if (GreaterThan(10, 9))
{
MessageBox.Show("10 is greater than 9");
}
但我认为你真的不想打电话给那些方法。我想这就是你想要的:
单击Root时,我认为您想要找出用户单击的复选框,然后显示该结果。选中复选框后,计算结果;这些函数是事件处理程序。要找出每个复选框的IF,请查看复选框对象 - 使用您给出的名称 - 并查看其IsChecked
属性。
像这样:
public void BT_Root_Click(object sender, RoutedEventArgs e)
{
if (RadioBT_Root2.IsChecked)
MessageBox.Show("RESULT: " + result.ToString());
else if (RadioBT_Root3.IsChecked)
MessageBox.Show("RESULT: " + result.ToString());
else if (RadioBT_Root4.IsChecked)
MessageBox.Show("RESULT: " + result.ToString());
}
除了,看看那些if语句:这三个人都在做同样的事情。您可以进一步简化:
public void BT_Root_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("RESULT: " + result.ToString());
}
但这是一个更好的方法:
public void BT_Root_Click(object sender, RoutedEventArgs e)
{
double number1 = 0;
double number2 = 0;
double result = 0;
if (RadioBT_Root2.IsChecked)
{
number1 = double.Parse(TXB_1.Text);
result = Math.Sqrt(number1);
}
else if (RadioBT_Root3.IsChecked)
{
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 3.0);
result = Math.Pow(number1, number2);
}
else if (RadioBT_Root4.IsChecked)
{
number1 = double.Parse(TXB_1.Text);
number2 = (1 / 4.0);
result = Math.Pow(number1, number2);
}
MessageBox.Show("RESULT: " + result.ToString());
}
您不需要result
,number
和number2
成为类成员,并且您根本不需要这些*_Checked
事件处理程序。