How to use general C# code to implement the functionality of the Python code? The following code was unsuccessful.
python code:
// NuGet Package References: Interop.UIAutomationClient
using System;
using System.Runtime.InteropServices;
using Interop.UIAutomationClient;
void Main()
{
var controller = new JianyingController();
try
{
controller.OpenDraft("3月8日");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
controller.Dispose();
}
}
public class ControlFinder
{
public static IUIAutomationCondition CreateDescriptionMatcher(
IUIAutomation automation,
string targetDesc,
int depth = 2,
bool exact = false)
{
targetDesc = targetDesc.ToLower();
// Use UIA_FullDescriptionPropertyId (30159)
var propertyId = 30159;
var propertyCondition = automation.CreatePropertyCondition(
propertyId,
targetDesc
);
return propertyCondition;
}
}
public class JianyingController : IDisposable
{
private readonly IUIAutomation _automation;
private IUIAutomationElement _appWindow;
public JianyingController()
{
// Create an instance of CUIAutomation
_automation = new CUIAutomation();
_appWindow = FindJianyingWindow();
if (_appWindow == null)
{
throw new Exception("Failed to find the Jianying window.");
}
}
private IUIAutomationElement FindJianyingWindow()
{
// Get the desktop root element
var rootElement = _automation.GetRootElement();
// Create search condition: find by window name
var condition = _automation.CreatePropertyCondition(
UIA_PropertyIds.UIA_NamePropertyId,
"剪映专业版"
);
// Find the Jianying window
return rootElement.FindFirst(Interop.UIAutomationClient.TreeScope.TreeScope_Children, condition);
}
public void OpenDraft(string draftName)
{
Console.WriteLine($"Attempting to open the draft: {draftName}");
// Create a search condition for the draft title
var draftTitleCondition = _automation.CreatePropertyCondition(
UIA_PropertyIds.UIA_AutomationIdPropertyId,
$"HomePageDraftTitle:{draftName}"
);
// Search for the draft element
var draftElement = _appWindow.FindFirst(
Interop.UIAutomationClient.TreeScope.TreeScope_Descendants,
draftTitleCondition
);
if (draftElement == null)
{
throw new Exception($"Failed to find the draft named {draftName}.");
}
// Get the parent button element
var walker = _automation.ControlViewWalker;
var draftButton = walker.GetParentElement(draftElement);
if (draftButton == null)
{
throw new Exception("Failed to find the parent button element of the draft.");
}
// Get the Invoke Pattern and click
var invokePattern = draftButton.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)
as IUIAutomationInvokePattern;
if (invokePattern == null)
{
throw new Exception("Failed to retrieve the Invoke pattern of the button.");
}
invokePattern.Invoke();
}
public void Dispose()
{
if (_appWindow != null)
{
Marshal.ReleaseComObject(_appWindow);
_appWindow = null;
}
if (_automation != null)
{
Marshal.ReleaseComObject(_automation);
}
}
}
// Common UIA Property IDs
public static class UIA_PropertyIds
{
public const int UIA_NamePropertyId = 30005;
public const int UIA_AutomationIdPropertyId = 30011;
public const int UIA_FullDescriptionPropertyId = 30159;
}
// Common UIA Pattern IDs
public static class UIA_PatternIds
{
public const int UIA_InvokePatternId = 10000;
}python code:
import uiautomation as uia
class ControlFinder:
"""Control Finder that encapsulates logic related to control search"""
@staticmethod
def desc_matcher(target_desc: str, depth: int = 2, exact: bool = False) -> Callable[[uia.Control, int], bool]:
"""Matcher function to find controls based on full_description"""
target_desc = target_desc.lower()
def matcher(control: uia.Control, _depth: int) -> bool:
if _depth != depth:
return False
full_desc: str = control.GetPropertyValue(30159).lower()
return (target_desc == full_desc) if exact else (target_desc in full_desc)
return matcher
class Jianying_controller:
"""Jianying Controller"""
app: uia.WindowControl
"""Jianying Window"""
def open_draft(self, draft_name: str) -> None:
"""Open the specified Jianying draft
Args:
draft_name (`str`): The name of the Jianying draft to be opened
"""
print(f"Starting to open {draft_name}")
# Click the corresponding draft
draft_name_text = self.app.TextControl(
searchDepth=2,
Compare=ControlFinder.desc_matcher(f"HomePageDraftTitle:{draft_name}", exact=True)
)
if not draft_name_text.Exists(0):
raise exceptions.DraftNotFound(f"Could not find the Jianying draft named {draft_name}")
draft_btn = draft_name_text.GetParentControl()
assert draft_btn is not None
draft_btn.Click(simulateMove=False)