简体   繁体   English

简单的C#WPF表单生成了看似无意义的错误消息

[英]Simple C# WPF form generating a slew of seemingly nonsensical error messages

The code below contains a "whole bunch" of error messages, and I can't for the life of me figure out why. 下面的代码包含“一堆”错误消息,我无法一生找出原因。 I recently had to "downgrade" from VS2010 to VS2008, and have had nothing but misery since. 我最近不得不从VS2010“降级”到VS2008,此后除了痛苦之外什么都没有。 The first few error messages are shown as comments next to where they are occurring. 前几条错误消息在发生的位置旁边显示为注释。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO;

namespace UniClient_NextGen
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void buttonPopMenuItemsLU_Click(object sender, RoutedEventArgs e) 
        { // err msg #1 = "} expected"
            public static int TOPLEVEL_ID = 0;
            public static int PARENT_ID = 1;
            public static int SELF_ID = 2;
            public static int MENU_CAPTION = 3;
            public static int MENU_NAME = 4;

            string fileName = @"C:\_UniClientNextGen\MenuItemsWithIDs.txt";
            using (StreamReader reader = File.OpenText(fileName)) // err msg #2 = "Invalid token 'using' in class, struct, or interface member declaration" + err msg #3 = "; expected" (at end of this line)
            {
                string _line = null;
                string[] strElements;
                do
                {
                    _line = reader.ReadLine();
                    strElements = _line.Split(",");
                    // strElements should now have five elements
                    int iTopLevelID = Convert.ToInt32(strElements[TOPLEVEL_ID]);
                    int iParentID = Convert.ToInt32(strElements[PARENT_ID]);
                    int iOwnID = Convert.ToInt32(strElements[SELF_ID]);
                    string sMenuCaption = strElements[MENU_CAPTION];
                    string sMenuName = strElements[MENU_NAME];
                    //performSQL("INSERT INTO MENU_ITEMS_LOOKUP (TopLevelMenuID, ParentMenuID, MenuItemName, MenuItemCaption) VALUES (iTopLevelID, iParentID, iOwnID, sMenuCaption, sMenuName)");
                } while (_line != null);
            }
        } // err msg #4 = "Type or namespace definition, or end-of-file expected" 

        private void buttonPopSorterTypesLU_Click(object sender, RoutedEventArgs e)
        {
            //
        }

        private void buttonPopTabsheetsLU_Click(object sender, RoutedEventArgs e)
        {
            //
        }

        private void buttonPopMenuItem_SorterTypeM2M_Click(object sender, RoutedEventArgs e)
        {
            //
        }

        private void buttonPopSorterType_TabsheetM2M_Click(object sender, RoutedEventArgs e)
        {
            //
        }
    }
}

为什么在方法内部有public static声明?

I think you're XAML definition or XAML namespace is not in sync with your class. 我认为您是XAML定义或XAML命名空间与您的类不同步。 Did you recently rename a class or a namespace? 您最近是否重命名了类或名称空间?

Here is the corrected code (I have moved static member declaration in right place and corrected the _line.Splt() parameter): 这是更正后的代码(我将静态成员声明移到正确的位置并更正了_line.Splt()参数):

//...
public static int TOPLEVEL_ID = 0;
public static int PARENT_ID = 1;
public static int SELF_ID = 2;
public static int MENU_CAPTION = 3;
public static int MENU_NAME = 4;

void buttonPopMenuItemsLU_Click(object sender, RoutedEventArgs e) {
    string fileName = @"C:\_UniClientNextGen\MenuItemsWithIDs.txt";
    using(StreamReader reader = File.OpenText(fileName)) {
        string _line = null;
        string[] strElements;
        do {
            _line = reader.ReadLine();
            strElements = _line.Split(',');
            int iTopLevelID = Convert.ToInt32(strElements[TOPLEVEL_ID]);
            int iParentID = Convert.ToInt32(strElements[PARENT_ID]);
            int iOwnID = Convert.ToInt32(strElements[SELF_ID]);
            string sMenuCaption = strElements[MENU_CAPTION];
            string sMenuName = strElements[MENU_NAME];
        } while(_line != null);
    }
}
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM