Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从MainWindow访问类中的方法_C#_Wpf - Fatal编程技术网

C# 从MainWindow访问类中的方法

C# 从MainWindow访问类中的方法,c#,wpf,C#,Wpf,我正在尝试创建一个WPF应用程序,用一个简单的表单来注册学生,表单中包含姓名、入学号、学分等等。 我有一个Mainwindow.xaml和一个Student.cs 在mymain window.xaml中,我有一个高级按钮,可以根据学分提高学生的等级(如果学生的学分超过120学分,等级应提高到“2”) 这是带有Advance()方法的Student.cs 当然没用了。。。 有人能帮我吗 编辑 这就是我现在拥有的,仍然不起作用 public void Advance() {

我正在尝试创建一个WPF应用程序,用一个简单的表单来注册学生,表单中包含姓名、入学号、学分等等。 我有一个
Mainwindow.xaml
和一个
Student.cs

在my
main window.xaml中,我有一个高级按钮,可以根据学分提高学生的等级(如果学生的学分超过120学分,等级应提高到“2”)

这是带有Advance()方法的Student.cs

当然没用了。。。 有人能帮我吗

编辑 这就是我现在拥有的,仍然不起作用

    public void Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            Level = 1;
        }
        else if (credits >= 120 && Level == 1)
        {
            Level = 2;
        }
        else if (credits >= 240 && Level == 2)
        {
            Level = 3;
        }
        else if (Credits >= 360 && Level == 3)
        {
            Level = 4;
        }
        else if (Level == 4)
        {
            MessageBox.Show("You can't advance more!");
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }
    }
public void Advance()
{
如果(学分>=0&&学分<120)
{
级别=1;
}
否则如果(学分>=120&&Level==1)
{
级别=2;
}
否则如果(学分>=240&&Level==2)
{
级别=3;
}
否则如果(学分>=360&&Level==3)
{
级别=4;
}
否则,如果(级别==4)
{
MessageBox.Show(“你不能再前进了!”);
}
其他的
{
Show(“无法前进!”);
}
}

我认为最简单的方法是省略集合,然后让Btnavance同时做这两件事。问题是集合中的学生事先不存在,因为它是不同的往返


也就是说,Advance必须查看文本框并找出答案。

实际上,您应该通过绑定来实现这一点,将Level文本框绑定到学生的Level属性,在模型上实现iNotifyPropertyChanged。我建议你研究一下绑定,然后用这种方式重新设计它

但是,如果您希望继续当前的设计,我建议您进行以下更改,以实现您期望的行为:

1) 在
btnSet\u中单击
,删除此行:
student.Level=int.Parse(txtLevel.Text)文本框不应设置您的级别;应采用提前法进行设置

2) 您的高级方法应如下所示:

public int Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            level = 1;
        }
        else if (credits >= 120 && credits < 240)
        {
            level = 2;
        }
        else if (credits >= 240 && credits < 360)
        {
            level = 3;
        }
        else if (Credits >= 360)
        {
            if (level != 4)
            {
                level = 4;
            }
            else 
            {
                MessageBox.Show("You can't advance more!");
            }
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }

        return level;
    }
4) 由于您没有使用绑定,因此在您的“高级”单击中,您需要将返回的值发布回接口:

private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
    txtLevel.Text = student.Advance();              //this should be the call of the method        
}

您正在更新模型项,而不是将控件设置为更新后的值

您面临哪些错误?如果您希望UI反映
student
中的更改,则需要在调用
student.Advance()后更新
txtLevel
@crashmstr是正确的。您需要使用函数。
txtLevel.Text=student.Level.ToString()?这与您在
btnSet\u Click
中所做的操作正好相反(但您应该检查以确保
sudent!=null
,并且还需要考虑您何时创建
新学生以及如果他们继续单击您的
Advance
按钮会发生什么情况)。如果我正确理解您的代码,您还需要更新Advance方法逻辑,以便在条件语句中包含Level:else if(credits>=120&&Level==1)。否则,多次“高级”单击将继续增加级别,而不会将积分调整得更高。@Koosshh56我假设您希望您的级别文本框显示该值。在更新business对象(student)后,我在您的代码中看不到任何试图写回文本框的内容
    public void Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            Level = 1;
        }
        else if (credits >= 120 && Level == 1)
        {
            Level = 2;
        }
        else if (credits >= 240 && Level == 2)
        {
            Level = 3;
        }
        else if (Credits >= 360 && Level == 3)
        {
            Level = 4;
        }
        else if (Level == 4)
        {
            MessageBox.Show("You can't advance more!");
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }
    }
public int Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            level = 1;
        }
        else if (credits >= 120 && credits < 240)
        {
            level = 2;
        }
        else if (credits >= 240 && credits < 360)
        {
            level = 3;
        }
        else if (Credits >= 360)
        {
            if (level != 4)
            {
                level = 4;
            }
            else 
            {
                MessageBox.Show("You can't advance more!");
            }
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }

        return level;
    }
<TextBox x:Name="txtLevel" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
    txtLevel.Text = student.Advance();              //this should be the call of the method        
}