I have designed a form to create controls like textbox, panels buttons dynamically. I have an option to edit the size and position of the controls. I need to maintain the layout and the parent and child relation of the controls so that I can save the information and re-create it when loaded again. Is there any other better option other than XML to maintain the layout information?
Thanks in Advance.
Try using app.config file it'll provide you better operations and will save a lot of code that you'll be doing right now to iterate through your saved XML file.
For more info Go here
Do you have the option of saving it in the database (per user if required)? If you have to implement it file-based XML is probably the easiest option, whether you go the app.config route or write your own class to save and retrieve custom user settings from XML - a good place to save this might be the user's AppData folder.
Anything saved to app.config might be lost when you use something like ClickOnce to do version upgrades.
Related
I'm totally new to C#, and got an assignment building a WPF form to modify settings in an existing C# program using Visual Studio and WPF.
I have numerous goals:
Get a list of all of the Settings names.
Retrieve all of the user properties.
Add a line for every setting to the form, and allow the user to restore the original settings, use the current ones, or modify them to a new value.
Make sure that the user input is in the correct Type.
Bind between the TextBox and the values in the line, not sure what is the best way to send the details. What object do you recommend to bind to the xaml? The list containing the property lines? Or to bind every line separately?
I think that I need to access the App.config file for 3, so far unsuccessfully. I would have like to get an advice about the architecture, since I'm new to VS, C# and WPF.
I don't think this is an assignment suitable for someone new to wpf let alone c#.
Even experienced wpf developers are likely to find some tricky bits in this task.
app.config will be in the same folder as your exe. If that is in program files then you will not be able to edit and save. Unless your users are win 7 or earlier.
User settings will usually be stored in appdata for this reason. Since the user is expected to likely want to change them.
They go in a user.config file. The location of one off my system is:
C:\Users\Andrew\AppData\Local\MapEditor\MapEditor.exe_Url_aszfdqs5110y44xmg0kfuuqbatf5la5a\1.0.0.0\user.config
I am user Andrew on the machine and these are the user settings for MapEditor.exe.
The file itself is xml.
In there I see
And there's a bunch of stuff inside that.
I would not try and edit xml directly.
And this is your first bit you'll find isn't exactly easy.
Because you need to translate xml.
"All the user properties."
You presumably know what they are.
Because the user can't just add them.
They need to be defined in the app.
I'd pick out the pieces you want them to change and copy data to a viewmodel. Or observable collection of viewmodels presented by a parent viewmodel.
Probably simplest as an observablecollection.
You can then have a different viewmodel class per type of property.
They enter a string in a number then it'll fail to transfer back to the viewmodel and you can trap validation.errors that'll bubble.
To save, translate the viewmodels back into xml and save it.
If I want to add a string as a setting or an image as a resource – I do it through Visual Studio.
But how do I add something complicated such as a large array or a Form which has to first be computed at runtime (or in case of the Form – populated with controls)?
I thought I could run it and persist it in settings (Properties.Settings.Default.Setting1 =...), and then publish. But that doesn't work (See: How to persist from build programmatically? ).
So how is it done?
Have you read up on serialization? I know that's kind of a generic answer, but I hope it helps.
As for a large array, you need to come up with an approach or mechanism for storing the data.
If it's a simple array that won't change very often, you can store it in the app.config.
If it's a datasource (ie: the data changes often) you could use an XML file and ideally a database.
I'm not really sure what you mean by persisting a Form. A Form should contain all controls compiled within it's own executable or dll. If you are asking how to populate the form, you would do this in the Main() method of the main form.
I want to write the entire contents of my application page (eg Mainpage.xml) to a file (in Isolated Storage ) How do I do it in WP7 ? are there any methods available to parse the page contents and write it to file in windows phone 7 ?
There is no built in way to do this.
However there are a couple of approaches you could try:
If the structure is static you could try and extract the resource containing this from the DLL. For future re-use it would be easier to load the page from the DLL again though.
If you're generating a page (or part of a page) at runtime (based on user input/preferences) and you want to be able to save/reload this then just save enough information to be able to recreate it. It's unlikely that XAML would be the best format for this though.
You could create this as you build the UI. Alternatively you could walk the visual tree to get details of all that is rendered. I'd recommend recording as you go so you can more easily keep track of non-default values in the rendered objects.
I have around 60 controls and I want to show a tooltip for each of them.
I could do this manually but I want something that would allow me to make changes to the tooltip messages or the control they are bound to without modyfing the code (I don't know what this would be called).
I thought about XML, but is there a better to store simple data like this?
I would recommend a custom configuration section. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx. Then all you have to change are the values in the config file. You could even reference it as a separate file as to not clutter up your web.config.
If you wanted them to be user-configurable through an admin section I'd say put the text in a database.
Otherwise, I'd suggest putting it in resource files.
I've got some controls on a web page that surface configuration elements of my web application.
I've wired up to their "OnChange" events, to capture value changes and so on. For each on change, I would like to say Configuration.Save(), but apart from getting "access denied" exceptions on web.config, I suspect this could be some weirdness, in trying to save to the configuration file for each control's onchange.
Any suggestions for the best way to handle this?
Would you add some detail as to what type of settings you are trying to update? I really don't think you want to save changes to your web.config from the application. Does the application have a database you could tie your saves to? That seems more appropriate for a changes you would want to make regularly, and in a transaction safe manner.
I still think this is better done using a database. But, if you must, then I'd check out this guide to working with web.config.
From an architectural standpoint, it would be better to save the changes to the configuration file when the page is submitted. However, it is likely that the web.config file is locked when the application is started.
Consequently, if you have application-specific changes that you would like to save, it is better to find a method other than web.config for saving them.
You would want to find an alternative to modifying the web.config file directly. Doing so causes your application to restart. Either generate your own XML file or a database configuration set up.