使用.cs

时间:2016-07-22 02:20:29

标签: c# wpf

我对C#和一般的编程还是比较新的,所以我不确定我是否能够正确地提出这个问题,但是这里有。我正在使用Visual Studio专门为C#编写一个类,我们给出的任务是使用教师提供的WPF应用程序,然后创建一个.cs文件来输入我们的代码以使应用程序正常运行。我已成功创建控制台应用程序,以及仅使用VB的WPF应用程序,但我不完全确定如何使这两个概念相互协作。

到目前为止,我的代码如下所示:

MainWindow.xaml(讲师提供):

<Window x:Class="CreateClassesObjs.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:CreateClassesObjs"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="66,37,0,0" VerticalAlignment="Top" Width="164" IsDropDownOpen="True"/>
    <Button x:Name="button" Content="Select this course" HorizontalAlignment="Left" Margin="283,39,0,0" VerticalAlignment="Top" Width="166" Click="button_Click"/>
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="69" Margin="66,233,0,0" VerticalAlignment="Top" Width="164"/>
    <Label x:Name="label" Content="Please select a course " HorizontalAlignment="Left" Margin="66,7,0,0" VerticalAlignment="Top" Width="383"/>
    <Label x:Name="label1" Content="You have selected these courses:" HorizontalAlignment="Left" Margin="66,202,0,0" VerticalAlignment="Top" Width="176"/>

</Grid>

MainWindow.xaml.cs(讲师提供):

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 CreateClassesObjs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    Course choice;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Course course1 = new Course();
        Course course2 = new Course();
        Course course3 = new Course();
        Course course4 = new Course();
        Course course5 = new Course();
        Course course6 = new Course();
        Course course7 = new Course();

        course1.setName("IT 145");
        course2.setName("IT 200");
        course3.setName("IT 201");
        course4.setName("IT 270");
        course5.setName("IT 315");
        course6.setName("IT 328");
        course7.setName("IT 330");



        this.comboBox.Items.Add(course1);
        this.comboBox.Items.Add(course2);
        this.comboBox.Items.Add(course3);
        this.comboBox.Items.Add(course4);
        this.comboBox.Items.Add(course5);
        this.comboBox.Items.Add(course6);
        this.comboBox.Items.Add(course7);
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        choice = (Course)(this.comboBox.SelectedItem);
        this.listBox.Items.Add(choice);
    }

}
}

和Course.cs(我开始研究的代码):

#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace CreateClassesObjs
{
public partial class Course :MainWindow
{
    //Field
    private string courseName;

    //Method to set courseName to string value
    public void setName(string newName)
    {
        courseName = newName;
    }

    //overrides string ToString
    /*public override string ToString()
    {
        // this method returns the name field
        Course course1 = new Course();
        Console.WriteLine(course1.courseName);

    }*/
}
}

我一直试图从我能找到的教程中把它拼凑起来,但我觉得我只是把它搞糊涂了。我不是在寻找一个完整的答案,而是在正确的方向上轻推。提前谢谢!

3 个答案:

答案 0 :(得分:0)

你真的很接近这个工作。您只需更改ToString方法即可。我猜你现在看到的是这样的事情:

example

如果你因为无法编译而没有看到这个,我建议为你的Course对象创建一个新的.cs文件,而不是让它嵌入你的MainWindow类。这里的partial课程毫无理由

WPF目前不知道如何在ComboBox中显示课程。在普通 WPF应用程序中,可以定义一个DataTemplate来告诉ComboBox如何呈现课程。由于你的教练所造成的憎恶,我们没有那么奢侈。相反,您需要在ToString方法中返回一些内容来安抚它。我会告诉你那是什么。

(请注意:这是如何不做WPF的一个主要例子,即使是在介绍级别。不是你的错。)

答案 1 :(得分:0)

我希望这会有所帮助,但这是我能够让程序运行的方式。您需要定义您的getter和setter以及您的新课程以打印出来。

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 CreateClassesObjs
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        Course choice;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Course course1 = new Course("IT 145");
            Course course2 = new Course("IT 200");
            Course course3 = new Course("IT 201");
            Course course4 = new Course("IT 270");
            Course course5 = new Course("IT 315");
            Course course6 = new Course("IT 328");
            Course course7 = new Course("IT 330");

            course1.setName("IT 145");
            course2.setName("IT 200");
            course3.setName("IT 201");
            course4.setName("IT 270");
            course5.setName("IT 315");
            course6.setName("IT 328");
            course7.setName("IT 330");

            this.comboBox.Items.Add(course1);
            this.comboBox.Items.Add(course2);
            this.comboBox.Items.Add(course3);
            this.comboBox.Items.Add(course4);
            this.comboBox.Items.Add(course5);
            this.comboBox.Items.Add(course6);
            this.comboBox.Items.Add(course7);
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            choice = (Course)(this.comboBox.SelectedItem);
            this.listBox.Items.Add(choice);
        }

        class Course
        {
            private string name = "";


            public Course(string name)
            {
                this.name = name;
            }

            public void setName(string name)
            {
                this.name = name;
            }

            public string getName()
            {
                return name;
            }

            public override string ToString()
            {
                return getName();
            }
        }
    }
}

答案 2 :(得分:0)

这是我的解决方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateClassesObjs
{
    class Course
    {
        private string ClassName { get; set; }

        public void setName(string selection)
        {
            ClassName = selection;
        }

        public override string ToString()
        {
            return ClassName.ToString();
        }

    }
}

由于我在同一条船上,他们仍在学校使用此工具。我可以根据这里的建议进行工作。我能提出的最好建议是,如果任何人找到此主题,请向学校提供有关该主题的反馈。这是课程可以更改以正确教会我们的唯一方法。