0

如果我有用户设置ToggleThis

在此处输入图像描述

我想在菜单中为用户提供此设置,例如设置/切换设置。点击它。每次单击都应切换用户设置 true/false,但还会更新 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 中的图标和/或某些值上使用绑定,但我真的不知道从哪里开始。

谢谢

4

2 回答 2

1

你需要Save在你认为合适的时候打电话。

Properties.Settings.Default.Save();

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

于 2012-09-16T20:36:32.333 回答
1

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

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

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

  • 奥斯汀已经指出其中之一:保存您的设置。您应该在方法中的 if/else 之后立即执行此操作:MenuItem_Click. 只需确保在调用Settings.Save 之前该方法不会以某种方式退出......a 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();
    }
于 2012-09-16T21:26:01.003 回答