WPF project automatic optimization method and system
By automatically identifying and deleting unused resources and functions in WPF projects, the problem of inefficiency of traditional cleaning methods is solved, project performance and maintenance are improved, and efficient resource management is achieved.
Patent Information
- Application Number
- CN202411141066.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-20
- Publication Date
- 2025-01-03
AI Technical Summary
During the development of WPF projects, unused pictures, resources or functions occupy storage space and affect compilation and operation efficiency. Traditional cleaning methods rely on manual inspection, which is inefficient and easy to miss.
Using an automated method, by scanning the project directory, establishing a multimedia resource and xaml resource information dictionary, analyzing source code files, identifying and deleting unused resources and functions, including using Roslyn's semantic model analysis function to ensure accuracy and before deletion Backup resources.
It improves the performance and maintenance of WPF projects, reduces storage space usage, replaces manual inspection, avoids omissions, and improves work efficiency.
Smart Images

Figure CN119248274A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a WPF project automatic optimization method and system. Background Art
[0002] During the development of WPF (Windows Presentation Foundation) projects, some unused images, resources or functions are often left in the project files, which not only take up storage space, but also affect the compilation and operation efficiency of the project. The usual cleaning and optimization methods rely on manual inspection, which has problems such as low work efficiency and easy omissions. Summary of the invention
[0003] In order to solve the defects of the prior art, the present invention provides a WPF project automatic optimization method and system.
[0004] In order to solve the above technical problems, the present invention adopts the following technical solutions:
[0005] In a first aspect, an embodiment of the present invention provides a WPF project automatic optimization method, comprising the following steps:
[0006] Scan the WPF project directory and all its subdirectories, and create a multimedia resource information dictionary for the scanned multimedia resources, wherein the multimedia resource information dictionary includes a multimedia resource file name and a multimedia resource path;
[0007] Use XDocument to parse the project file of the WPF project to establish a xaml resource information dictionary, wherein the xaml resource information dictionary includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path;
[0008] Read all source code files of the WPF project and generate a source code file content dictionary, wherein the source code file content dictionary includes source code text content and source code file path;
[0009] Based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary, obtaining unused multimedia resources and unused xaml resources;
[0010] Deleting the unused multimedia resources and unused xaml resources from the WPF project;
[0011] In the project file of the WPF project, remove the reference to the deleted multimedia resource.
[0012] In some embodiments, it also includes:
[0013] Use Roslyn's MSBuildWorkspace to load the WPF project, obtain a syntax tree, and then traverse the syntax tree to generate function definition information;
[0014] Based on the function definition information, traverse the syntax tree using the semantic model analysis function of Roslyn to obtain unused functions;
[0015] The unused functions are deleted from the WPF project.
[0016] In some embodiments, it also includes:
[0017] Based on the unused functions and the source code file content dictionary, it is confirmed that the unused functions are indeed unused.
[0018] In some embodiments, it also includes:
[0019] Before deleting the unused multimedia resources, unused xaml resources and unused functions from the WPF project, the unused multimedia resources, unused xaml resources and unused functions are backed up.
[0020] In some embodiments, the use of XDocument to parse the engineering file of the WPF project to establish a xaml resource information dictionary, the xaml resource information dictionary includes key attribute values, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path including:
[0021] Use XDocument to parse the project file of the WPF project to generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path;
[0022] Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, and all ResourceDictionary elements are found to establish a xaml resource information dictionary, which includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path.
[0023] In a second aspect, an embodiment of the present invention provides a WPF project automatic optimization system, including:
[0024] A multimedia resource information dictionary building module is used to scan the WPF project directory and all its subdirectories, and build a multimedia resource information dictionary for the scanned multimedia resources, wherein the multimedia resource information dictionary includes the multimedia resource file name and the multimedia resource path;
[0025] A xaml resource information dictionary building module is used to use XDocument to parse the engineering file of the WPF project to build a xaml resource information dictionary, wherein the xaml resource information dictionary includes a key attribute value, a starting position of the xaml resource, an ending position of the xaml resource and a xaml file path;
[0026] A source code file content dictionary generation module is used to read all source code files of a WPF project and generate a source code file content dictionary, wherein the source code file content dictionary includes source code text content and source code file path;
[0027] An unused resource acquisition module, used for obtaining unused multimedia resources and unused xaml resources based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary;
[0028] An unused resource deletion module is used to delete the unused multimedia resources and unused xaml resources from the WPF project;
[0029] The multimedia resource reference removal module is used to remove references to deleted multimedia resources in the project files of WPF projects.
[0030] In some embodiments, it also includes:
[0031] The unused function acquisition module is used to load the WPF project using the MSBuildWorkspace of Roslyn to obtain a syntax tree, and then traverse the syntax tree to generate function definition information;
[0032] Based on the function definition information, traverse the syntax tree using the semantic model analysis function of Roslyn to obtain unused functions;
[0033] The unused function deletion module is used to delete the unused functions from the WPF project.
[0034] In some embodiments, it also includes:
[0035] The unused function confirmation module is used to confirm that the unused function is indeed unused based on the unused function and the source code file content dictionary.
[0036] In some embodiments, it also includes:
[0037] The backup module is used to back up the unused multimedia resources, unused xaml resources and unused functions before deleting them from the WPF project.
[0038] In some embodiments, the use of XDocument to parse the engineering file of the WPF project to establish a xaml resource information dictionary, the xaml resource information dictionary includes key attribute values, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path including:
[0039] Use XDocument to parse the project file of the WPF project to generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path;
[0040] Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, and all ResourceDictionary elements are found to establish a xaml resource information dictionary, which includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path.
[0041] The present invention provides a WPF project automatic optimization method and system. Compared with the prior art, the present invention has the following technical effects:
[0042] 1. The embodiment of the present invention analyzes the project file, automatically finds unused multimedia resources and xaml resources, then deletes these unused resources, and finally compiles the project and gives the result, thereby replacing manual inspection, improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0043] 2. The embodiment of the present invention analyzes the project file, automatically searches for unused multimedia resources, xaml resources and functions, then deletes these unused resources, and finally compiles the project and gives the result, thereby replacing manual inspection, further reducing the storage space required for the project, further improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0044] 3. Before deleting unused multimedia resources, unused xaml resources and unused functions from the WPF project, back up the unused multimedia resources, unused xaml resources and unused functions so that they can be restored when necessary, thereby further improving the maintainability of the WPF project. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description only involve some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0046] Figure 1 This is a flow chart of a WPF project automation optimization method according to an embodiment of the present invention;
[0047] Figure 2 A flowchart of another WPF project automatic optimization method according to an embodiment of the present invention;
[0048] Figure 3 This is a schematic diagram of the structure of a WPF project automation optimization system according to an embodiment of the present invention;
[0049] Figure 4 A schematic diagram of another WPF project automation optimization system structure according to an embodiment of the present invention;
[0050] Figure 5 The figure is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0051] In order to enable those skilled in the art to better understand the solution of the present invention, the technical solution in the embodiment of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is only a part of the embodiment of the present invention, not all of the embodiments. Based on the embodiment of the present invention, other solutions obtained by ordinary technicians in this field without creative work should belong to the protection scope of the present invention.
[0052] In order to facilitate those skilled in the art to understand the embodiments of the present invention, the technical terms involved in the present invention are explained below.
[0053] WPF projects are usually used to develop Windows desktop applications with rich functions and excellent visual effects. They have the following main features:
[0054] 1. Use XML-based markup language to define the user interface, so that the interface design and logic code are separated;
[0055] 2. Built-in support for animation effects and multimedia content;
[0056] 3. Flexible layout system, supporting complex UI design.
[0057] XDocument is a class for manipulating XML documents, commonly used in the .NET platform. It provides a convenient way to create, edit, and process the content of XML documents. In C#, the XDocument class is located in the System.Xml.Linq namespace and is part of the LINQ to XML API.
[0058] Roslyn is a compiler platform developed by Microsoft for C# and Visual Basic.NET languages. It is an open source project that provides developers with powerful code analysis and code generation tools.
[0059] XAML (eXtensible Application Markup Language) is an XML-based markup language that is mainly used to describe user interfaces and declarative programming. It is widely used in various Microsoft technologies, especially in frameworks such as Windows Presentation Foundation (WPF), Universal Windows Platform (UWP), and Xamarin.Forms.
[0060] Example 1
[0061] Figure 1 A flowchart of a WPF project automatic optimization method according to an embodiment of the present invention is shown. Figure 1 It can be seen that the WPF project automatic optimization method includes the following steps:
[0062] S10, scanning the WPF project directory and all its subdirectories, and establishing a multimedia resource information dictionary for the scanned multimedia resources, wherein the multimedia resource information dictionary includes a multimedia resource file name and a multimedia resource path.
[0063] Use the Directory.EnumerateFiles method in C# with the SearchOption.AllDirectories option to recursively scan the WPF project directory and all its subdirectories. Preferably, use the "*.*" pattern to match all multimedia resources. Scan and filter all multimedia resource files, such as pictures, audio, video, etc., according to the suffix name. Create an index entry for each scanned multimedia resource, record the multimedia resource name and multimedia resource path, and thereby create a multimedia resource dictionary for all scanned multimedia resources. The multimedia resource information dictionary includes the multimedia resource file name and the multimedia resource path.
[0064] S11. Use XDocument to parse the engineering file of the WPF project to establish a xaml resource information dictionary, wherein the xaml resource information dictionary includes a key attribute value, a starting position of the xaml resource, an ending position of the xaml resource, and a xaml file path.
[0065] Preferably, traverse the WPF project directory and its subdirectories to search for all project files, such as .csproj files, and for each project file, use XDocument to parse and generate an xml structure, traverse the xml structure to find all xaml files in the WPF project, and generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path;
[0066] Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, all ResourceDictionary elements are found, and for each ResourceDictionary element, all its child elements are traversed to check whether there is a Key attribute. If the Key attribute is included, a xaml resource is found, and then the start and end positions of the xaml resource are matched through a regular expression to establish a xaml resource information dictionary, which includes the key attribute value, the start position of the xaml resource, the end position of the xaml resource and the xaml file path.
[0067] S12. Read all source code files of the WPF project and generate a source code file content dictionary, wherein the source code file content dictionary includes source code text content and source code file path.
[0068] Preferably, all source code files of the WPF project, such as files with suffixes such as .cs and .xaml, are traversed, and the source code file contents are read using the File.ReadAllText method and saved in a string to generate a source code file content dictionary, which includes source code text content and source code file path.
[0069] S13. Obtain unused multimedia resources and unused xaml resources based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary.
[0070] Preferably, in the source code text content of the source code file content dictionary, the multimedia resource file names of the multimedia resource information dictionary are searched using matching or regular expressions, and the searched multimedia resource files are marked as used multimedia resources, and the rest are unused multimedia resources.
[0071] Preferably, in the source code text content of the source code file content dictionary, regular matching or string matching is used to search for the key attribute value of the xaml resource information dictionary, and the searched xaml resources are marked as used xaml resources, and the rest are unused xaml resources.
[0072] S14. Delete the unused multimedia resources and unused xaml resources from the WPF project.
[0073] Preferably, the search results are displayed to the user, and unused multimedia resources and unused xaml resources are reviewed to confirm that they are indeed useless. An entry for deleting a single multimedia resource and xaml resource is provided for the user to select and delete the file.
[0074] The unused multimedia resources and unused xaml resources are deleted from the WPF project. The system completes the cleanup process and provides a cleanup report, which includes detailed information about the deleted resources, and also records the backup location and any possible problems if there is a backup.
[0075] S15. In the project file of the WPF project, remove the reference to the deleted multimedia resource.
[0076] Preferably, modify the engineering file of the WPF project, remove the reference to the deleted multimedia, and save the modification. Then compile the WPF project, if the compilation is successful, give the compilation result and end the optimization process, otherwise give an error message and remind to manually modify the error.
[0077] This embodiment analyzes the project file, automatically finds unused multimedia resources and xaml resources, then deletes these unused resources, and finally compiles the project and gives the result, thereby replacing manual inspection, improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0078] Figure 2 Another WPF project automatic optimization method flow chart of the embodiment of the present invention is shown. Figure 2 It can be seen that the WPF project automatic optimization method also includes the following steps:
[0079] S16. Use the MSBuildWorkspace of Roslyn to load the WPF project, obtain a syntax tree, and then traverse the syntax tree to generate function definition information.
[0080] Preferably, the MSBuild API is used to load and analyze the solution file, such as a .sln file. The solution file is loaded using Microsoft.Build.Construction.SolutionFile. The MSBuild API provides the SolutionFile class for parsing and operating the solution file. Once the solution file is loaded, the WPF project list contained therein can be accessed, and further operations can be performed on each WPF project.
[0081] Load the solution file through the SolutionFile.Prase method of the solution object and access the list of WPF projects contained in it. Iterate through these WPF projects and access the detailed information of each WPF project, including WPF project references.
[0082] For each WPF project, load the WPF project file using Microsoft.Build.Evaluation.ProjectCollection and the WPF project file path.
[0083] Using the WPF project information obtained through the MSBuild API, a Roslyn workspace is created, and the solution is loaded using Roslyn's MSBuildWorkspace. Each WPF project therein is traversed, and for each of the WPF projects, a syntax tree is obtained. The syntax tree is then traversed to generate function definition information, including function name, parameters, access modifiers, etc.
[0084] S17. Based on the function definition information, the syntax tree is traversed using the semantic model analysis function of Roslyn to obtain unused functions.
[0085] Preferably, the semantic model analysis function of Roslyn is used to traverse all syntax trees in the workspace and check the calling status of functions, that is, to find unused functions.
[0086] By analyzing the syntax tree, Roslyn can build a semantic model of the source code, which provides semantic-level details such as type information, definition, and reference of symbols in the code. Using the semantic model, you can determine how symbols (such as variables, methods, etc.) in the code are defined and used.
[0087] Use the semantic model to analyze each symbol (variable, method, class, etc.) in the code to check whether they are referenced in other parts of the code base. Any symbol that is not referenced can be considered unused code.
[0088] In another embodiment, the following steps are also included:
[0089] Based on the unused functions and the source code file content dictionary, it is confirmed again that the unused functions are indeed not used by any code path, so that the result of the unused function search process is more accurate.
[0090] S18. Delete the unused functions from the WPF project.
[0091] Preferably, for the functions confirmed to be unused, Roslyn is used to determine their specific locations in the source code files. A report is then prepared, including detailed information of the unused functions, and provided to the user or project team for confirmation of whether to delete them. After the user confirms, the unused functions are deleted from the WPF project according to the above analysis results.
[0092] Based on Example 1, this embodiment adds function optimization, that is, this embodiment analyzes the project file, automatically finds unused multimedia resources, xaml resources and functions, and then deletes these unused resources, and finally compiles the project and gives the results, thereby replacing manual inspection, further reducing the storage space required for the project, further improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0093] In another embodiment, the following steps are also included:
[0094] Before deleting the unused multimedia resources, unused xaml resources and unused functions from the WPF project, the unused multimedia resources, unused xaml resources and unused functions are backed up so as to be restored when necessary.
[0095] Example 2
[0096] Figure 3 A schematic diagram of the structure of a WPF project automation optimization system according to an embodiment of the present invention is shown. Figure 3 It can be seen that the WPF project automatic optimization system includes: a multimedia resource information dictionary establishment module 20, a xaml resource information dictionary establishment module 21, a source code file content dictionary generation module 22, an unused resource acquisition module 23, an unused resource deletion module 24 and a multimedia resource reference removal module 25.
[0097] The multimedia resource information dictionary building module 20 is used to scan the WPF project directory and all its subdirectories, and build a multimedia resource information dictionary for the scanned multimedia resources. The multimedia resource information dictionary includes the multimedia resource file name and the multimedia resource path.
[0098] Use the Directory.EnumerateFiles method in C# with the SearchOption.AllDirectories option to recursively scan the WPF project directory and all its subdirectories. Preferably, use the "*.*" pattern to match all multimedia resources. Scan and filter all multimedia resource files, such as pictures, audio, video, etc., according to the suffix name. Create an index entry for each scanned multimedia resource, record the multimedia resource name and multimedia resource path, and thereby create a multimedia resource dictionary for all scanned multimedia resources. The multimedia resource information dictionary includes the multimedia resource file name and the multimedia resource path.
[0099] The xaml resource information dictionary building module 21 is used to use XDocument to parse the engineering file of the WPF project to build a xaml resource information dictionary, and the xaml resource information dictionary includes key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path.
[0100] Preferably, traverse the WPF project directory and its subdirectories to search for all project files, such as .csproj files, and for each project file, use XDocument to parse and generate an xml structure, traverse the xml structure to find all xaml files in the WPF project, and generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path;
[0101] Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, all ResourceDictionary elements are found, and for each ResourceDictionary element, all its child elements are traversed to check whether there is a Key attribute. If the Key attribute is included, a xaml resource is found, and then the start and end positions of the xaml resource are matched through a regular expression to establish a xaml resource information dictionary, which includes the key attribute value, the start position of the xaml resource, the end position of the xaml resource and the xaml file path.
[0102] The source code file content dictionary generation module 22 is used to read all source code files of the WPF project and generate a source code file content dictionary. The source code file content dictionary includes source code text content and source code file path.
[0103] Preferably, all source code files of the WPF project, such as files with suffixes such as .cs and .xaml, are traversed, and the source code file contents are read using the File.ReadAllText method and saved in a string to generate a source code file content dictionary, which includes source code text content and source code file path.
[0104] The unused resource acquisition module 23 is used to obtain unused multimedia resources and unused xaml resources based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary.
[0105] Preferably, in the source code text content of the source code file content dictionary, the multimedia resource file names of the multimedia resource information dictionary are searched using matching or regular expressions, and the searched multimedia resource files are marked as used multimedia resources, and the rest are unused multimedia resources.
[0106] Preferably, in the source code text content of the source code file content dictionary, regular matching or string matching is used to search for the key attribute value of the xaml resource information dictionary, and the searched xaml resources are marked as used xaml resources, and the rest are unused xaml resources.
[0107] The unused resource deletion module 24 is used to delete the unused multimedia resources and unused xaml resources from the WPF project.
[0108] Preferably, the search results are displayed to the user, and unused multimedia resources and unused xaml resources are reviewed to confirm that they are indeed useless. An entry for deleting a single multimedia resource and xaml resource is provided for the user to select and delete the file.
[0109] The unused multimedia resources and unused xaml resources are deleted from the WPF project. The system completes the cleanup process and provides a cleanup report, which includes detailed information about the deleted resources, and also records the backup location and any possible problems if there is a backup.
[0110] The multimedia resource reference removal module 25 is used to remove references to deleted multimedia resources in the project file of the WPF project.
[0111] Preferably, modify the engineering file of the WPF project, remove the reference to the deleted multimedia, and save the modification. Then compile the WPF project, if the compilation is successful, give the compilation result and end the optimization process, otherwise give an error message and remind to manually modify the error.
[0112] This embodiment analyzes the project file, automatically finds unused multimedia resources and xaml resources, then deletes these unused resources, and finally compiles the project and gives the result, thereby replacing manual inspection, improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0113] Figure 4 Another schematic diagram of the WPF project automation optimization system structure of the embodiment of the present invention is shown. Figure 4 It can be seen that the WPF project automation optimization system also includes:
[0114] The unused function acquisition module 26 is used to load the WPF project using the MSBuildWorkspace of Roslyn to obtain a syntax tree, and then traverse the syntax tree to generate function definition information.
[0115] Preferably, the MSBuild API is used to load and analyze the solution file, such as a .sln file. The solution file is loaded using Microsoft.Build.Construction.SolutionFile. The MSBuild API provides the SolutionFile class for parsing and operating the solution file. Once the solution file is loaded, the WPF project list contained therein can be accessed, and further operations can be performed on each WPF project.
[0116] Load the solution file through the SolutionFile.Prase method of the solution object and access the list of WPF projects contained in it. Iterate through these WPF projects and access the detailed information of each WPF project, including WPF project references.
[0117] For each WPF project, load the WPF project file using Microsoft.Build.Evaluation.ProjectCollection and the WPF project file path.
[0118] Using the WPF project information obtained through the MSBuild API, a Roslyn workspace is created, and the solution is loaded using Roslyn's MSBuildWorkspace. Each WPF project therein is traversed, and for each of the WPF projects, a syntax tree is obtained. The syntax tree is then traversed to generate function definition information, including function name, parameters, access modifiers, etc.
[0119] Based on the function definition information, traverse the syntax tree using the semantic model analysis function of Roslyn to obtain unused functions;
[0120] Preferably, the semantic model analysis function of Roslyn is used to traverse all syntax trees in the workspace and check the calling status of functions, that is, to find unused functions.
[0121] By analyzing the syntax tree, Roslyn can build a semantic model of the source code, which provides semantic-level details such as type information, definition, and reference of symbols in the code. Using the semantic model, you can determine how symbols (such as variables, methods, etc.) in the code are defined and used.
[0122] Use the semantic model to analyze each symbol (variable, method, class, etc.) in the code to check whether they are referenced in other parts of the code base. Any symbol that is not referenced can be considered unused code.
[0123] In another embodiment, the WPF project automation optimization system further includes:
[0124] The unused function confirmation module is used to reconfirm that the unused function is indeed not used by any code path based on the unused function and the source code file content dictionary, so as to make the result of the unused function search process more accurate.
[0125] The unused function deletion module 27 is used to delete the unused functions from the WPF project.
[0126] Preferably, for the functions confirmed to be unused, Roslyn is used to determine their specific locations in the source code files. A report is then prepared, including detailed information of the unused functions, and provided to the user or project team for confirmation of whether to delete them. After the user confirms, the unused functions are deleted from the WPF project according to the above analysis results.
[0127] Based on Example 2, this embodiment adds function optimization, that is, this embodiment analyzes the project file, automatically finds unused multimedia resources, xaml resources and functions, and then deletes these unused resources, and finally compiles the project and gives the results, thereby replacing manual inspection, further reducing the storage space required for the project, further improving work efficiency, avoiding omissions, and thus improving the performance and maintainability of the WPF project.
[0128] In another embodiment, the WPF project automation optimization system further includes:
[0129] The backup module is used to back up the unused multimedia resources, unused xaml resources and unused functions before deleting them from the WPF project, so as to restore them when necessary.
[0130] Example 3
[0131] An embodiment of the present invention provides a readable storage medium, comprising: computer program instructions, which, when executed by at least one processor of an electronic device, enable the electronic device to implement the WPF project automatic optimization method provided by any of the above method embodiments.
[0132] Example 4
[0133] Figure 5 A schematic diagram of the structure of an electronic device according to an embodiment of the present invention is shown. Figure 5 It can be seen that an electronic device 50 includes: a memory 52 and a processor 54;
[0134] The memory 52 may be an independent physical unit, and may be connected to the processor 54 via a bus 56. The memory 52 and the processor 54 may also be integrated together and implemented via hardware.
[0135] The memory 52 is used to store program instructions, and the processor 54 calls the program instructions to execute the WPF project automatic optimization method provided by any of the above method embodiments.
[0136] Optionally, when part or all of the methods of the above embodiments are implemented by software, the above electronic device 50 may also only include a processor 54. The memory 52 for storing programs is located outside the electronic device 50, and the processor 54 is connected to the memory through circuits / wires to read and execute the programs stored in the memory.
[0137] The processor 54 may be a central processing unit (CPU), a network processor (NP), or a combination of a CPU and a NP.
[0138] The processor 54 may further include a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD) or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof.
[0139] The memory 52 may include a volatile memory, such as a random-access memory (RAM); the memory may also include a non-volatile memory, such as a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD); the memory may also include a combination of the above types of memory.
[0140] Example 5
[0141] An embodiment of the present invention provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the WPF project automatic optimization method of the above embodiment is implemented.
[0142] The above embodiments are preferred implementation modes of the present invention, but the implementation modes of the present invention are not limited to the above embodiments. Any other changes, modifications, substitutions, combinations, and simplifications that do not deviate from the spirit and principles of the present invention should be equivalent replacement methods and are included in the protection scope of the present invention.
Claims
1. A WPF project automatic optimization method, characterized in that: The steps include: Scan the WPF project directory and all its subdirectories, and create a multimedia resource information dictionary for the scanned multimedia resources, wherein the multimedia resource information dictionary includes a multimedia resource file name and a multimedia resource path; Use XDocument to parse the project file of the WPF project to establish a xaml resource information dictionary, wherein the xaml resource information dictionary includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path; Read all source code files of the WPF project and generate a source code file content dictionary, wherein the source code file content dictionary includes source code text content and source code file path; Based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary, obtaining unused multimedia resources and unused xaml resources; Deleting the unused multimedia resources and unused xaml resources from the WPF project; In the project file of the WPF project, remove the reference to the deleted multimedia resource.
2. The WPF project automation optimization method according to claim 1, characterized in that: Also includes: Use Roslyn's MSBuildWorkspace to load the WPF project, obtain a syntax tree, and then traverse the syntax tree to generate function definition information; Based on the function definition information, traverse the syntax tree using the semantic model analysis function of Roslyn to obtain unused functions; The unused functions are deleted from the WPF project.
3. The WPF project automation optimization method according to claim 2, characterized in that: Also includes: Based on the unused functions and the source code file content dictionary, it is confirmed that the unused functions are indeed unused.
4. The WPF project automation optimization method according to claim 3, characterized in that: Also includes: Before deleting the unused multimedia resources, unused xaml resources and unused functions from the WPF project, the unused multimedia resources, unused xaml resources and unused functions are backed up.
5. The WPF project automation optimization method according to claim 4, characterized in that: The method of using XDocument to parse the engineering file of the WPF project to establish a xaml resource information dictionary includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path: Use XDocument to parse the project file of the WPF project to generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path; Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, and all ResourceDictionary elements are found to establish a xaml resource information dictionary, which includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path.
6. A WPF project automation optimization system, characterized in that: include: A multimedia resource information dictionary building module is used to scan the WPF project directory and all its subdirectories, and build a multimedia resource information dictionary for the scanned multimedia resources, wherein the multimedia resource information dictionary includes the multimedia resource file name and the multimedia resource path; A xaml resource information dictionary building module is used to use XDocument to parse the engineering file of the WPF project to build a xaml resource information dictionary, wherein the xaml resource information dictionary includes a key attribute value, a starting position of the xaml resource, an ending position of the xaml resource and a xaml file path; A source code file content dictionary generation module is used to read all source code files of a WPF project and generate a source code file content dictionary, wherein the source code file content dictionary includes source code text content and source code file path; An unused resource acquisition module, used for obtaining unused multimedia resources and unused xaml resources based on the multimedia resource information dictionary, the xaml resource information dictionary and the source code file content dictionary; An unused resource deletion module is used to delete the unused multimedia resources and unused xaml resources from the WPF project; The multimedia resource reference removal module is used to remove references to deleted multimedia resources in the project files of WPF projects.
7. The WPF project automation optimization system according to claim 6, characterized in that: Also includes: The unused function acquisition module is used to load the WPF project using the MSBuildWorkspace of Roslyn to obtain a syntax tree, and then traverse the syntax tree to generate function definition information; Based on the function definition information, traverse the syntax tree using the semantic model analysis function of Roslyn to obtain unused functions; The unused function deletion module is used to delete the unused functions from the WPF project.
8. The WPF project automation optimization system according to claim 7, characterized in that: Also includes: The unused function confirmation module is used to confirm that the unused function is indeed unused based on the unused function and the source code file content dictionary.
9. The WPF project automation optimization system according to claim 8, characterized in that: Also includes: The backup module is used to back up the unused multimedia resources, unused xaml resources and unused functions before deleting them from the WPF project.
10. The WPF project automation optimization system according to claim 9, characterized in that: The method of using XDocument to parse the engineering file of the WPF project to establish a xaml resource information dictionary includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path: Use XDocument to parse the project file of the WPF project to generate a xaml file information dictionary, wherein the xaml file information dictionary includes the xaml file name and the xaml file path; Based on the xaml file information dictionary, the Load method of XDocument is used to load the xaml file, and all ResourceDictionary elements are found to establish a xaml resource information dictionary, which includes the key attribute value, the starting position of the xaml resource, the ending position of the xaml resource and the xaml file path.
