This post is related to c# - approach for saving user settings in a WPF application?
I have found multiple examples for saving/loading one window configuration but I am not sure if it will also save the configurations for all usercontrols open inside that window or just the actual main windows configuration...
First is it possible to save/load multiple usercontrol configurations inside a window using the application/user settings?
Am I forced to just read/write xml to store the the multiple usercontrols configuration?
Thanks in advance and let me know if you need any other information!
EDIT
I have an application with a ribbonbar on top that allows the user to open one or more user controls from the ribbonbar. Hypothetically if i wanted to save all the open usercontrols window position, height, width, etc... would it still be possible to use the application settings to accomplish this?
EDIT2
I have an application and within that application there are many usercontrols a user can access. If a user opens 2 usercontrols and uses those controls often, I want to be able to save those usercontrols to the workspace so there always there when the program is open or closed. The way I am approaching it now is with serialized xml from the database. I was researching this topic and came across the application settings approach and wanted to know if this was a viable approach for my situation. Thanks again Marc for taking the time to help me figure this problem out.
What I've used is user-specific ApplicationSettings and binding those to properties I want to 'save'. Here's a good example of what I've done http://www.jaylee.org/post/2007/02/WPF-DataBinding-and-Application-Settings.aspx
I little more detail might be helpful here, I'm not sure I follow. Are you saying you have multiple instances of a given user control on a single window and you want to persist the properties for each of them?
A lot of this depends on how you created and populated those controls. If they were created dynamically from data for example, the best solution is to serialize that data to disk and re-create the controls when the application restarts.
Alternatively you could bake in the serialization of the settings into the user control itself such that it writes out it's settings to a file name based on it's instance name (control1.xml, control2.xml, etc) and then have it repopulate when the instance comes back to life.
Knowing how these controls are added and the properties are set in first place would be helpful.
Related
I have built a form where the user can add controls, buttons and more dynamically.
is it possible somehow to save the dynamic controls and load them automatically the next time the user starts the program?
if it is not possible I would like to know if there is a common method or good practice for saving this data to some kind of a config file?
Thanks
One way would be to loop through all the controls on the form and save them to Isolated Storage...
https://msdn.microsoft.com/en-us/library/3ak841sy.aspx
https://msdn.microsoft.com/en-us/library/xf96a1wz(v=vs.110).aspx
But it really depends on your use case, do you want to do this automatically and transparent from the user, or do you want to present the user an option to save to a file somewhere?
While it is not specifically for dynamic controls, to give you more ideas see link below on another stackoverflow question :
Best practice to save application settings in a Windows Forms Application
I want to create one project with different versions like
Normal
Purchased
So as per my version I want to change some detail of my project like if someone is using Normal then icon should be "Normal.png" and if my it's Purchased then all the form should have icon of "Purchased.png".
Currently what happens is I wrote a code on condition base in every form.
Is there any way from where I can avoid this?
Is there any way from which I can create a new resource file and during compiling time tool will decide which resource file to use?
I would create a static "theme" class that you can call when loading a windows form--and then pass it the current form. Depending on the current theme (in this case, "Normal" or "Purchased"), manipulate the form accordingly.
See this link for an example of changing a form's icon dynamically with code using resources.
You can also bind properties of controls to application settings -- see this link for more on that. (This wouldn't apply for the form icon, but it could help with other changes you want to make based on Normal/Purchased behavior).
I've looked over Stack overflow and seen some posts that almost solve this problem but not really. To be specific I'm referring to an application that is deployed to users who might have 3 or 4 displays and the application would want to remember which one to go to on startup.
I have a WPF application and I want to save what screen it was on when the form does it's OnClose. Then when it loads I want to put my application on that screen.
How can this be done (without hacky Win32 API calls)?
EDIT: In a previous StackO post someone mentioned doing something like:
var screen = System.Windows.Forms.Screen.FromRectangle(
new System.Drawing.Rectangle(
(int)myWindow.Left, (int)myWindow.Top,
(int)myWindow.Width, (int)myWindow.Height));
Once I have screen it seems that the only thing I can do is check whether the application is either on the primary screen or not. Which is fine...if my users only have two screens, but not good if they have 3+. Is there something better I can do with this resulting screen object than just check if it's primary.
You can get more infomation on multiple monitors from Screen.AllScreens property
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.allscreens.aspx
Also from memory, I think if you just save your Window Left and Top position and restore them when you load your app, this works with multiple monitors.
I did this previously with a ViewManager class, a custom XML config file and MVVM. Essentially, when OnClose is called, the ViewManager class (A custom class that really just held a collection of also custom ViewInfo instances) is polled to find out which views are currently open and in which order (as they could be tabbed through using Ctrl+Tab) and what the ID of the record was that was being displayed; this app was using Entity Framework to access database data, each application view mapped to either a collection of records or a single record of a specific type.
Anyhow the XML output would contain the name of the View, the Application (there were several portions to the app which I referred to as applications internally) to which it belonged, information about the record that was loaded, etc.
Upon loading the program, the only View that is automatically loaded is the HomeView which potentially contains all other views. ViewManager checks the XML file and loads views based on it's content. This action could be turned off in the options screen so that users where presented with a clean workspace upon entering if they like.
basically my project is an MDI Winform application where a user can customize the interface by adding various controls and changing the layout. I would like to be able to save the state of the application for each user.
I have done quite a bit of searching and found these:
How to auto save and auto load all properties in winforms C#?
Save WinForm or Controls to File
Basically from what I understand, the best approach is to serialize the data to XML, however winform controls are not serializable, so I would have use surrogate classes:
http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx
Now, do I need to write a surrogate class for each of my controls? I would need to write some sort of a recursive algorithm to save all my controls, what is the best approach to do accomplish that? How would I then restore all the windows, should I use the memento design pattern for that? If I want to implement multiple users later, should I use Nhibernate to store all the object data in a database? I am still trying to wrap my head around the problem and if anyone has any experience or advice I would greatly appreciate it, thanks
You don't want to serialize the actual control instances. They should be created and destroyed along with the Form they reside in. Rather look at what you let the user customize. Layout and position? Very well, save out the Top and Left coordinates for each control along with a control identifier. Do you let the user add new controls? Save their ids along with a type identifier so when its time to reload you are able to recreate the controls at their previous position.
Whether you use XML or some other format, there is no best approach or best practice, choose what makes sense for your project. XML happens to be an easy to go with format with great support in the .Net Framework.
I know there is a software, LinsUI Layout Manager, which handle your problems very well. They have free version for interested developers. You can check the site.
Cheer
How could you create an application that could search through a directory, find user controls in the form of paired .xaml / .xaml.cs files, and compile/execute these on the fly? Is this possible? Is there an .exec() or .compile() class somewhere that could do this?
Basically what I want to do with this is have a WPF application that is running and by having the user fill out a form describing the kind of page/functionality he wants, the application could generate XAML and code behind files which are then immediately usable without having to recompile the application.
I'm assuming that this is to change the behaviour of the UI on a known application rather than a XAML/CS component generator for use in another application - after all there's already applications that do this - Expression Blend anyone?
Do you really need to recompile the underlying CS? As far as I can see it all you'll be doing is changing the apparent behaviour and look of the application and UI. This could be achieved by command binding within the xaml and styles for the components.
The reality is that in order to perform the functionality that you require you'll be giving the user a finite choice as to behaviour. You'll need to decide what behaviour is application and what is the UI.
Application bahaviour is governed by fixed commands (they could accept parameters to change behaviour).
UI behaviour (appearance, animation etc) is covered by the xaml. If the application runs in a browser window you could auto generate the xml needed as requried, linking it to the underlying app. commands and allow the browser to execute the new behaviour for you.
Is this a good idea? I can see a few problems with this.
How will the code behind 'know' how to interact with the rest of the application
Security? You will be allowing somebody to make system API calls on behalf of the main application
App domains???
Rather build up the forms using ItemsControls and DataTemplates. In the form where the user specifies what functionality he wants in the form, you will be presenting him with a list of 'building blocks' anyway. Make each 'building block' a ViewModel and associate each ViewModel with a DataTemplate 'UserControl'.