无法正确读取设置

时间:2018-01-22 18:38:23

标签: c# wpf backup settings

我最近将我游戏的启动器重新制作为WPF。虽然大部分代码都运行良好,但是当我移动它时,我正在处理的一个特殊功能完全破坏了。

我正在处理的功能是一个简单的备份系统。设置很简单:用户转到保存管理器并勾选自动备份设置。这是tick的XAML代码:

  <CheckBox x:Name="AutoBackupCheckbox" Content="{x:Static  properties1:Resources.EnableBackupsString}" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Checked="AutoBackupCheckbox_CheckChanged" Unchecked="AutoBackupCheckbox_CheckChanged" IsChecked="{Binding Source={x:Static properties1:Settings.Default},Path=AutoBackupEnabled}"/>

当复选框上的勾号发生变化时,会发生这种情况:

        private void AutoBackupCheckbox_CheckChanged(object sender, RoutedEventArgs e)
    {
        Settings.Default.AutoBackupEnabled = AutoBackupCheckbox.IsEnabled;
        MessageBox.Show(Settings.Default.AutoBackupEnabled.ToString());
        Settings.Default.Save();
    }

问题是代码坚定地坚持True。我不知道是什么迫使这个设置。这使得备份系统始终被禁用,因为在调试期间,我总是看到设置在为真时返回false。

以下是启动器在启动时所执行的操作的代码:

        {
        var jumperDeck = new JumpList();
        jumperDeck.JumpItems.Add(JumperCatalog.StartGameJumpTask);
        jumperDeck.JumpItems.Add(JumperCatalog.QuickLoadJumpTask);
        jumperDeck.JumpItems.Add(JumperCatalog.SaveManagerTask);
        jumperDeck.JumpItems.Add(JumperCatalog.ResetLauncherLockTask);
        jumperDeck.JumpItems.Add(JumperCatalog.BugReporterJumpTask);
        jumperDeck.JumpItems.Add(JumperCatalog.EManualJumpTask);
        jumperDeck.JumpItems.Add(JumperCatalog.DiscordJumpTask);
        jumperDeck.JumpItems.Add(JumperCatalog.ReadmeJumpTask);
        JumpList.SetJumpList(Application.Current, jumperDeck);

        var assembly = Assembly.GetExecutingAssembly();
        {
            var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            var version = fvi.FileVersion;
            LauncherVersionLabel.Content = LauncherVersionLabel.Content + @" (" + version + @")";
        }
        GameVersionLabel.Content = GameVersionLabel.Content + @" (0.20.4-20180117)";
        var args = Environment.GetCommandLineArgs();
        //if (IsCompatible == false)
        //{
        //    Form osIncompatible = new IncompatibleVersion();
        //    osIncompatible.ShowDialog();
        //    Close();
        //}
        foreach (var arg in args)
            switch (arg)
            {
                case "-ResetLauncherLock":
                    Settings.Default.SkipLauncher = false;
                    Settings.Default.Save();
                    break;
                case "-NoLauncher":
                case "-QuickLoad":
                    if (!File.Exists(PlayerFile) || !File.Exists(RgssLib) || !File.Exists(InputPath))
                    {
                        MessageBox.Show(Properties.Resources.FileMissingLabelString, Properties.Resources.CriticalFailiureString, MessageBoxButton.OK,
                            MessageBoxImage.Error);
                    }
                    else if (arg == "-QuickLoad")
                    {
                        QuickLoad();
                    }
                    else
                    {
                        NormalStart();
                    }
                    break;
            }
        if (Settings.Default.SkipLauncher)
            if (!File.Exists(PlayerFile) || !File.Exists(RgssLib) || !File.Exists(InputPath))
            {
                MessageBox.Show(Properties.Resources.FileMissingLabelString, Properties.Resources.CriticalFailiureString, MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            else
            {
                NormalStart();
            }
        IoController.DiskCheck(CommonVariables.FolderCheck);
        if (!CommonVariables.IsDriveReady)
        {
            MessageBox.Show(Properties.Resources.DNRString, Properties.Resources.DNRTitle, MessageBoxButton.OK, MessageBoxImage.Warning);
            Close();
        }
        if (CommonVariables.PermissionError || CommonVariables.SpaceError)
        {
            CanSaveLabel.Content = Properties.Resources.CommonWordNo;
            CanSaveLabel.Foreground = Brushes.DarkRed;

            if (CommonVariables.PermissionError)
            {
                CanSaveLabel.Content = CanSaveLabel.Content + Properties.Resources.DiagnositcPermissionError;
                EngineErrorLabel.Content = Properties.Resources.BackupPermissionError;
            }
            else
            {
                CanSaveLabel.Content = CanSaveLabel.Content + Properties.Resources.DiagnosticSpaceError;
                EngineErrorLabel.Content = Properties.Resources.NotEnoughSpaceLabelString;
            }
            EngineErrorLabel.Visibility = CommonVariables.PermissionError ? Visibility.Visible : Visibility.Hidden;
        }
        else
        {
            CanSaveLabel.Content = Properties.Resources.CommonWordYes;
            CanSaveLabel.Foreground = Brushes.ForestGreen;
        }

        var gfMiss = 0;

        if (File.Exists(PlayerFile))
        {
            var gamever = FileVersionInfo.GetVersionInfo(PlayerFile);
            Rgss3PlayLabel.Content = Properties.Resources.PresentString + " " + @"(" + gamever.FileVersion + @")";
            Rgss3PlayLabel.Foreground = Brushes.ForestGreen;
        }
        else
        {
            Rgss3PlayLabel.Content = Properties.Resources.MissingString;
            Rgss3PlayLabel.Foreground = Brushes.DarkRed;
            gfMiss = gfMiss + 1;
        }

        if (File.Exists(RgssLib))
        {
            var libver = FileVersionInfo.GetVersionInfo(RgssLib);
            Rgss3LibLabel.Content = Properties.Resources.PresentString + " " + @"(" + libver.FileVersion + @")";
            Rgss3LibLabel.Foreground = Brushes.ForestGreen;
        }
        else
        {
            Rgss3LibLabel.Content = Properties.Resources.MissingString;
            Rgss3LibLabel.Foreground = Brushes.DarkRed;
            gfMiss = gfMiss + 1;
        }
        if (File.Exists(GameFile))
        {
            FileAvailiable.Content = Properties.Resources.PresentString;
            FileAvailiable.Foreground = Brushes.ForestGreen;
        }
        else
        {
            FileAvailiable.Content = Properties.Resources.MissingString;
            FileAvailiable.Foreground = Brushes.DarkRed;
            gfMiss = gfMiss + 1;
        }
        if (File.Exists(InputPath))
        {
            var inputver = FileVersionInfo.GetVersionInfo(InputPath);
            InputLabel.Content = Properties.Resources.PresentString + @"(" + inputver.FileVersion + @")";
            InputLabel.Foreground = Brushes.ForestGreen;
        }
        else
        {
            InputLabel.Content = Properties.Resources.MissingString;
            InputLabel.Foreground = Brushes.DarkRed;
            gfMiss = gfMiss + 1;
        }

        if (File.Exists(RgssExtFile))
        {
            var rex = FileVersionInfo.GetVersionInfo(RgssExtFile);
            RexVersionLabel.Foreground = Brushes.ForestGreen;
            RexVersionLabel.Content = Properties.Resources.PresentString +" (" + rex.FileVersion + ")";
        }
        else
        {
            RexVersionLabel.Foreground = Brushes.DarkRed;
            RexVersionLabel.Content = Properties.Resources.MissingString;
            gfMiss = gfMiss + 1;
        }

        if (File.Exists(CoreLibLocation))
        {
            var clx = FileVersionInfo.GetVersionInfo(CoreLibLocation);
            CoreLibraryLabel.Content = Properties.Resources.PresentString + " (" + clx.FileVersion + ")";
            CoreLibraryLabel.Foreground = Brushes.ForestGreen;
        }
        else
        {
            CoreLibraryLabel.Content = Properties.Resources.MissingString;
            CoreLibraryLabel.Foreground = Brushes.DarkRed;
            gfMiss += 1;
        }

        if (gfMiss > 0)
        {
            GfPresentLabel.Content = Properties.Resources.CommonWordNo;
            GfPresentLabel.Foreground = Brushes.DarkRed;
            EngineErrorLabel.Content = Properties.Resources.FileMissingLabelString;
            EngineErrorLabel.Visibility = Visibility.Visible;
            PlayButton.IsEnabled = false;
            SkipSplashCheckbox.IsEnabled = false;
            DisableLauncherCheckbox.IsEnabled = false;
        }
        else
        {
            GfPresentLabel.Content = Properties.Resources.CommonWordYes;
            GfPresentLabel.Foreground = Brushes.ForestGreen;
            if (!Directory.Exists(CommonVariables.SaveLocation))
            {
                Directory.CreateDirectory(CommonVariables.SaveLocation);
                CommonVariables.IsSaveAvailable = false;
            }
            else
            {
                CommonVariables.IsSaveAvailable = NorthbridgeCommonTasks.GetFolderSaves();
            }
        }
        ExportButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable;
        QuickLoadCommand.IsEnabled = CommonVariables.IsSaveAvailable;
        AutoBackupCheckbox.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable;
        LocationBackup.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable && (bool)AutoBackupCheckbox.IsChecked;
        //ArchiveBckup.Enabled = gfMiss == 0 && CommonVariables.IsSaveAvailable && (bool)AutoBackupCheckbox.IsChecked;
        DeleteButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable;
        TestBackupButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable &&
                                     AutoBackupCheckbox.IsChecked == true &&
                                     LocationBackup.Text != null;
        BuildZipCheckbox.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable;
        BrowseButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable && AutoBackupCheckbox.IsEnabled;
        RestoreBackupButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable &&
                                        AutoBackupCheckbox.IsChecked == true;
        DeleteBackupButton.IsEnabled = gfMiss == 0 && CommonVariables.IsSaveAvailable;
        ImportButton.IsEnabled = gfMiss == 0 && !CommonVariables.PermissionError && !CommonVariables.SpaceError;
        GpuControl.IsEnabled = gfMiss == 0;
        //SlotSelectCheckBox.Enabled = ImportButton.IsEnabled;
        ReadmeButton.IsEnabled = File.Exists(Ci == "el-GR" ? GreekReadme : ReadmeFile);
        AutoBackupCheckbox.IsChecked = Settings.Default.AutoBackupEnabled;
        SkipSplashCheckbox.IsChecked = Settings.Default.AcceleratedMode;
        if (!File.Exists(Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "\\RMVXA-GL.dll")) GpuControl.Visibility = Visibility.Hidden;
        GpuControl.IsChecked = Settings.Default.EnableGPUAccelration;
        SingleBackupModeCheckbox.IsEnabled = gfMiss == 0 && (bool)AutoBackupCheckbox.IsChecked;
    }

这是启动游戏的代码:

        private void NormalStart()
    {
        Thread.Sleep(500);
        Hide();
        CommonProcedures.CheckDrive();
        GameInfo.Arguments = Settings.Default.AcceleratedMode ? "-playnow -astart" : "-playnow";
        if (!Settings.Default.EnableGPUAccelration) GameInfo.Arguments = GameInfo.Arguments + " -NoGPU";
        if (Settings.Default.CompatMode)
        {
            GameInfo.Arguments = GameInfo.Arguments + " -compatmode";
            Directory.CreateDirectory("C:\\Immortal Sins\\Config\\");
            IoController.DirectoryCopy(ConfigArea, "c:\\Immortal Sins\\Config\\", true);
        }
        _gameProcess.StartInfo = GameInfo;
        if (Settings.Default.AutoBackupEnabled && Settings.Default.AutoBackupLocation != null)
            BackupEngine.MonitorSetupCode();
        _gameProcess.Start();
        _gameProcess.WaitForExit();
        if (Settings.Default.AutoBackupEnabled && Settings.Default.AutoBackupLocation != null &&
            BackupEngine.CountChanges > 0 &&
            !Settings.Default.SingleBackupMode) BackupEngine.SnapshotBackupCode();
        else if (Settings.Default.AutoBackupEnabled && Settings.Default.AutoBackupLocation != null &&
                 BackupEngine.CountChanges > 0 && Settings.Default.SingleBackupMode && Settings.Default.ArchiveBackups)
            BackupEngine.SingleBackupArchive();
        if (Settings.Default.CompatMode)
        {
            IoController.DirectoryCopy("C:\\Immortal Sins\\Config\\", ConfigArea, true);
            Directory.Delete("C:\\Immortal Sins\\", true);
        }
        Close();
    }

2 个答案:

答案 0 :(得分:1)

使用IsEnabled,而不是IsEnabled! {{1}}始终为true,因为它仅检查是否启用了复选框。如果用户选中复选框,则必须使用IsChecked属性。 文档:ToggleButton.IsChecked

答案 1 :(得分:0)

我发现了这个问题。还有另一个组件一直发送true(和false),因为该组件上的数据绑定是TwoWay,而不是OneWay。这一点,以及迈克的解决方案解决了这个问题。