在调用之间实现数据存储? (C#-WPF)

发布于 2024-08-16 19:34:53 字数 516 浏览 8 评论 0原文

我有一个 ALPHA 应用程序,它允许您从 XPS 文件中获取模糊字体并存储 .odttf 文件以在 Silverlight 中使用。该应用程序“工作”就像它在锡上所说的那样,尽管是以一种非常粗糙的方式。

在清理这个工具以创建测试版的过程中,我可以解决一个问题。我希望应用程序以向导样式布局,该向导样式将从用户那里收集数据,例如使用哪种字体、在哪里保存提取的文件等。

在当前的实现中,这一切都是在后面的代码中完成的(实际上它调用辅助类)。但实施向导意味着每条数据都收集在不同的“页面”上。我不想简单地使用页面构造函数传递数据,因为我正在尝试实现 MVVM,其目的是保持代码干净。

我想我正在寻找的是持久化窗口的代码中的数据存储。这样我就可以抓取窗口(视图)中的数据,通过绑定将其传递到我的视图模型,并将其发送到窗口模型(数据类)以存储在某处。

我可以使用数据库或 XML 文件,但这对于我需要的几条信息来说似乎是过于强大的存储。

总而言之,有没有一种方法可以让内存中的对象持久化窗口调用并可以被多个模型(类)引用。

谢谢!

I have an ALPHA application which allows you grab an obfuscated font from and XPS file and store the .odttf file for use in Silverlight. The application "works" as in it does what it says on the tin, albeit in a very rough sort of way.

In the process of cleaning this tool up to create the BETA I can across an issue. I want the application to be laid out in a wizard style which would gather data from the user, like which font to use, where to save the extracted file, etc.

In the current implementation this is all done in the code behind (Actually it calls a helper class). But implementing a wizard means that each piece of the data is gathered on a different "page". I did not want to simple pass the data around using the page constructors as I am trying to implement MVVM which aims to keep the code behind clean.

I suppose what I am looking for is a data storage in code that persists windows. that way I can grab the data in my window (view), pass it to my viewmodel via binding and sent it down to the windows model(data class) to be stored somewhere.

I could use a database or XML file but that seems like overpowered storage for the few pieces of information I need.

So in summary, is there a way to have an in memory object that persists window calls and can be referenced by multiple models (classes).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2024-08-23 19:34:53

静态类或单例来存储数据并从所有视图访问它?

Static class or singleton to store your data and access it from all your views?

ぃ弥猫深巷。 2024-08-23 19:34:53

最好的方法是对所有页面使用相同的 ViewModel,然后如果您想稍后使用它,则将其序列化。

public class MyViewModel
{
    // Properties to be serialized
    public string MyViewModelProperty1 = "";
    public string MyViewModelProperty2 = "";

    // Save to file.
    public virtual bool Save(string viewmodelFilePath)
    {
        try
        {
            Directory.CreateDirectory(Path.GetDirectoryName(viewmodelFilePath));
            StreamWriter write = new StreamWriter(viewmodelFilePath);
            XmlSerializer xml = new XmlSerializer(GetType());
            xml.Serialize(write, this);
            write.Close();
        }
        catch
        {
            return false;
        }
        return true;
    }

    // Load from file.
    public static object Load(Type type, string viewmodelFilePath)
    {
        StreamReader reader;
        object settings;
        XmlSerializer xml = new XmlSerializer(type);

        try
        {
            reader = new StreamReader(viewmodelFilePath);
            viewmodel = xml.Deserialize(reader);
            reader.Close();
        }
        catch
        {
            viewmodel = 
                type.GetConstructor(System.Type.EmptyTypes).Invoke(null);
        }
        return viewmodel;
    }
}

Petzold 书中的原始代码

Best way would be using the same ViewModel for all pages and then serializing it if you want to use it later.

public class MyViewModel
{
    // Properties to be serialized
    public string MyViewModelProperty1 = "";
    public string MyViewModelProperty2 = "";

    // Save to file.
    public virtual bool Save(string viewmodelFilePath)
    {
        try
        {
            Directory.CreateDirectory(Path.GetDirectoryName(viewmodelFilePath));
            StreamWriter write = new StreamWriter(viewmodelFilePath);
            XmlSerializer xml = new XmlSerializer(GetType());
            xml.Serialize(write, this);
            write.Close();
        }
        catch
        {
            return false;
        }
        return true;
    }

    // Load from file.
    public static object Load(Type type, string viewmodelFilePath)
    {
        StreamReader reader;
        object settings;
        XmlSerializer xml = new XmlSerializer(type);

        try
        {
            reader = new StreamReader(viewmodelFilePath);
            viewmodel = xml.Deserialize(reader);
            reader.Close();
        }
        catch
        {
            viewmodel = 
                type.GetConstructor(System.Type.EmptyTypes).Invoke(null);
        }
        return viewmodel;
    }
}

Original code from Petzold book

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文