みなさん、こんにちは。
同じナビゲート イベント ハンドラーを使用する多数の webView があります。ナビゲート先の URI をインターセプトし、httpwebrequest (Cookie を追加するため) を実行してから、webview にコンテンツを設定し、javascript で css を追加します。
しかし、ある時点で送信者への参照が失われ、それを解決する方法がよくわかりません。4つのナビゲートイベントハンドラーを実行したくありません..
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
wb1.Navigate(new Uri(site + notif, UriKind.Absolute));
wb2.Navigate(new Uri(site + comptes, UriKind.Absolute));
wb3.Navigate(new Uri(site + virements, UriKind.Absolute));
wb4.Navigate(new Uri(site + autres, UriKind.Absolute));
wb1.Navigating += new EventHandler<NavigatingEventArgs>(wb_Navigating);
wb2.Navigating += new EventHandler<NavigatingEventArgs>(wb_Navigating);
wb3.Navigating += new EventHandler<NavigatingEventArgs>(wb_Navigating);
wb4.Navigating += new EventHandler<NavigatingEventArgs>(wb_Navigating);
}
void wb_Navigating(object sender, NavigatingEventArgs e)
{
Uri uri = e.Uri;
e.Cancel = true;
cookieContainer = new CookieContainer();
//Add cookies here
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.CookieContainer = cookieContainer;
req.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), req);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
Deployment.Current.Dispatcher.BeginInvoke(() => OnUiThreadResponse(results));
}
myResponse.Close();
}
private void OnUiThreadResponse(string results)
{
//HERE i need to know which WebBrowser fired the navigating event
wbi.NavigateToString(results);
//Add css with JS
wbi.InvokeScript("");
}