Reputation: 681
It is possible to access somehow ONLY via xaml code to an environment variable? In my case, I only need the read access.
Upvotes: 1
Views: 1174
Reputation: 2127
You can write a custom markup extension. Something like:
[MarkupExtensionReturnType(typeof(String))]
public class EnvironmentVarExtension : MarkupExtension
{
private string _variableName;
public EnvironmentVarExtension(string variableName)
{
_variableName = variableName;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Environment.GetEnvironmentVariable(VariableName);
}
[ConstructorArgument("variableName")]
public string VariableName
{
get { return _variableName; }
set { _variableName = value; }
}
}
And use it in your XAML:
<Grid>
<TextBlock Text="{local:EnvironmentVar Path}" />
</Grid>
Upvotes: 3