【问题标题】:How to properly update settings from menus如何从菜单正确更新设置
【发布时间】:2023-03-13 09:48:01
【问题描述】:

如果我有用户设置ToggleThis

我希望在菜单中为用户提供此设置,例如设置/切换设置。点击它。每次点击都应该切换用户设置的真/假,但也会更新 menuItem 图标以显示实际设置。

我可以使用

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Menu IsMainMenu="True">
            <MenuItem Header="_Settings">
                <MenuItem Header="_Toggle" Name="ToggleSettings" Click="MenuItem_Click">
                    <MenuItem.Icon>
                        <Image Source="Images/Toggle.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>            
        </Menu>
    </Grid>
</Window>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (ToggleSettings.Icon == null)
            {
                Uri Icon = new Uri("pack://application:,,,/Images/" + "Toggle.png");
                ToggleSettings.Icon = new Image
                {
                    Source = new BitmapImage(Icon)
                };
                Properties.Settings.Default.toggleThis = true;
            }
            else
            {
                ToggleSettings.Icon = null;
                Properties.Settings.Default.toggleThis = false;
            }
        }


    }
}

但是,我知道这不是正确的做法,例如,根据之前的设置,在启动时菜单可能不会处于正确的状态。麻烦的是,我不知道正确的方法。谁能给我一些关于正确方法的指示?

我假设我需要在 MenuItem 中的图标和/或某些值上使用绑定,但我真的不知道从哪里开始。

谢谢

【问题讨论】:

  • 你应该学习 MVVM、绑定、数据模板和触发器。

标签: c# wpf xaml settings


【解决方案1】:

实际上没有“正确”的方法,只有最有效的方法,以及根据上下文最合适的方法。

您的示例看起来不错,至少到目前为止,您唯一的问题似乎是,您选择的选项不会与用户选择/未选择的内容同步,他们最后一次使用软件。

这只需要您的两小段代码位于两个特定位置。

  • Austin 已经指出其中之一:保存您的设置。您应该在方法中的 if/else 之后立即执行此操作:MenuItem_Click。只需确保在调用 Settings.Save 之前该方法不会以某种方式退出... try/catch 以优雅的方式确保设置状态一致是谨慎的。
  • 另一个是你自己提到的“时间”:初始化,或者应用程序的启动。在您应用中的某个位置,在初始加载完成之前,您必须访问您创建的设置 (toggleThis) 并使用它来设置菜单项的初始状态。

实现这一点的最佳方法是使用私有方法,该方法负责更改菜单项上显示的图标,以及将最新状态存储在应用程序的设置中。一个称为Toggle() 的方法可能会在您的MenuItem_Click 方法中调用。您需要提供有问题的菜单项和 ID,但可用于访问代码隐藏中的菜单项。同样,此代码示例还假设您的图标也存储在设置中,尽管图标可以来自任何地方,只要您可以引用它们。

所以你的代码可能是这样的,虽然不完全是这样:

public MainWindow() 
    { 
        InitializeComponent();
        this.SetToggleIcon(Properties.Settings.Default.toggleThis);
    } 

    private void Toggle()
    {
        this.StoreToggleState(!Properties.Settings.Default.toggleThis);
        this.SetToggleIcon(Properties.Settings.Default.toggleThis);
    }

    private void SetToggleIcon(bool state)
    {
      this.menuItem_ToggleSettings.Icon = (Properties.Settings.Default.toggleThis) ? Properties.Settings.Default.IconTrue : Properties.Settings.Default.IconFalse;
    }

    private void StoreToggleState(bool state)
    {
       Properties.Settings.Default.toggleThis = state;
       Properties.Settings.Default.Save();
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e) 
    { 
      this.Toggle();
    }

【讨论】:

    【解决方案2】:

    您需要在认为合适时致电Save

    Properties.Settings.Default.Save();
    

    目前尚不完全清楚如何您正在使用它,但这将确保至少存储更新的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多