Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 相当于WPF dotnet core中的用户设置/应用程序设置_C#_Wpf_.net Core - Fatal编程技术网

C# 相当于WPF dotnet core中的用户设置/应用程序设置

C# 相当于WPF dotnet core中的用户设置/应用程序设置,c#,wpf,.net-core,C#,Wpf,.net Core,对于.Net Core>=3.0的WPF应用程序,持久化用户设置的首选方法是什么 创建了WPF.Net核心3.0项目(VS2019 V16.3.1) 现在我已经看到没有属性。设置部分了 在线搜索后,开始深入Microsoft.Extensions.Configuration 除了访问设置的臃肿代码之外,现在更糟糕的是->不保存? 幸运或不幸的是,Microsoft.Extensions.Configuration 不支持按设计保存。阅读本期Github的更多内容 对于.Net Core>=

对于.Net Core>=3.0的WPF应用程序,持久化用户设置的首选方法是什么

创建了WPF.Net核心3.0项目(VS2019 V16.3.1) 现在我已经看到没有属性。设置部分了

在线搜索后,开始深入Microsoft.Extensions.Configuration

除了访问设置的臃肿代码之外,现在更糟糕的是->不保存?

幸运或不幸的是,Microsoft.Extensions.Configuration 不支持按设计保存。阅读本期Github的更多内容


对于.Net Core>=3.0的WPF应用程序,持久化用户设置的首选方法是什么(简单/快速/简单)


之前,请立即保存
Properties.Settings.Default.LanguageSettings=selected.twoLetterIsLanguageName;
Properties.Settings.Default.Save()


正如您在引用的帖子中指出的,Microsoft.Extensions.Configuration API是为您的应用程序一次性设置的,或者至少是只读的。如果你的主要目标是保持用户设置简单/快速/简单,你可以自己做一些事情。将设置存储在
ApplicationData
文件夹中,类似于旧API

公共类设置管理器,其中T:class
{
私有只读字符串\u文件路径;
公共设置管理器(字符串文件名)
{
_filePath=GetLocalFilePath(文件名);
}
私有字符串GetLocalFilePath(字符串文件名)
{
字符串appData=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
返回Path.Combine(appData,文件名);
}
public T LoadSettings()=>
文件是否存在(\u filePath)?
JsonConvert.DeserializeObject(File.ReadAllText(_filePath)):
无效的
公共无效保存设置(T设置)
{
字符串json=JsonConvert.SerializeObject(设置);
writealText(_filePath,json);
}
}

使用最基本的
用户设置的演示

公共类用户设置
{
公共字符串名称{get;set;}
}
我不打算提供一个完整的MVVM示例,但我们在内存中有一个实例,ref
\u userSettings
。加载设置后,演示将覆盖其默认属性。当然,在生产中,您不会在启动时提供默认值。这只是为了举例说明

公共部分类主窗口:窗口
{
私有只读设置管理器\u设置管理器;
私有用户设置\u用户设置;
公共主窗口()
{
初始化组件();
_userSettings=newusersettings(){Name=“Funk”};
_settingsManager=new settingsManager(“UserSettings.json”);
}
内存中的专用无效按钮(对象发送器,路由目标)
{
应用(_userSettings);
}
私有无效按钮\u加载设置(对象发送器、路由目标)
{
_userSettings=\u settingsManager.LoadSettings();
应用(_userSettings);
}
私有无效按钮\u保存设置(对象发送器、路由目标)
{
_userSettings.Name=textBox.Text;
_settingsManager.SaveSettings(_userSettings);
}
私有无效应用(用户设置用户设置)
{
textBox.Text=userSettings?.Name??“未找到任何设置”;
}
}
XAML


凭记忆
负载设置
保存设置

您可以使用Nuget软件包。它与.Net标准2.0兼容,因此应该可以在.Net核心应用程序中使用

没有用于此的设计器,但在其他方面,它的工作原理与.Net版本相同,您应该能够从
设置.designer.cs
中复制代码。此外,您还可以覆盖
OnPropertyChanged
,因此无需调用
Save

下面是一个示例,来自正在运行的.Net标准项目:

public class WatchConfig: ApplicationSettingsBase
{
    static WatchConfig _defaultInstance = (WatchConfig)Synchronized(new WatchConfig());

    public static WatchConfig Default { get => _defaultInstance; }

    protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        Save();
        base.OnPropertyChanged(sender, e);
    }

    [UserScopedSetting]
    [global::System.Configuration.DefaultSettingValueAttribute(
    @"<?xml    version=""1.0"" encoding=""utf-16""?>
    <ArrayOfString>
      <string>C:\temp</string>
     <string>..\otherdir</string>
     </ArrayOfString>")]
    public StringCollection Directories
    {
        get { return (StringCollection)this[nameof(Directories)]; }
        set { this[nameof(Directories)] = value; }
    }
}
公共类WatchConfig:ApplicationSettingsBase { 静态WatchConfig _defaultInstance=(WatchConfig)已同步(newwatchconfig()); 公共静态WatchConfig默认值{get=>\u defaultInstance;} 受保护的重写无效OnPropertyChanged(对象发送方,PropertyChangedEventArgs e) { Save(); base.OnPropertyChanged(发送方,e); } [用户范围设置] [全局::System.Configuration.DefaultSettingValueAttribute( @" C:\temp ..\otherdir ")] 公共集合目录 { 获取{return(StringCollection)this[nameof(Directories)];} 设置{this[nameof(目录)]=value;} } }
只需双击项目中的
设置.Settings
文件。它仍然会像以前一样在设计师中打开。您只是不再在属性窗口中列出它。

您可以添加相同的旧良好设置文件,例如,通过右键单击属性->添加->新项目并搜索“设置”。可以在设置设计器中编辑该文件,并在之前的.net framework项目中使用该文件(ConfigurationManager、settings.Default.Upgrade()、settings.Default.Save等)

还将app.config文件添加到项目根文件夹(与通过Add->New项相同),再次保存设置,编译项目,您将在输出文件夹中找到.dll.config文件。您现在可以像以前一样更改默认应用程序值


使用Visual Studio 1.16.3.5和.net core 3.0 WPF项目进行测试。

用于WPF net.core

项目单击鼠标右键->添加新项目->设置文件(常规)

使用

其中“Settings1”创建了文件名

范例

双击设置1。设置文件并编辑

private void MainWindowRoot_SourceInitialized(object sender, EventArgs e)
{
    this.Top = Settings1.Default.Top;
    this.Left = Settings1.Default.Left;
    this.Height = Settings1.Default.Height;
    this.Width = Settings1.Default.Width;
    // Very quick and dirty - but it does the job
    if (Settings1.Default.Maximized)
    {
        WindowState = WindowState.Maximized;
    }
}

private void MainWindowRoot_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        // Use the RestoreBounds as the current values will be 0, 0 and the size of the screen
        Settings1.Default.Top = RestoreBounds.Top;
        Settings1.Default.Left = RestoreBounds.Left;
        Settings1.Default.Height = RestoreBounds.Height;
        Settings1.Default.Width = RestoreBounds.Width;
        Settings1.Default.Maximized = true;
    }
    else
    {
        Settings1.Default.Top = this.Top;
        Settings1.Default.Left = this.Left;
        Settings1.Default.Height = this.Height;
        Settings1.Default.Width = this.Width;
        Settings1.Default.Maximized = false;
    }

    Settings1.Default.Save();
}
基于
private void MainWindowRoot_SourceInitialized(object sender, EventArgs e)
{
    this.Top = Settings1.Default.Top;
    this.Left = Settings1.Default.Left;
    this.Height = Settings1.Default.Height;
    this.Width = Settings1.Default.Width;
    // Very quick and dirty - but it does the job
    if (Settings1.Default.Maximized)
    {
        WindowState = WindowState.Maximized;
    }
}

private void MainWindowRoot_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        // Use the RestoreBounds as the current values will be 0, 0 and the size of the screen
        Settings1.Default.Top = RestoreBounds.Top;
        Settings1.Default.Left = RestoreBounds.Left;
        Settings1.Default.Height = RestoreBounds.Height;
        Settings1.Default.Width = RestoreBounds.Width;
        Settings1.Default.Maximized = true;
    }
    else
    {
        Settings1.Default.Top = this.Top;
        Settings1.Default.Left = this.Left;
        Settings1.Default.Height = this.Height;
        Settings1.Default.Width = this.Width;
        Settings1.Default.Maximized = false;
    }

    Settings1.Default.Save();
}