2012-06-27 20 views
5

Dựa trên this answer Tôi đã tạo một tùy chỉnh DialogHandler để xử lý các hộp cảnh báo Javascript bật lên từ bên trong điều khiển WebBrowser.Watin DialogHandler đóng SaveFileDialog

Handler được đính kèm với trình duyệt WatiN IE được kế thừa, được gọi là ExtendedIeBrowser.

Vì lý do không xác định, DialogHandler của Watin can thiệp với Winforms SaveFiledialogs. SaveFileDialog được tự động đóng lại bằng cách quay lại DialogResult.Cancel. Điều kỳ lạ là Handle() của Trình xử lý tùy chỉnh không bao giờ được gọi. Chỉ có CanHandle() được gọi (hai lần) và trả về false, vì vậy hộp thoại không được xử lý, do đó, nó sẽ vẫn mở.

Có bất kỳ điều gì mà tôi có thể làm để thay đổi khắc phục hành vi kỳ lạ này không?

Đây là nguồn gốc ExtendedIeBrowser:

public class ExtendedIeBrowser : IE 
{ 
    private IntPtr hwnd; 
    public ExtendedIeBrowser(WebBrowser webBrowserControl) : base(webBrowserControl.ActiveXInstance, false) 
    { 
    } 

    public void Initialize(WebBrowser webBrowserControl) 
    { 
     hwnd = webBrowserControl.FindForm().Handle; 
     StartDialogWatcher(); 
    } 

    public override IntPtr hWnd { get { return hwnd; } } 

    protected override void Dispose(bool disposing) 
    { 
     hwnd = IntPtr.Zero; 
     base.Dispose(disposing); 
    } 
} 

Tiếp nối nguồn CustomPopupDialogHandler:

class CustomPopupDialogHandler : ReturnDialogHandler 
{ 
    protected static Logger _logger = LogManager.GetCurrentClassLogger(); 

    public override bool HandleDialog(Window window) 
    { 
     bool handled = false; 
     try 
     { 
      var button = GetWantedButton(window); 
     if (button != null) 
     { 
      button.Click(); 
     } 
      handled = true; 
     } 
     catch (Exception ex) 
     { 
      _logger.ErrorException("HandleDialog", ex); 
     } 
     return handled; 
    } 

    public override bool CanHandleDialog(Window window) 
    { 
     bool canHandle = false; 
     try 
     { 
     canHandle = GetWantedButton(window) != null; 
     } 
     catch (Exception ex) 
     { 
      _logger.ErrorException("CanHandleDialog", ex); 
     } 
     return canHandle; 
    } 

    private WinButton GetWantedButton(Window window) 
    { 
     WinButton button = null; 
     try 
     { 
      if (window.Title.Contains("Windows Internet Explorer") || window.Title.Contains("Message from webpage")) 
      { 
       var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && (new WinButton(w.Hwnd).Title.Contains("Leave") || new WinButton(w.Hwnd).Title.Contains("OK")).FirstOrDefault(); 
       if (windowButton != null) 
       { 
        string s = windowButton.Title; 
        button = new WinButton(windowButton.Hwnd); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      _logger.ErrorException("GetWantedButton", ex); 
     } 
     return button; 
    } 
} 
+0

Tôi không biết watin có thể can thiệp vào các hộp thoại ngoài 'WebBrowser''s !! – Odys

Trả lời

0

Hãy thử điều này:

WatiN.Core.Settings.AutoCloseDialogs = false; 

Theo mặc định được thiết lập để true.

Các vấn đề liên quan