我有以下代码将窗口显示为对话框
BrowserPopupWindow.Navigate(loginType, uri);
BrowserPopupWindow.ShowDialog();
在导航方法上我正在做一些异步webrequest,因为返回执行路径并且Dialog关闭,如何保持Dialog打开直到我得到webrequest的响应。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using Intel_ChromeBook_Grive.Utility;
using Intel_ChromeBook_Grive.Helpers;
using System.Windows.Threading;
namespace Intel_ChromeBook_Grive.Views
{
/// <summary>
/// Interaction logic for BrowserPopup.xaml
/// </summary>
public partial class BrowserPopup : Window
{
internal delegate void OutlookOAuthFecthed(string oauthURI);
internal event OutlookOAuthFecthed OutlookOAuthFecthedEvent;
DispatcherTimer TimeoutBrowser = new DispatcherTimer();
bool iSAccessTokenFecthed = false;
internal delegate void YahooGuidFetched(string guid);
internal event YahooGuidFetched YahooGuidFetchedEvent;
internal delegate void UserClosedBrowser(Constants.CurrentLoginType logintype);
internal event UserClosedBrowser UserClosedBrowserEvent;
private Constants.CurrentLoginType Logintype;
Uri YahooUri;
public BrowserPopup()
{
InitializeComponent();
TimeoutBrowser.Tick += TimeoutBrowser_Tick;
TimeoutBrowser.Interval = TimeSpan.FromSeconds(10);
}
void TimeoutBrowser_Tick(object sender, EventArgs e)
{
PopupMessage.Messagepop(Strings.NetwokrErrorText);
ClosePopUp();
}
internal void Navigate(Constants.CurrentLoginType logintype,Uri uri = null)
{
webbrowser.LoadCompleted +=webbrowser_LoadCompleted;
webbrowser.Navigating += webbrowser_Navigating;
webbrowser.Navigated += webbrowser_Navigated;
if (logintype == Constants.CurrentLoginType.Google)
{
WizardLoader.isUserLoggedin = false;
Logintype = logintype;
webbrowser.Navigate("https://www.google.com/accounts/Logout");
}
else if (logintype == Constants.CurrentLoginType.Outlook)
{
Logintype = logintype;
webbrowser.Navigate(Constants.OutLook_signInUrl);
}
else if (logintype == Constants.CurrentLoginType.Yahoo)
{
Logintype = logintype;
YahooUri = uri;
webbrowser.Navigate("https://login.yahoo.com/config/login?logout=1");
}
}
void webbrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
//throw new NotImplementedException();
}
void webbrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
TimeoutBrowser.Start();
//throw new NotImplementedException();
}
void webbrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
TimeoutBrowser.Stop();
#region Google OAuth handle
//if login is for google
if (Logintype == Constants.CurrentLoginType.Google)
{
//** Show the loading indicator till the actaul page is loaded
if (e.Uri.ToString().Contains(Constants.AUTH_URI))
{
webbrowser.Visibility = System.Windows.Visibility.Visible;
}
else if (e.Uri.ToString().Contains(@"https://accounts.google.com/ServiceLogin?") && !e.Uri.ToString().Contains(Constants.AUTH_URI))
{
webbrowser.Navigate(OAouthManager.BuildAuthenticationUri());
}
else
{
mshtml.HTMLDocument theDoc = (sender as WebBrowser).Document as mshtml.HTMLDocument;
if (theDoc.title.ToLower().StartsWith("navigation cancelled"))
{
PopupMessage.Messagepop(Strings.NetwokrErrorText);
ClosePopUp();
}
else if (theDoc.title.ToLower().StartsWith("denied error"))
{
PopupMessage.Messagepop(Strings.UserDeniedText);
// MessageBox.Show("User did not allow access");
ClosePopUp();
}
else if (theDoc.title.ToLower().StartsWith("success"))
{
// TheBrowserPop.IsOpen = false;
this.Visibility = System.Windows.Visibility.Hidden;
// TheLogin_Browser.Visibility = System.Windows.Visibility.Hidden;
string OAouth = theDoc.getElementById("code").getAttribute("value");
OAouthManager m = new OAouthManager();
m.AccessTokenFetchedEvent += m_AccessTokenFetchedEvent;
m.GetAccessToken(OAouth);
while (Logintype == Constants.CurrentLoginType.Google && iSAccessTokenFecthed == false)
{
}
}
}
}
#endregion
#region OutLook OAuth handle
else if (Logintype == Constants.CurrentLoginType.Outlook)
{
webbrowser.Visibility = System.Windows.Visibility.Visible;
if (e.Uri.AbsoluteUri.Contains("code="))
{
if (OutlookOAuthFecthedEvent != null)
{
OutlookOAuthFecthedEvent(e.Uri.AbsoluteUri);
}
ClosePopUp();
}
else if (e.Uri.AbsoluteUri.Contains("error=access_denied"))
{
MessageBox.Show(Strings.UserDeniedText);
ClosePopUp();
}
mshtml.HTMLDocument theDoc = (sender as WebBrowser).Document as mshtml.HTMLDocument;
if (theDoc.title.ToLower().StartsWith("navigation cancelled"))
{
MessageBox.Show(Strings.NetwokrErrorText);
ClosePopUp();
}
}
#endregion
#region Yahoo OAuth handle
if (Logintype == Constants.CurrentLoginType.Yahoo)
{
if (e.Uri.AbsoluteUri.Contains("http://www.test.com/?oauth_token"))
{
webbrowser.Visibility = System.Windows.Visibility.Hidden;
if (YahooGuidFetchedEvent != null)
{
YahooGuidFetchedEvent(e.Uri.ToString());
}
webbrowser.LoadCompleted -= webbrowser_LoadCompleted;
ClosePopUp();
} else if (e.Uri.AbsoluteUri.Contains("logout"))
{
webbrowser.Navigate(YahooUri);
}
else
{
webbrowser.Visibility = System.Windows.Visibility.Visible;
}
}
#endregion
}
void m_AccessTokenFetchedEvent(GoogleAccessToken googleAccessToken)
{
// GoogleAccessToken TheFirstToken = OAouthManager.GetAccessToken(OAouth);
if (googleAccessToken == null)
{
PopupMessage.Messagepop(Strings.NetwokrErrorText);
ClosePopUp();
return;
}
if (App.Current.Properties.Contains(Constants.G_AccessTokenKey))
{
App.Current.Properties[Constants.G_AccessTokenKey] = googleAccessToken.access_token;
}
else
{
App.Current.Properties.Add(Constants.G_AccessTokenKey, googleAccessToken.access_token);
}
if (App.Current.Properties.Contains(Constants.G_RefreshTokenKey))
{
App.Current.Properties[Constants.G_RefreshTokenKey] = googleAccessToken.refresh_token;
}
else
{
App.Current.Properties.Add(Constants.G_RefreshTokenKey, googleAccessToken.refresh_token);
}
WizardLoader.isUserLoggedin = true;
ClosePopUp();
}
void ClosePopUp()
{
TimeoutBrowser.Tick -= TimeoutBrowser_Tick;
TimeoutBrowser = null;
webbrowser.LoadCompleted -= webbrowser_LoadCompleted;
this.Holderpanel.Children.Remove(webbrowser);
webbrowser = null;
// this.Height = 0;
// this.Width = 0;
// this.Visibility = System.Windows.Visibility.Hidden;
Keyboard.ClearFocus();
this.Close();
}
~BrowserPopup()
{
//webbrowser.Navigating -=
}
private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (UserClosedBrowserEvent != null)
{
UserClosedBrowserEvent(Logintype);
}
ClosePopUp();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Closing My window");
}
}
}
答案 0 :(得分:0)
猜测方法在某个意外的地方调用ClosePopUp。你应该找出你的代码在LoadCompleted Event Handler中的哪个条件分支。
答案 1 :(得分:0)
如果您在 webbrowser_LoadCompleted 事件中关闭窗口,很明显您的实例将会关闭。
在条件上,您的webbrowser_LoadCompleted事件中有几个 ClosePopup()调用。如果接受任何条件,将调用ClosePopup()(其中您编写了 this.close )方法,这将导致关闭表单。
简而言之,在加载事件时关闭表单并不是一个好主意。您需要改进相应的应用逻辑。
正如您所说,您有一些异步调用,详细的执行记录将帮助您详细说明。我宁愿记录事件而不是调试它。 : - )
您使用了 TextBlock_PreviewMouseDown 事件,该事件也会关闭事件,如果您可以禁用它并尝试,我认为它将解决您的问题。