如何获取列表&LT;串GT;值的集合在WPF的app.config? [英] How to get a List<string> collection of values from app.config in WPF?
本文介绍了如何获取列表&LT;串GT;值的集合在WPF的app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的例子罢了,在的ItemsControl 与 BackupDirectories 这是我从code得到一个列表。
我怎样才能改变这种使我从app.config文件相同的信息?
XAML:
&LT;窗口x:类=TestReadMultipler2343.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300&GT;
&LT;电网保证金=10&GT;
&LT; Grid.RowDefinitions&GT;
&所述; RowDefinition高度=30/&GT;
&LT; RowDefinition高度=自动/&GT;
&LT; /Grid.RowDefinitions>
&LT; Grid.ColumnDefinitions&GT;
&LT; ColumnDefinition WIDTH =120/&GT;
&LT; ColumnDefinition WIDTH =160/&GT;
&LT; /Grid.ColumnDefinitions>
&LT;的TextBlock
Grid.Row =0
Grid.Column =0
文本=标题:/&GT;
&LT;的TextBlock
Grid.Row =0
Grid.Column =1
文本={结合标题}/&GT;
&LT;的TextBlock
Grid.Row =1
Grid.Column =0
文本=备份目录/&GT;
&LT;的ItemsControl
Grid.Row =1
Grid.Column =1
的ItemsSource ={结合BackupDirectories}/&GT;
&LT; /网格和GT;
&LT; /窗GT;
code-背后:
使用System.Collections.Generic;
使用System.Windows;
使用System.Configuration;
使用System.ComponentModel;命名空间TestReadMultipler2343
{
公共部分类窗口1:窗口,INotifyPropertyChanged的
{ #地区ViewModelProperty:标题
私人字符串_title;
公共字符串标题
{
得到
{
返回_title;
} 组
{
_title =价值;
OnPropertyChanged(标题);
}
}
#endregion #地区ViewModelProperty:BackupDirectories
私人列表&LT;串GT; _backupDirectories =新的List&LT;串GT;();
公开名单&LT;串GT; BackupDirectories
{
得到
{
返回_backupDirectories;
} 组
{
_backupDirectories =价值;
OnPropertyChanged(BackupDirectories);
}
}
#endregion 公共窗口1()
{
的InitializeComponent();
的DataContext =这一点; 标题= ConfigurationManager.AppSettings.Get(称号); GetBackupDirectoriesInternal();
} 无效GetBackupDirectoriesInternal()
{
BackupDirectories.Add(@C:\\ test1的);
BackupDirectories.Add(@C:\\ TEST2);
BackupDirectories.Add(@C:\\ TEST3);
BackupDirectories.Add(@C:\\ TEST4);
} 无效GetBackupDirectoriesFromConfig()
{
// BackupDirectories = ConfigurationManager.AppSettings.GetValues(backupDirectories);
}
#区域INotifiedProperty座
公共事件PropertyChangedEventHandler的PropertyChanged; 保护无效OnPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged; 如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
#endregion }
}
的app.config:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;结构&gt;
&LT;&的appSettings GT;
&LT;添加键=标题值=备份工具/&GT;
&LT;! - &LT;添加键=backupDirectories&GT;
&LT;增加价值=C:\\测试1/&GT;
&LT;增加价值=C:\\ TEST2/&GT;
&LT;增加价值=C:\\ TEST3/&GT;
&LT;增加价值=C:\\ TEST4/&GT;
&LT; /添加&gt; - &GT;
&LT; /的appSettings&GT;
&LT; /结构&gt;
解决方案
您可以创建app.config文件中自己的自定义配置部分。有相当几个的教程 周围让你开始。最终,你可以有这样的事情:
&LT; configSections&GT;
&lt;节名称=backupDirectoriesTYPE =TestReadMultipler2343.BackupDirectoriesSection,TestReadMultipler2343/&GT;
&LT; / configSections&GT;&LT; backupDirectories&GT;
&LT;目录位置=C:\\测试1/&GT;
&LT;目录位置=C:\\ TEST2/&GT;
&LT;目录位置=C:\\ TEST3/&GT;
&LT; / backupDirectories&GT;
The following example fills the ItemsControl with a List of BackupDirectories which I get from code.
How can I change this so that I get the same information from the app.config file?
XAML:
<Window x:Class="TestReadMultipler2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0"
Grid.Column="0"
Text="Title:"/>
<TextBlock
Grid.Row="0"
Grid.Column="1"
Text="{Binding Title}"/>
<TextBlock
Grid.Row="1"
Grid.Column="0"
Text="Backup Directories:"/>
<ItemsControl
Grid.Row="1"
Grid.Column="1"
ItemsSource="{Binding BackupDirectories}"/>
</Grid>
</Window>
code-behind:
using System.Collections.Generic;
using System.Windows;
using System.Configuration;
using System.ComponentModel;
namespace TestReadMultipler2343
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Title
private string _title;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
OnPropertyChanged("Title");
}
}
#endregion
#region ViewModelProperty: BackupDirectories
private List<string> _backupDirectories = new List<string>();
public List<string> BackupDirectories
{
get
{
return _backupDirectories;
}
set
{
_backupDirectories = value;
OnPropertyChanged("BackupDirectories");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Title = ConfigurationManager.AppSettings.Get("title");
GetBackupDirectoriesInternal();
}
void GetBackupDirectoriesInternal()
{
BackupDirectories.Add(@"C:\test1");
BackupDirectories.Add(@"C:\test2");
BackupDirectories.Add(@"C:\test3");
BackupDirectories.Add(@"C:\test4");
}
void GetBackupDirectoriesFromConfig()
{
//BackupDirectories = ConfigurationManager.AppSettings.GetValues("backupDirectories");
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="title" value="Backup Tool" />
<!--<add key="backupDirectories">
<add value="C:\test1"/>
<add value="C:\test2"/>
<add value="C:\test3"/>
<add value="C:\test4"/>
</add>-->
</appSettings>
</configuration>
解决方案
You can create your own custom config section in the app.config file. There are quite a few tutorials around to get you started. Ultimately, you could have something like this:
<configSections>
<section name="backupDirectories" type="TestReadMultipler2343.BackupDirectoriesSection, TestReadMultipler2343" />
</configSections>
<backupDirectories>
<directory location="C:\test1" />
<directory location="C:\test2" />
<directory location="C:\test3" />
</backupDirectories>
这篇关于如何获取列表&LT;串GT;值的集合在WPF的app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文