是一个字段,但像类型错误一样使用

时间:2014-03-24 09:39:48

标签: c# wpf syntax-error

我错过了“使用”吗?因为我如何看待它我用作实例而非类型。

第一次使用“设置”

时出现错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Markup;

namespace AmpelThingy
{
    class Save
    {
        StringBuilder outstr = new StringBuilder();

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
        settings.NewLineOnAttributes = true;


        XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
        dsm.XamlWriterMode = XamlWriterMode.Expression;

        XamlWriter.Save(wrapPanel1, dsm);
        string savedControls = outstr.ToString();

        File.WriteAllText(@"AA.xaml", savedControls);
    }
}

2 个答案:

答案 0 :(得分:4)

你的代码可能有很多错误,但请将代码包装在一个方法中。

你有

namespace
{
    class
    {
      /* code*/
    }
}

它应该包含在一个方法中:

namespace
{
    class
    {
        Save()
        {
          //Do your thing.
        }  
    }
}

答案 1 :(得分:0)

全局区域只能包含声明。将任何其他代码移动到方法中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using  System.Windows.Markup;

    namespace AmpelThingy
    {
        class AnyClassName
        {
            public class Save()
            {
                StringBuilder outstr = new StringBuilder();

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
                settings.NewLineOnAttributes = true;


                XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
                dsm.XamlWriterMode = XamlWriterMode.Expression;

                XamlWriter.Save(wrapPanel1, dsm);
                string savedControls = outstr.ToString();

                File.WriteAllText(@"AA.xaml", savedControls);
            }
        }
    }

所以请记住。像XamlWriter.Save这样的代码必须移动到一个方法。方法外的所有内容都只能包含字段。字段是声明,该声明在该类中的所有方法和属性中可用(如果标记为公共,则在类外部)。

一个字段的例子:

public class Foo
{
    private string _filename = "any filename"; //is ok
    _filename = "any other filename"; //is not ok
}
相关问题