Looking for guidelines to create application file. For example I have an application that store user input into a file (Textbox, DataGrid, ListBox etc). I'm looking for WPF-C# implementation.
I would like to have the following:
- If user edit a any form(Textbox, etc) an asterisk is displayed to the window title.
- When window is closed and asterisk is still there, a promote "Would you like to save changes" appears. If then saved the aster开发者_高级运维isk disappear.
What do real applications use to create their application file? (Note: I'm not looking for database saving or SQL)
I'm just looking for hints and guidelines. Thank you.
One way I've approached this in the past is to just have a Settings/Application object.
Use a form level variable to determine if something has been changed.
If it has been, then prompt the user.
If they click yes, then serialize your Settings/Applications object. If you want it to be human readable/editable, then use an Xml serializer, if you don't care about that, use the binary serializer.
Use the IsDirty pattern, which is where you have a boolean property for each field that you flip to true on TextChanged events. This can be used to determine when to show the asterix. After they click save you clear the IsDirty properties for each field.
Bind your form to a class object that represents the data you want to save, and serialize that object to a file on save.
There's many options for achieving your goal, the most common solutions would involve either serializing your classes and storing them to disk or exporting the data your classes contain into a regular text document like CSV.
If you want files that usable files like CSV types I would recommend looking at the FileHelpers. Another thing to keep in mind using a database is the same as storing a file to disk. That's how databases work (unless its an in memory db) it's just a different type of file.
Mark a flag to notify that changes were done to the textbox. Better, customize your textbox and put a flag as property which is set internally when textbox's text is changed.
You can do in a few ways. Some of them are -
- Application.Idle event handler - Check for the flag. If dirty/true, append * to the title of the form's title. You might want to use some patterns to do this on all the monitored text boxes.
- Design a class to do this task exclusively and keep track of it.
There are other ways too. I'll edit the answer as I remember them.
精彩评论