我已经绑定了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}/>
这不起作用。请建议如何在运行时更改静态属性值。
答案 0 :(得分:0)
使用WPF 4.5,您可以这样做:
[{"Position":{"Left":0.0,"Top":0.0,"Right":0.0,"Bottom":0.0},"Image":{"PixelBuffer":{},"PixelHeight":1600,"PixelWidth":2560,"Dispatcher":{"HasThreadAccess":true,"CurrentPriority":0}}},{"Position":{"Left":490.0,"Top":0.0,"Right":0.0,"Bottom":0.0},"Image":{"PixelBuffer":{},"PixelHeight":1600,"PixelWidth":2560,"Dispatcher":{"HasThreadAccess":true,"CurrentPriority":0}}}]
答案 1 :(得分:0)
您可以使用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