文章目录
- 1. 创建kanzi窗口插件
- 2. 业务逻辑
- 3. 关键代码
- 3.1 遍历本工程的绑定信息
- 3.2 遍历所有工程的绑定信息
- 3.3 DiguiSearchNode实现
- 3.4 双击跳转
- 4. 安装
背景:在kanzi studio里按Ctrl+F,可以查找,能查找节点/资源等等,但是不能查找绑定信息,十分的不方便。
所以做一个窗口插件,能够查询节点的绑定属性和绑定代码逻辑,甚至可以跨工程查询。
kanzi_6">1. 创建kanzi窗口插件
参考kanzi帮助文档创建窗口插件
按照帮助文档,一步步建立窗口插件工程。这里不一一赘述。
- Class1.cs里包含kanzi窗口插件
- UserControl1.xaml.cs 是wpf窗口
public partial class UserControl1 : UserControl, PluginWindow
2. 业务逻辑
插件窗口通过studio来遍历所有场景节点和预设件节点,获取节点的绑定信息,
将查询字符串与绑定属性/绑定代码比较,来判断是否查询正确,
将查询信息更新到表格中。
双击表格中某一行,会通过studio.Commands.SelectProjectItems选择对应的节点,让焦点跳过去。
3. 关键代码
3.1 遍历本工程的绑定信息
private void Button_Click(object sender, RoutedEventArgs e){SearchType = 1;// 清空 datagridthis.datagrid.Items.Clear();if (this.studio.ActiveProject == null) return;if (this.searchText.Text.Length <= 0) return;projectName = this.studio.ActiveProject.Name;{//item name, path, bind number // 遍历获取的绑定项Project activeProject = this.studio.ActiveProject;Screen screen = activeProject.Screen;if (screen != null){Node2D rootNode2D = screen.RootNode2D;if(rootNode2D != null){DiguiSearchNode(rootNode2D);}}PrefabLibrary prefabLibrary = activeProject.PrefabLibrary;if(prefabLibrary != null){// 遍历预制体库foreach (var item in prefabLibrary.Items){var rootNode = item.RootNode;DiguiSearchNode(rootNode);} } }}
3.2 遍历所有工程的绑定信息
private void Button_Click_1(object sender, RoutedEventArgs e){SearchType = 2;// 清空 datagridthis.datagrid.Items.Clear();if (this.searchText.Text.Length <= 0) return;var pIndex = 0;foreach (var project in this.studio.Solution.Projects){projectName = project.Name;projectIndex = pIndex;{//item name, path, bind number // 遍历获取的绑定项Project activeProject = project;Screen screen = activeProject.Screen;if (screen != null){Node2D rootNode2D = screen.RootNode2D;if (rootNode2D != null){DiguiSearchNode(rootNode2D);}}PrefabLibrary prefabLibrary = activeProject.PrefabLibrary;if (prefabLibrary != null){// 遍历预制体库foreach (var item in prefabLibrary.Items){var rootNode = item.RootNode;DiguiSearchNode(rootNode);}}}pIndex++;}}
3.3 DiguiSearchNode实现
private void DiguiSearchNode(Node node){// if (item.Name.Contains(this.searchText.Text)){// 假设 item 是 BindItem 类型var bindingHost = node as BindingHost;var number = 0;if (bindingHost!= null){number = bindingHost.Bindings.Count();for (int i = 0; i < number; i++){var currentBind = bindingHost.Bindings.ToList()[i];if (currentBind.Property.Name.IndexOf(this.searchText.Text, StringComparison.OrdinalIgnoreCase) >= 0|| currentBind.Code.IndexOf(this.searchText.Text, StringComparison.OrdinalIgnoreCase) >= 0){this.datagrid.Items.Add(new BindItem{ProjectIndex = projectIndex,ProjectName = projectName,Name = node.Name,Path = node.Path,BindNumber = i,BindPropertyName = currentBind.Property.Name,BindCode = currentBind.Code,item = node});}}}}foreach (var child in node.ChildNodes){DiguiSearchNode(child);}}
3.4 双击跳转
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e){try{if (datagrid.SelectedItem != null){var selectedItem = (BindItem)datagrid.SelectedItem; // 确保你的数据项类型为 BindItem// 确认 selectedItem 的字段在访问前不是 nullif (!string.IsNullOrEmpty(selectedItem.Name) && !string.IsNullOrEmpty(selectedItem.Path)){//this.studio.Log("双击了: " + selectedItem.Name + " " + selectedItem.Path);// 这里可以添加打开详细信息窗口的逻辑PluginCommand c;//this.studio.ExecutePluginCommand("ChangeSelectedItem", new List<ProjectItem> { selectedItem.item } );if(selectedItem.ProjectIndex < this.studio.Solution.Projects.Count()){this.studio.Solution.ActiveProject = this.studio.Solution.Projects.ToList()[selectedItem.ProjectIndex];this.studio.Commands.SelectProjectItems(new List<ProjectItem> { selectedItem.item });} }else{this.studio.Log("选中的项目有空字段。");}}else{this.studio.Log("没有选中任何项。");}}catch (InvalidCastException ex){// 捕获类型转换异常this.studio.Log("类型转换错误: " + ex.Message);}catch (NullReferenceException ex){// 捕获空引用异常this.studio.Log("空引用错误: " + ex.Message);}catch (Exception ex){// 捕获所有其他异常this.studio.Log("发生了未知错误: " + ex.Message);}}
4. 安装
编译后的dll文件需要拷贝到kanzi安装目录下的plugins文件夹下,重启kanzi,在菜单栏点击插件,打开插件窗口
搜索栏输入text,点Find In Project,可以查询到本工程绑定信息里有text的所有节点,双击自动跳转到这一个节点
搜索栏输入text,点Find In Projects,可以查询到所有工程绑定信息里有text的所有节点,双击自动跳转到这一个节点
如果双击最后一行,会跳转到第二个引用工程,跨工程跳转,会导致插件窗口关闭,重新打开即可。