freiberuflicher Software Entwickler C#, vb.Net, vba, UWP, WPF, WinForms, SQL Server, Access, Excel, ASP.Net Core MVC Telefon: 07022/9319004   Kontakt
#

WPF: Settings dynamisch erstellen, schreiben und lesen

 

Unter WPF werden Einstellungen in Settings gespeichert. Diese befinden sich normalerweise im Entwicklungsmodus in einer editierbaren Datei.

Man kann aber die Settings auch dynamisch zur Laufzeit verwenden wie einstellbare User-Variablen oder App-Variablen.

Im Gegensatz zu UWP muss man die Settings allerdings selber über Properties erstellen, ändern und lesen.

 

#UWP Settings, Settings, Properties

 

Speichern von Settings

Die Settings werden in WPF als XML Datei verwaltet. Wenn man die Settings zur Laufzeit verwaltet, dann muss man auf die Settings.Default.Properties zugreifen

Properties.Settings.Default.Properties

 

Innerhalb dieser Aufzählung befinden sich Element vom Typ SettingsProperty

SettingsProperty prop

 

 

Eine einzelne Setting Einstellung wird direkt mit dem Namen abgefragt

//< existing Setting >

prop = Properties.Settings.Default.Properties[property_name];

//</ existing Setting >

 

Wenn man eine neue Setting Einstellung erstellen will, dann muss man ein Settingsproperty erstellen und dieses in den Properties[] anfügen.

Mit .Save() wird die Einstellung und somit auch die Settings-Datei gespeichert

prop = new System.Configuration.SettingsProperty(property_name);

prop.PropertyType = typeof(string);

Properties.Settings.Default.Properties.Add(prop);

Properties.Settings.Default.Save();

 

Das Lesen und Schreiben von Werten in die Settings verwende ich einfach über den DefaultValue. Eine Zuweisung über .Value ist nicht vorhanden.

//< set value  >

Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

//</ set value >

 

 

 

Komplettes Settings Beispiel

In WPF habe ich die Settings als Defaultvalue gespeichert und gelesen. Dieses ist als  Workaround, da das Schreiben in einen echten Value unter Properties nicht ausgeführt wird.

        private void save_Setting(string setting_Name, string setting_Value)

        {

            //----------< save_Settings() >----------

 

            string property_name = setting_Name;

 

            //< Setting erstellen >

            SettingsProperty prop = null;

            if (Properties.Settings.Default.Properties[property_name] != null)

            {

                //< existing Setting >

                prop = Properties.Settings.Default.Properties[property_name];

                //</ existing Setting >

            }

            else

            {

                //< new Setting >

                prop = new System.Configuration.SettingsProperty(property_name);

                prop.PropertyType = typeof(string);

                Properties.Settings.Default.Properties.Add(prop);

                Properties.Settings.Default.Save();

                //</ new Setting >

            }

            //</ Setting erstellen >

 

            //< set value  >

            Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

            //</ set value >

 

            //< Save Settings File >

            Properties.Settings.Default.Save();

            //</ Save Settings File >

           

            //----------</ save_Settings() >----------

        }

 

 

 

 

 

Komplettes Settingsbeispiel

 

      #region  Region: Settings

 

        private void save_Settings()

        {           

            foreach (FrameworkElement element in CanvasMain.Children)

            {

                if (element.GetType() == typeof(UcPanel))

                {

                    UcPanel panel = element as UcPanel;                   

                    save_Setting("Panel_ID_" + panel.panel_ID, panel.panel_ID );

                    save_Setting("panel_Caption_" + panel.panel_ID, panel.panel_Caption);

                    save_Setting("panel_Background_Color_" + panel.panel_ID, panel.panel_Background_Color );

                    save_Setting("panel_Border_Thickness_" + panel.panel_ID, panel.panel_Border_Thickness.ToString()  );

                    save_Setting("panel_Left_" + panel.panel_ID, Canvas.GetLeft(panel).ToString());

                    save_Setting("panel_Top_" + panel.panel_ID, Canvas.GetTop(panel).ToString());

                    save_Setting("panel_Height_" + panel.panel_ID, panel.ActualHeight.ToString() );

                    save_Setting("panel_Width_" + panel.panel_ID, panel.ActualWidth.ToString());                  

                }

            }

        }

 

        private void read_Settings()

        {

            int posID = "Panel_ID_".Length;

            foreach (SettingsProperty setting in Properties.Settings.Default.Properties)                            {

                if (setting != null)

                {

                    if (setting.Name.Contains("Panel_ID_"))

                    {

                        UcPanel panel = new UcPanel();

 

                        string panel_ID = setting.Name.Substring(posID);

 

                        //--< setup panel >--

                        string sValue = "";

 

                        sValue = read_Setting("panel_Caption_" + panel_ID);

                        panel.panel_Caption = sValue;

 

                        sValue = read_Setting("panel_Border_Thickness_" + panel_ID);

                        panel.panel_Border_Thickness = Convert.ToInt16 ( sValue);

 

                        sValue = read_Setting("panel_Background_Color_" + panel_ID);

                        panel.panel_Background_Color  = sValue;

 

                        sValue = read_Setting("panel_Left_" + panel_ID);

                        Canvas.SetLeft(panel, Convert.ToDouble(sValue));

 

                        sValue = read_Setting("panel_Top_" + panel_ID);

                        Canvas.SetTop(panel, Convert.ToDouble(sValue));

 

                        sValue = read_Setting("panel_Height_" + panel_ID);

                        panel.Height  = Convert.ToDouble(sValue);

 

                        sValue = read_Setting("panel_Width_" + panel_ID);

                        panel.Width = Convert.ToDouble(sValue);

                        //--< setup panel >--

 

                        //< add >

                        CanvasMain.Children.Add(panel);

                        //</ add >

                    }

 

                }

            }

        }

 

        private void save_Setting(string setting_Name, string setting_Value)

        {

            //----------< save_Settings() >----------

 

            string property_name = setting_Name;

 

            //< Setting erstellen >

            SettingsProperty prop = null;

            if (Properties.Settings.Default.Properties[property_name] != null)

            {

                //< existing Setting >

                prop = Properties.Settings.Default.Properties[property_name];

                //</ existing Setting >

            }

            else

            {

                //< new Setting >

                prop = new System.Configuration.SettingsProperty(property_name);

                prop.PropertyType = typeof(string);

                Properties.Settings.Default.Properties.Add(prop);

                Properties.Settings.Default.Save();

                //</ new Setting >

            }

            //</ Setting erstellen >

 

            //< set value  >

            Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

            //</ set value >

 

            //< Save Settings File >

            Properties.Settings.Default.Save();

            //</ Save Settings File >

           

            //----------</ save_Settings() >----------

        }

 

        private string read_Setting(string setting_Name)

        {

            //----------< save_Settings() >----------

            string sResult = "";

 

            //< get Setting >

            if (Properties.Settings.Default.Properties[setting_Name] != null)

            {

                sResult = Properties.Settings.Default.Properties[setting_Name].DefaultValue.ToString() ;               

            }

            //</ get Setting >

 

            //< correct >

            if (sResult == "NaN") sResult = "0";

            //</ correct >

 

            //< output >

            return sResult;

            //</ output >

 

            //----------</ save_Settings() >----------

        }

        #endregion  /Region: Settings

 

 

Screenshot:

Settings einstellungen

 

Settings View Code

Mobile
»
WPF: Settings dynamisch erstellen, schreiben und lesen

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Raimund.Popp@Microsoft-Programmierer.de