简体   繁体   中英

How to change static property value in runtime C#?

I have bind the static property in xaml.

C#

 public static WrapperClass
 {
    public static string Filter 
      {
         get 
            {
                 return this.GetString(CultureInfo.Culture, "Filter"); 
            }
      }

XAML

<Application.Resources>
        <x:StaticExtension Member=local:WrapperClass.Filter x:Key="filtering" />
</Application.Resources>

<Button Content={DynamicResource filtering}/>

This is not working. Please suggest an idea on how to change the static property value in runtime.

使用WPF 4.5,您可以这样做:

<TextBox Text="{Binding Path=(local:WrapperClass.Filter), Mode=TwoWay}" />

you can use PackSettingReader class

public class PackSettingReader
{
    public PackSettingReader(string settingKeyPrefix = null)
    {
        SettingKeyPrefix = settingKeyPrefix;
    }

    public PackSettingReader(Type settingKeyTypePrefix)
        : this(settingKeyTypePrefix.FullName)
    {
    }

    public string SettingKeyPrefix { get; }

    public string Get(string settingKey, string defaultValue)
    {
        return Get(settingKey) ?? defaultValue;
    }

    public TValue Get<TValue>(string settingKey, TValue defaultValueOnNotFound = default(TValue),
        bool throwExceptionOnNotFound = false)
    {
        try
        {
            var valueString = Get(settingKey);
            if (valueString != null)
                if (typeof(TValue) == typeof(Guid))
                    return (TValue) Convert.ChangeType(Guid.Parse(valueString), typeof(Guid));
                else if (typeof(TValue) == typeof(Type))
                    return (TValue) (object) Type.GetType(valueString, throwExceptionOnNotFound);
                else if (typeof(TValue).IsEnum)
                    return (TValue) Enum.Parse(typeof(TValue), valueString);
                else if (typeof(TValue) == typeof(int[]))
                    return (TValue) (object) valueString.Split(',').Select(s => int.Parse(s)).ToArray();
                else if (typeof(TValue) == typeof(TimeSpan))
                    return (TValue) (object) TimeSpan.Parse(valueString);
                else
                    return (TValue) Convert.ChangeType(valueString, typeof(TValue));
            if (throwExceptionOnNotFound)
                throw new InvalidOperationException("Setting key '" + settingKey + "' value not found!");
            return defaultValueOnNotFound;
        }
        catch (Exception)
        {
            if (throwExceptionOnNotFound)
                throw;
            return defaultValueOnNotFound;
        }
    }

    private string Get(string settingKey)
    {
        var settingProvider =
            ServiceLocator.ResolveOnCurrentInstance<ISettingProvider, ConfigurationSettingProvider>();
        return settingProvider?[SettingKeyPrefix, settingKey];
    }

    public string GetFullKey(string settingKey)
    {
        if (!string.IsNullOrEmpty(SettingKeyPrefix))
            settingKey = SettingKeyPrefix + '.' + settingKey;
        return settingKey;
    }
}

you can use default value also set value in and use everywhere

public class PackageSettings
{
    public static readonly PackageSettings Active;
    public static readonly PackSettingReader SettingReader = new PackSettingReader("Packaging");

    static PackageSettings()
    {
        Active = new PackageSettings
        {
            UserId = SettingReader.Get("UserId", 1),
        };
    }

    public long? UserId { get;  set; }
}

also you can set value in appSettings

 <?xml version="1.0"?>
<appSettings>
  <add key="UserId" value="1" />  
</appSettings>

now you can use everywhere this very comfortable in C#

PackageSettings.Active.UserId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM