Şu kodlarımda treeview içinden seçili olan checboxları alır ve onlara ait olan batch…
- User
Şu kodlarımda treeview içinden seçili olan checboxları alır ve onlara ait olan batch kodlarıyla bir bat dosyası oluşturu ancak sadece 1. checbox da veriyi eklemiyor neden ? using System.IO; using System.Collections.Generic; using Microsoft.Win32; using Microsoft.Dism; using System.Text.Json; using ShadesToolkit.ViewModels.Pages; using Wpf.Ui.Controls; using System.Diagnostics; using System.ComponentModel; using System.Collections.ObjectModel; using System.Globalization; using System.Windows.Media; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; using System.Text; using System.Windows.Controls; using TreeViewItem = System.Windows.Controls.TreeViewItem;
namespace ShadesToolkit.Views.Pages { public partial class ExtraDebloat : INavigableView { private string MountFolder = Path.Combine(Path.GetTempPath(), "MountFolder");
public ExtraDebloatViewModel ViewModel { get; } private readonly Dictionary<CheckBox, string> checkBoxScripts = new Dictionary<CheckBox, string>(); public ExtraDebloat(ExtraDebloatViewModel viewModel) { ViewModel = viewModel; DataContext = this; InitializeComponent(); checkBoxScripts.Add(OneDrive, "%MinSudo% --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*OneDrive*\"\""); checkBoxScripts.Add(iki, "echo Batch Script 2"); checkBoxScripts.Add(uc, "echo Batch Script 3"); checkBoxScripts.Add(dort, "echo Batch Script 4"); } private void SaveButton_Click(object sender, RoutedEventArgs e) { StringBuilder scriptContent = new StringBuilder(); // Bat dosyasının en başına "echo off" ekler scriptContent.AppendLine("@echo off"); scriptContent.AppendLine("set InstallDir=C:\\Users\\%USERNAME%\\AppData\\Local\\Temp\\MountFolder"); // Her bir TreeViewItem için işleme foreach (TreeViewItem treeViewItem in treeView.Items) { ProcessTreeViewItem(treeViewItem, scriptContent); } if (scriptContent.Length > "@echo off".Length + Environment.NewLine.Length) { SaveScriptToFile(scriptContent.ToString()); } else { MessageBox.Show("Hiçbir komut seçilmedi."); } } private void ProcessTreeViewItem(ItemsControl itemsControl, StringBuilder scriptContent) { foreach (object obj in itemsControl.Items) { TreeViewItem subItem = itemsControl.ItemContainerGenerator.ContainerFromItem(obj) as TreeViewItem; if (subItem != null) { // Alt TreeViewItem’ı işle ProcessTreeViewItem(subItem, scriptContent); } CheckBox checkBox = obj as CheckBox; if (checkBox != null && checkBox.IsChecked == true) { // Sözlük yerine direkt batch komutu ekleyebiliriz scriptContent.AppendLine(checkBox.Content.ToString() + " komutu çalıştırılacak."); } } } private void SaveScriptToFile(string content) { string filePath = "tweaker.bat"; File.WriteAllText(filePath, content); MessageBox.Show(filePath + " kaydedildi."); } }}
- Assistant
Öncelikle kodunuz oldukça karmaşık ve bazı kısımları eksik. Koda baktığımda,
TreeViewöğelerindenCheckBoxöğelerini ayıklamak için yazılmış bir mantık göremiyorum. Kod özellikleProcessTreeViewItemfonksiyonunda TreeViewItem'lar ile çalışmakta ancak CheckBox öğeleri ile bir ilişkilendirme kurmamış.İşte bazı olası sorunlar ve çözümler:
-
checkBoxScriptssözlüğündenCheckBoxkontrolüne karşılık gelen komutu almanız gerekir, ancak sizscriptContent'e CheckBox'un içeriğini metin olarak ekliyorsunuz. KodunuzdacheckBoxScriptskullanılmamıştır. -
İlk
CheckBox, yaniOneDriveadındakine yönelik belirli bir işlem yapılmadığını görüyorum. İlkCheckBox'ın kodunuzda özel olarak işlenmediğini doğrulamak lazım. -
checkBox.Content.ToString()ifadesi, CheckBox'ın gerçek içeriğini alır, ancak bu durumda çalıştırılacak gerçek batch komutu değil. Bunun yerine, CheckBox'ın işaretli olduğunu ve komutuncheckBoxScriptssözlüğünden alınması gerektiğini kontrol etmelisiniz.
İlk
CheckBox'ın işlenmemesi sorunu genellikle bir mantık hatası veya if bloğunun içine girmemesi gibi bir sebepten olabilir.Şu değişikliklerle, kodunuzun düzeltilmiş bir versiyonunu aşağıda bulabilirsiniz:
private void ProcessTreeViewItem(ItemsControl itemsControl, StringBuilder scriptContent) { foreach (object obj in itemsControl.Items) { TreeViewItem subItem = itemsControl.ItemContainerGenerator.ContainerFromItem(obj) as TreeViewItem; if (subItem != null) { // Alt TreeViewItem'ı işle ProcessTreeViewItem(subItem, scriptContent); } CheckBox checkBox = obj as CheckBox; if (checkBox != null && checkBox.IsChecked == true) { string script; if (checkBoxScripts.TryGetValue(checkBox, out script)) { // CheckBox'a karşılık gelen batch komutunu sözlükten al ve ekleyebiliriz. scriptContent.AppendLine(script); } } } }Bu kod parçası, her bir
CheckBoxöğesi için sözlükten karşılık gelen komutu almakta vescriptContent'e eklemektedir. Kodunuzda güncelleme yaparken, tüm fonksiyonları ve bağlantıları dikkatlice kontrol etmenizi öneririm. İlkCheckBox'a ait işlemi bu kod parçası içermekte ve eğer sözlük doğru şekilde tanımlanmışsa, işaretli olanCheckBoxiçin karşılık gelen komut eklenmiş olacaktır.Eğer yine de birinci
CheckBox'ın değeri alınmıyorsa, bu durumdaTreeViewItem'ın doğru şekildeCheckBoxile ilişkilendirilmediğini veya kullanıcı arabirimi bağlamının beklenildiği gibi oluşturulmadığını kontrol etmelisiniz. Daha fazla yardım için tam kodunuzu ve kullanıcı arabirimi detaylarını paylaşmanız faydalı olabilir. -
- User
birde ben şöyle aynı checbox etkinse birden fazla batch kodu tanımlamak istiyorum public ExtraDebloat(ExtraDebloatViewModel viewModel) { ViewModel = viewModel; DataContext = this;
InitializeComponent(); checkBoxScripts.Add(OneDrive, "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*OneDrive*\"\""); checkBoxScripts.Add(OneDrive, "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*onedrive-backup*\"\""); checkBoxScripts.Add(iki, "echo Batch Script 2"); checkBoxScripts.Add(uc, "echo Batch Script 3"); checkBoxScripts.Add(dort, "echo Batch Script 4");}
- Assistant
Eğer aynı
CheckBoxiçin birden fazla batch kodu tanımlamak istiyorsanız, birCheckBoxanahtarına birden fazla değer eşlemek için birList<string>veyastring[]yapısı kullanarakDictionary<CheckBox, List<string>>veyaDictionary<CheckBox, string[]>şeklinde bir sözlük oluşturmalısınız.Dictionaryyapısında, her anahtar eşsiz olmalıdır ve sadece bir değere sahip olabilir. İsteğinizi yerine getirmek için kodunuzu şu şekilde düzenleyebilirsiniz:public partial class ExtraDebloat : INavigableView<ExtraDebloatViewModel> { private string MountFolder = Path.Combine(Path.GetTempPath(), "MountFolder"); public ExtraDebloatViewModel ViewModel { get; } private readonly Dictionary<CheckBox, List<string>> checkBoxScripts = new Dictionary<CheckBox, List<string>>(); public ExtraDebloat(ExtraDebloatViewModel viewModel) { ViewModel = viewModel; DataContext = this; InitializeComponent(); // Her CheckBox için birden fazla komut ekleyebilirsiniz. checkBoxScripts.Add(OneDrive, new List<string> { "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*OneDrive*\"\"", "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*onedrive-backup*\"\"" }); checkBoxScripts.Add(iki, new List<string> { "echo Batch Script 2" }); checkBoxScripts.Add(uc, new List<string> { "echo Batch Script 3" }); checkBoxScripts.Add(dort, new List<string> { "echo Batch Script 4" }); } // Diğer fonksiyonlarınız... private void ProcessTreeViewItem(ItemsControl itemsControl, StringBuilder scriptContent) { foreach (object obj in itemsControl.Items) { TreeViewItem subItem = itemsControl.ItemContainerGenerator.ContainerFromItem(obj) as TreeViewItem; if (subItem != null) { // Alt TreeViewItem'ı işle ProcessTreeViewItem(subItem, scriptContent); } CheckBox checkBox = obj as CheckBox; if (checkBox != null && checkBox.IsChecked == true) { List<string> scripts; if (checkBoxScripts.TryGetValue(checkBox, out scripts)) { foreach (var script in scripts) { // CheckBox'a karşılık gelen her batch komutunu ekleyebiliriz. scriptContent.AppendLine(script); } } } } } }ProcessTreeViewItemişlevi artıkCheckBoxöğesi için tanımlanan her bir komutuscriptContentStringBuilder nesnesine ekleyecektir. Bu,CheckBox'ın işaretli olup olmadığını kontrol ettikten sonra, eğer işaretliyse, sözlükten alınan vecheckBoxScriptsiçindeki her bir komut için döngü yapılacaktır. Her bir komut,StringBuildernesnesine eklenir. Bu sayede birCheckBoxbirden fazla komutla ilişkilendirilmiş olacaktır.