在 xaml wpf 中使用 app.config 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34853378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:37:49  来源:igfitidea点击:

using app.config settings in xaml wpf

c#wpfxaml

提问by User9898

<appSettings>
  <add key="inactivity_interval" value="10" />
  <add key="maximumHeightPopUp" value="260" />
  <add key="horizontalArrowsHeight" value="35" />
  <add key="modelsListHeight" value="100" />
</appSettings>

I want to use part of this settings in the xaml for example to set grid height. Is it possible?

我想在 xaml 中使用此设置的一部分,例如设置网格高度。是否可以?

回答by Zergling

Yes you can do it,with using System.Configuration

是的,你可以做到,使用 System.Configuration

ConfigurationManager.AppSettings["inactivity_interval"]; 

will return value.

将返回值。

for more check this

更多检查这个

回答by rdn87

In your app.configchange your appsettingto

在您的app.config 中,将您的appsetting更改为

<applicationSettings>
    <WpfApplication1.Properties.Settings>
        <setting name="inactivity_interval" serializeAs="String">
            <value>10</value>
        </setting>
        <setting name="maximumHeightPopUp" serializeAs="String">
            <value>260</value>
        </setting>
        <setting name="horizontalArrowsHeight" serializeAs="String">
            <value>35</value>
        </setting>
        <setting name="modelsListHeight" serializeAs="String">
            <value>100</value>
        </setting>
    </WpfApplication1.Properties.Settings>
</applicationSettings>

Then in code-behind C# for retrive values:

然后在代码隐藏 C# 中检索值:

string inc_interval = WpfApplication1.Properties.Settings.Default.inactivity_interval.ToString();
string maximumHeightPopUp = WpfApplication1.Properties.Settings.Default.maximumHeightPopUp.ToString();
string horizontalArrowsHeight = WpfApplication1.Properties.Settings.Default.horizontalArrowsHeight.ToString();
string modelsListHeight= WpfApplication1.Properties.Settings.Default.modelsListHeight.ToString();