Paul Böttger
Paul Böttger

Reputation: 45

is a Field but used like a Type error

Did I miss a "using"? Because how I see it I use as a instance not a type.

The error appears by the first time use of "settings"

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);
    }
}

Upvotes: 0

Views: 1070

Answers (2)

Florian
Florian

Reputation: 5994

The global area can only contain declarations. Move any other code into a method:

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);
            }
        }
    }

So remember. Code like XamlWriter.Save has to be moved to a method. Everything outside a method can only contain fields. A field is declaration which is available in all methods and properties in that class (or outside the class if marked as public).

And example for a field:

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

Upvotes: 0

Marco
Marco

Reputation: 23945

Theres probably a lot more wrong with your code, but please wrap your code in a method.

You have

namespace
{
    class
    {
      /* code*/
    }
}

It should be wrapped in a method:

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

Upvotes: 4

Related Questions