C# 尝试从xaml.cs调用静态方法
对于我的大学项目,我必须建立一个银行应用程序,作为应用程序的一部分,我们必须根据一个人的收入和年龄确定他是否有资格开立银行账户。到目前为止,我得到了:C# 尝试从xaml.cs调用静态方法,c#,wpf,xaml,C#,Wpf,Xaml,对于我的大学项目,我必须建立一个银行应用程序,作为应用程序的一部分,我们必须根据一个人的收入和年龄确定他是否有资格开立银行账户。到目前为止,我得到了: public static bool AllowedStatus(decimal income, int age) { if ((income > minIncome) && (age > minAge)) { return true; }
public static bool AllowedStatus(decimal income, int age)
{
if ((income > minIncome) && (age > minAge))
{
return true;
}
else
{
return false;
}
}
在xaml.cs文件中,我得到:
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
if (activeAccount.AllowedStatus() == true)
{
//ACCOUNT CREATED MESSAGE
}
else
{
//ACCOUNT INVALID MESSAGE
}
}
但我一直收到消息“方法'AllowedStatus'没有重载”接受0个参数
如何在CreateAccountButton中获取if语句?单击以检查AllowedStatus方法中的bool,以便执行语句的其余部分
谢谢,
Tom您已将方法定义为具有两个参数:AllowedStatus(十进制收入,整数)。在CreateCountButton\u单击中调用方法时,必须提供这两个参数
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
if (activeAccount.AllowedStatus(10000, 20) == true) **<-- You have to provide values here**
{
//ACCOUNT CREATED MESSAGE
}
else
{
//ACCOUNT INVALID MESSAGE
}
}
private void CreateAccountButton\u单击(对象发送者,路由目标)
{
如果(activeAccount.AllowedStatus(10000,20)==true)**如果要从文本框中传递小数,则可以使用:
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
decimal income;
int age;
if (decimal.TryParse(yourIncomeTextBox.Text, out income)
&& int.TryParse(yourAgeTextBox.Text, out age)
&& activeAccount.AllowedStatus(income, age))
{
//ACCOUNT CREATED MESSAGE
}
else
{
//ACCOUNT INVALID MESSAGE
}
}
在CreateAccountButton\u Click处理程序中,您应该从哪里获取收入和年龄?这是来自表单上的其他控件吗?您将方法声明为AllowedStatus(十进制收入,整数年龄)
。显然,您应该在activeAccount.AllowedStatus()中向其传递一些参数
@Andy Korneyev当我尝试这样做时,它显示了一条消息“无效的表达式术语‘decimal’”。我想从用户在文本框中输入的任何内容中输入参数。@TomAtkinson您传递给收入变量的值是多少?根据您在这里和下面答案下面的评论,我投票以“1”结束这个问题不清楚你在问什么。”这很好,但我如何从应用程序的文本框中输入这些参数呢interface@TomAtkinson这是另一个问题。事实上,这是一个“去阅读WPF的教程”“问题。@TomAtkinson有一个方法FindName
,您可以使用它按名称查找控件。然后将其转换为Textbox和.Text prop。在xaml中指定文本框的name属性。然后在代码隐藏中,而不是传递AllowedStatus(10000,20)
传入AllowedStatus((convert.ToDecimal(textbox1name.Text),Convert.ToInt16(textbox2name.Text))
谢谢。我必须先解析文本框,但这已经解决了我的问题。