簡體   English   中英

如何在運行時C#中更改靜態屬性值?

[英]How to change static property value in runtime C#?

我已經在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}/>

這是行不通的。 請提出有關如何在運行時更改靜態屬性值的想法。

使用WPF 4.5,您可以這樣做:

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

您可以使用PackSettingReader類

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;
    }
}

您可以使用默認值,也可以在其中設置值並在任何地方使用

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; }
}

您也可以在appSettings中設置值

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

現在您可以在C#中非常舒適的任何地方使用

PackageSettings.Active.UserId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM