gpt4 book ai didi

c# - 如何从 WPF 中的 app.config 获取 List 值集合?

转载 作者:IT王子 更新时间:2023-10-29 03:42:36 24 4
gpt4 key购买 nike

以下示例使用我从代码中获得的 BackupDirectories 列表填充 ItemsControl

如何更改此设置以便从 app.config 文件中获取相同的信息?

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>

代码隐藏:

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

}
}

应用程序配置:

<?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>

最佳答案

您可以在单个值中用分号分隔它们,例如

应用程序配置

<add key="paths" value="C:\test1;C:\test2;C:\test3" />

C#

var paths = new List<string>(ConfigurationManager.AppSettings["paths"].Split(new char[] { ';' }));

关于c# - 如何从 WPF 中的 app.config 获取 List<string> 值集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1779117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com