0

我很抱歉又问了一个愚蠢的问题。我几乎完成了保存 winform 的设置,当然要感谢 StackOverflow 的人们,但我被困在最后一件事上。请不要仅仅因为我是初学者而将其标记下来。

我收到以下错误:

非静态字段、方法或属性“ShovelShovel.WindowSize.Width.get”需要对象引用

非静态字段、方法或属性“ShovelShovel.WindowSize.Height.get”需要对象引用

这里:

设置.cs

public partial class Settings : Form
{
    public Settings()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        var windowSize = new WindowSize { Width = WindowSize.Width, Height = WindowSize.Height };

        WindowSizeStorage.WriteSettings(windowSize);

        Application.Exit();
    }
}

去:

窗口大小.cs

public class WindowSize
{
    public int Width { get; set; }
    public int Height { get; set; }
}

public static class WindowSizeStorage
{
    public static string savePath = "WindowSize.dat";

    public static WindowSize ReadSettings()
    {
        var result = new WindowSize();
        using (FileStream fileStream = new FileStream(savePath, FileMode.Open))
        {
            using (BinaryReader binaryReader = new BinaryReader(fileStream))
            {
                result.Width = binaryReader.ReadInt32();
                result.Height = binaryReader.ReadInt32();
            }
        }

        return result;
    }

    public static void WriteSettings(WindowSize toSave)
    {
        using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(savePath, FileMode.Create)))
        {
            binaryWriter.Write(toSave.Width);
            binaryWriter.Write(toSave.Height);
        }
    }
}

http://forums.codeguru.com/showthread.php?530631-Im-having-trouble-with-my-code

在那里你可以在附件中找到我的项目的完整文件,以防上述内容不足。

4

1 回答 1

3

也许你的意思是:

var windowSize = new WindowSize { Width = this.Width, Height = this.Height };

代替:

var windowSize = new WindowSize { Width = WindowSize.Width, Height = WindowSize.Height };

如所写,它需要 Width 和 Height 作为WindowSize类的静态属性,但我认为这不是您想要的。相反,使用表单实例WidthHeight属性更有意义。

于 2012-12-11T04:38:43.590 回答