views:

118

answers:

1

i have some element in my container and want to save all properties of this elements. i list this element by VisualTreeHelper and save its attributes in DB, question is that how to retrieve this properties and affect them? i think that The Silverlight have some statement that behave like Exec in Sql-Server.

i save properties in one line that delimited by semicolon.(if you have any suggestion ,appreciate)

Edit: suppose this scenario: End-User choose a tool from Mytoolbox(a container like Grid) ,a dialog shown its properties for creation and finally draw Grid . in resumption he/she choose one element(like Button) and drop it on one of the grid's cell. now i want to save workspace that he/she created! My RootLayout have one container control so any of element are child of this.HERETOFORE i want create one string that contain all general properties(not all of them) and save to DB, and when i load this control, i create an element by the type that i saved and affect it by the properties that i saved; with something like EXEC command.

is this possible ? have you new approach for this scenario(Guide me with example please).

FINAL ANSWER HERE

+1  A: 

You might want to try Isolated File Storage, where you can save/load your settings to the local hard drive. Doing so is very easy, just a few lines of code.

i think that The Silverlight have some statement that behave like Exec

No it doesn't :)

Edit: ok, here is a guideline on how you might go about applying your locally saved settings to your UI elements.

First up, decide on an interface that your settable controls will implement:

public interface IMySettableSettingsControl
{
    Dictionary<string, object> RetrieveSettings();
    void ApplySettings(Dictionary<string, object> settings);
}

Now add this interface to your controls that will persist their settings locally:

public MyControl : IMySettableSettingsControl 
{
    public void ApplySettings(Dictionary<string, object> settings) 
    {
        if (settings.Key.Contains("TextboxContents"))
            someTextBox.Text = (string) settings["TextboxContents"];

        if (settings.Key.Contains("Height"))
            this.Height = (double) settings["Height"];

        ((IMySettableSettingsControl) someChildControl).ApplySettings( (Dictionary<string, object>) settings["MyChildControl"])
    }

    public Dictionary<string, object> RetrieveSettings()
    {
        Dictionary<string, object> localSettings = new Dictionary<string, object>();
        localSettings.Add("Height", this.Height);
        localSettings.Add("TextboxContents", someTextBox.Text);

        localSettings.Add("MyChildControl", ((IMySettableSettingsControl) someChildControl).RetrieveSettings();
    }
}

Of course you can get as fancy as you like with this - what i have illustrated above is simply an example. What i have done in the past is have my MainPage.cs file load the settings from Isolated Storage, and then pass the Dictionary in on the constructor of each controller it creates, each controller will then pass it on to each view it creates. To save the settings, i handle the Exit event of the Application:

Application.Current.Exit += new EventHandler(Current_Exit);

and then in the handler:

private void Current_Exit(object sender, EventArgs e)
{
    Dictionary<string, object> settings = new Dictionary<string, object>();

    //call each UI control or controller, passing in the settings object

    //now i can persist the settings dictionary to IsolatedStorage
}

Of course when i do this i use a custom data object for persisting my settings, and i have the option of calling a webservice to retrieve default settings if a local instance is not found (or is found to be corrupt, because the saving of it was interrupted for some reason).

So take the above example, slice it and dice it and change it to suit your needs. Hope it helps.

slugster
@slugster : i know that how to save/load my settings, the bottleneck is how can i set this propertis to my elemnt! thanks for reply.
Meysam Javadi
Check the edit.
slugster
@slugster: thanks. your post is great. where you write "object o = settings[key];" is ok and great but i don't know how to apply it.
Meysam Javadi
*object o* would be a string, or int, or double, or whatever. I stored everything in the Dictionary as an object so that all sorts of different types of settings could be stored in the same container - you would have to cast it to its correct type before reassigning it to the property of a UI element. I have modified my example to show this more clearly.
slugster
This sample is written using the philosophy that every control will know which of its own settings were saved, so each control can test for and access specifically keyed items in the Dictionary, and any settings from child controls can be stored as a nested Dictionary. It's not the most robust method for the long term, but it will get you started and you may think up better ways of storing or restoring the values as you continue.
slugster
@slugster: is there any other approach to save and load the controls dinamically ?
Meysam Javadi
After reading your edit, maybe you are after the *XamlReader.Load()* function (here: http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader.aspx), which can load a string of Xaml which you can then add in your visual tree.
slugster
Thanks, great code... i really excited!
Meysam Javadi