C# C语言中的基本按钮问题#

C# C语言中的基本按钮问题#,c#,button,backup,C#,Button,Backup,谢谢你抽出时间来读这篇文章。我正在使用Visual Studio创建一个程序,使用Visual Studio 2017将几个闪存驱动器备份配置文件存储到C#GUI中的按钮。问题是,我遇到了一些我无法理解的错误,主要是因为这是我第一次在C#工作,还有一些隐藏在微软的代码 -第一个错误涉及控件。Add没有显示按钮,尽管每个在线来源都告诉我这是如何在主窗口的第78行添加按钮 -第二个涉及到我不知道如何在主窗口的第76行为按钮提供另一个类的方法 守则: Profile.cs using System;

谢谢你抽出时间来读这篇文章。我正在使用Visual Studio创建一个程序,使用Visual Studio 2017将几个闪存驱动器备份配置文件存储到C#GUI中的按钮。问题是,我遇到了一些我无法理解的错误,主要是因为这是我第一次在C#工作,还有一些隐藏在微软的代码

-第一个错误涉及控件。Add没有显示按钮,尽管每个在线来源都告诉我这是如何在主窗口的第78行添加按钮

-第二个涉及到我不知道如何在主窗口的第76行为按钮提供另一个类的方法

守则: Profile.cs

using System;
using Microsoft.VisualBasic.FileIO;
public class Profile
{
    private string mySource, myDestination, myName;
    public Profile()
    {
        myName = myDestination = mySource = "";
    }
    public Profile(string name, string source, string destination)
    {
        myName = name;
        mySource = source;
        myDestination = destination;
        using (System.IO.StreamWriter file =
            new System.IO.StreamWriter(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt"))
        {
            file.WriteLine(myName);
            file.WriteLine(mySource);
            file.WriteLine(myDestination);
            file.WriteLine("\n");
        }
    }
    public void backup()
    {
        try
        {
            FileSystem.CopyDirectory(mySource, myDestination,
    UIOption.AllDialogs);
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
    }
    public String getName()
    {
        return myName;
    }
}
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            refresh();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Enter the name of the profile: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter the folder which you want to copy (ENTER EXACTLY AS IN FILE SYSTEM): ");
            string source = Console.ReadLine();
            Console.WriteLine("Enter backup destination (ENTER EXACTLY AS IN FILE SYSTEM): ");
            string destination = Console.ReadLine();
            refresh();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Enter the EXACT name of the profile you want to delete: ");
            string line = Console.ReadLine();

                string[] lines = File.ReadAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt");
                for (int i = 0; lines[i] != null; i += 1)
            {
                if (lines[i] == line)
                {
                    lines[i] = lines[i + 1] = lines[i + 2] = "";
                    i += 2;
                }
            }
            File.WriteAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt", lines);

        }
        private void ButtonBackup(Profile pro)
        {
            pro.backup();
        }
        public void refresh()
        {
            string[] lines = File.ReadAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt");
            String reader = "placeholder";
            int i = 0;
            while (reader != null)
            {
                reader = lines[i];
                i += 1;
                if (reader != "\n")
                {
                    String name = reader;
                    reader = lines[i];
                    i += 1;
                    String source = reader;
                    reader = lines[i];
                    String destination = reader;
                    i++;
                    Profile newprofile = new Profile(name, source, destination);
                    Button b = new Button();
                    b.Name = name;
                    b.Click += new EventHandler(ButtonBackup(newprofile); //error 1
                    System.Windows.Controls.Add(b); //error 2
                }

            }
        }





    }

    }
MainWindow.xaml.cs

using System;
using Microsoft.VisualBasic.FileIO;
public class Profile
{
    private string mySource, myDestination, myName;
    public Profile()
    {
        myName = myDestination = mySource = "";
    }
    public Profile(string name, string source, string destination)
    {
        myName = name;
        mySource = source;
        myDestination = destination;
        using (System.IO.StreamWriter file =
            new System.IO.StreamWriter(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt"))
        {
            file.WriteLine(myName);
            file.WriteLine(mySource);
            file.WriteLine(myDestination);
            file.WriteLine("\n");
        }
    }
    public void backup()
    {
        try
        {
            FileSystem.CopyDirectory(mySource, myDestination,
    UIOption.AllDialogs);
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
    }
    public String getName()
    {
        return myName;
    }
}
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            refresh();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Enter the name of the profile: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter the folder which you want to copy (ENTER EXACTLY AS IN FILE SYSTEM): ");
            string source = Console.ReadLine();
            Console.WriteLine("Enter backup destination (ENTER EXACTLY AS IN FILE SYSTEM): ");
            string destination = Console.ReadLine();
            refresh();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Enter the EXACT name of the profile you want to delete: ");
            string line = Console.ReadLine();

                string[] lines = File.ReadAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt");
                for (int i = 0; lines[i] != null; i += 1)
            {
                if (lines[i] == line)
                {
                    lines[i] = lines[i + 1] = lines[i + 2] = "";
                    i += 2;
                }
            }
            File.WriteAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt", lines);

        }
        private void ButtonBackup(Profile pro)
        {
            pro.backup();
        }
        public void refresh()
        {
            string[] lines = File.ReadAllLines(@"C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt");
            String reader = "placeholder";
            int i = 0;
            while (reader != null)
            {
                reader = lines[i];
                i += 1;
                if (reader != "\n")
                {
                    String name = reader;
                    reader = lines[i];
                    i += 1;
                    String source = reader;
                    reader = lines[i];
                    String destination = reader;
                    i++;
                    Profile newprofile = new Profile(name, source, destination);
                    Button b = new Button();
                    b.Name = name;
                    b.Click += new EventHandler(ButtonBackup(newprofile); //error 1
                    System.Windows.Controls.Add(b); //error 2
                }

            }
        }





    }

    }
使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.IO;
命名空间WpfApp1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
刷新();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
WriteLine(“输入配置文件的名称:”);
字符串名称=Console.ReadLine();
WriteLine(“输入要复制的文件夹(输入与文件系统中完全相同):”;
string source=Console.ReadLine();
WriteLine(“输入备份目标(输入与文件系统中完全相同):”;
字符串destination=Console.ReadLine();
刷新();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
WriteLine(“输入要删除的配置文件的确切名称:”;
string line=Console.ReadLine();
string[]lines=File.ReadAllLines(@“C:\Users\Scott\Documents\visualstudio 2017\Projects\WpfApp1\profiles.txt”);
对于(int i=0;行[i]!=null;i+=1)
{
if(行[i]==行)
{
行[i]=行[i+1]=行[i+2]=“”;
i+=2;
}
}
writeAllines(@“C:\Users\Scott\Documents\Visual Studio 2017\Projects\WpfApp1\profiles.txt”,行);
}
私人作废按钮备份(Profile pro)
{
pro.backup();
}
公共无效刷新()
{
string[]lines=File.ReadAllLines(@“C:\Users\Scott\Documents\visualstudio 2017\Projects\WpfApp1\profiles.txt”);
字符串读取器=“占位符”;
int i=0;
while(读卡器!=null)
{
读卡器=行[i];
i+=1;
如果(读卡器!=“\n”)
{
字符串名称=读取器;
读卡器=行[i];
i+=1;
字符串源=读取器;
读卡器=行[i];
字符串目的地=读卡器;
i++;
Profile newprofile=新配置文件(名称、来源、目的地);
按钮b=新按钮();
b、 名称=名称;
b、 单击+=neweventhandler(按钮备份(newprofile);//错误1
System.Windows.Controls.Add(b);//错误2
}
}
}
}
}

您确定刚才以正确的方式创建了WPF应用程序吗? (文件/新建/项目..模板/Windows经典桌面..)

最后单击WPF应用程序上的此处,所有内容都已正确配置为使用WPF,尤其是按钮;-)

请创建一个像这样的新项目,并在其他任何事情之前创建按钮

您似乎对WPF了解不多,所以阅读或观看一些关于WPF的示例可能会很有用

如果项目是新的,最简单的方法是转到文件MainWindow.xaml,切换到最左侧的工具箱,然后将按钮拖动到窗口。(如果“工具箱”不存在,请通过菜单将其与视图/工具箱一起添加。)

Xaml代码将包含如下内容:

> <Grid>
>         <Button>
>             <Button Content="Button" Width="75"/>
>         </Button>
> </Grid>
在MainWindow.xaml.cs文件中,您会发现许多using语句,尤其是
using System.Windows.Controls但您可以通过鼠标右键单击和“删除并排序usings”来删除它们


至此,您已经证明,您不需要System.Windows.Controls来处理工具箱中的“按钮”等简单控件

您确定刚才以正确的方式创建了WPF应用程序吗? (文件/新建/项目..模板/Windows经典桌面..)

最后单击WPF应用程序上的此处,所有内容都已正确配置为使用WPF,尤其是按钮;-)

请创建一个像这样的新项目,并在其他任何事情之前创建按钮

您似乎对WPF了解不多,所以阅读或观看一些关于WPF的示例可能会很有用

如果项目是新的,最简单的方法是转到文件MainWindow.xaml,切换到最左侧的工具箱,然后将按钮拖动到窗口。(如果“工具箱”不存在,请通过菜单将其与视图/工具箱一起添加。)

Xaml代码将包含如下内容:

> <Grid>
>         <Button>
>             <Button Content="Button" Width="75"/>
>         </Button>
> </Grid>
在MainWindow.xaml.cs文件中,您会发现许多using语句,尤其是
using System.Windows.Controls但您可以通过鼠标右键单击和“删除并排序usings”来删除它们


至此,您已经证明,您不需要System.Windows.Controls来处理工具箱中的“按钮”等简单控件

我把这个问题转移到另一个答案,因为我担心这会误导贝金尼