[英]How to change static property value in runtime C#?
I have bind the static property in xaml. 我已经在xaml中绑定了静态属性。
C# C#
public static WrapperClass
{
public static string Filter
{
get
{
return this.GetString(CultureInfo.Culture, "Filter");
}
}
XAML 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 您可以使用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;
}
}
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 您也可以在appSettings中设置值
<?xml version="1.0"?>
<appSettings>
<add key="UserId" value="1" />
</appSettings>
now you can use everywhere this very comfortable in C# 现在您可以在C#中非常舒适的任何地方使用
PackageSettings.Active.UserId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.