Basato su this answer Ho creato un numero personalizzato DialogHandler
per gestire le caselle di avviso Javascript che si aprono da un controllo WebBrowser.Watin DialogHandler chiude SaveFileDialog
Il Handler
è collegato a un browser WatiN IE
ereditato, denominato ExtendedIeBrowser
.
Per un motivo sconosciuto, DialogHandler di Watin interferisce con Winforms SaveFiledialogs. Il SaveFileDialog
viene chiuso automaticamente restituendo DialogResult.Cancel. La cosa strana è che il Handle()
del gestore personalizzato non viene mai chiamato. Solo CanHandle()
viene chiamato (due volte) e restituisce false, quindi la finestra di dialogo non deve essere gestita affatto, pertanto dovrebbe rimanere aperta.
C'è qualcosa che posso fare per cambiare superare questo strano comportamento?
Questa è la fonte 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);
}
}
seguito la fonte 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;
}
}
Non sapevo che Watin possa interferire con i dialoghi diversi da quelli di 'WebBrowser' !! – Odys