مشاركة عبر


كيفية القيام بما يلي: إنشاء إعدادات تطبيق

قم باستخدام تعليمات برمجية مُدارة، يمكنك إنشاء إعدادات تطبيق الجديد وربط هذه إلى خصائص تشغيل النموذج الخاص بك أو عناصر تحكم النموذج الخاص بك، حيث يتم تم تحميله هذه الإعدادات وحفظها auإلىmatically في وقت التشغيل.

في إجراء التالي، قمت بإنشاء برنامج تضمين فئة مشتقة من ApplicationSettingsBase. لهذا فئة إضافة خاصية قابلة للوصول بشكل عام لكل إعدادات التطبيق الذي تريد عرض.

يمكنك أيضا تنفيذ هذا إجراء باستخدام تعليمات برمجية أدنى في مصمم ‏‫Visual Studio. لمزيد من المعلومات، راجع: كيفية القيام بما يلي: إنشاء إعدادات تطبيق باستخدام "مصمم".

إلى إنشاء الجديد "إعدادات التطبيق" برمجياً

  1. إضافة فئة جديدة إلى مشروع الخاص بك، و إعادة تسميته. لتنفيذ هذا إجراء، فإننا سوف استدعاء هذه MyUserSettings. التغيير فئة تعريف حتى فئة مشتقة من ApplicationSettingsBase.

  2. تعريف خاصية في ذلك فئة برنامج التضمين لكل يطبق الإعداد الذي يتطلب وقم بتطبيق هذه الخاصية مع أي ApplicationScopedSettingAttributeأو UserScopedSettingAttribute، استناداً إلى نطاق من الإعداد. ل المزيد من المعلومات حول إعدادات نطاق، راجع نظرة عامة حول إعدادات تطبيق. قبل الآن، يجب أن تبدو تعليمات برمجية كما يلي:

    Imports System.Configuration
    
    Public Class MyUserSettings
        Inherits ApplicationSettingsBase
        <UserScopedSetting()> _
        <DefaultSettingValue("white")> _
        Public Property BackgroundColor() As Color
            Get
                BackgroundColor = Me("BackgroundColor")
            End Get
    
            Set(ByVal value As Color)
                Me("BackgroundColor") = value
            End Set
        End Property
    End Class
    
    using System;
    using System.Configuration;
    using System.Drawing;
    
    public class MyUserSettings : ApplicationSettingsBase
    {
        [UserScopedSetting()]
        [DefaultSettingValue("white")]
        public Color BackgroundColor
        {
            get
            {
                return ((Color)this["BackgroundColor"]);
            }
            set
            {
                this["BackgroundColor"] = (Color)value;
            }
        }
    }
    
  3. إنشاء مثيل لهذا برنامج تضمين فئة في تطبيق الخاص بك. عادة ما يكون عضو خاص الرئيسي نموذج. والآن بعد أن قمت بتعريف الفئة الخاص بك، تحتاج إلى يربط خاصية؛ في هذه الحالة، BackColorالخاصية الخاصة بك نموذج. يمكنك تنفيذ ذلك في الخاص بك نموذج's Load معالج حدث.

    Dim Mus As MyUserSettings
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Mus = New MyUserSettings()
        Mus.BackgroundColor = Color.AliceBlue
        Me.DataBindings.Add(New Binding("BackColor", Mus, "BackgroundColor"))
    End Sub
    
    MyUserSettings mus;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        mus = new MyUserSettings();
        mus.BackgroundColor = Color.AliceBlue;
        this.DataBindings.Add(new Binding("BackColor", mus, "BackgroundColor"));
    }
    
  4. إذا قمت بتوفير طريقة لتغيير إعدادات وقت التشغيل، فستحتاج إلى حفظ إعدادات مستخدم الحالي إلى قرص عند إغلاق النموذج الخاص بك، وإلا ستفقد هذه التغييرات.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Mus.Save()
    End Sub
    
        //Make sure to hook up this event handler in the constructor!
        //this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                mus.Save();
            }
    

    You have now successfully تاريخ الإنشاء a جديد إعدادات تطبيق و حدود it إلى the specified خاصية.

أمان

The الإعدادات افتراضية موفر, LocalFileSettingsProvider, persists معلومات إلى تكوين ملفات كـ نص عادي. This limits الأمان إلى the ملف الوصول الأمان provided بواسطة the operating النظام for the الحالي مستخدم. Because of this, care must be taken مع the معلومات stored في تكوين ملفات. For مثال, واحد عام استخدم for إعدادات تطبيق هو إلى store اتصال سلاسل that يؤشر إلى the تطبيق's بيانات store. However, because of الأمان concerns, such سلاسل should not تضمين passwords. For المزيد معلومات حول اتصال سلاسل, see SpecialSetting.

راجع أيضًا:

المهام

كيفية القيام بما يلي: التحقق من صحة إعدادات تطبيق

المرجع

SpecialSettingAttribute

LocalFileSettingsProvider

المبادئ

نظرة عامة حول إعدادات تطبيق