using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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.Navigation; using System.Windows.Shapes; using KPlan.Util; using System.Threading; using System.Windows.Threading; namespace KPlan.Forms { /// /// Login.xaml 的交互逻辑 /// public partial class Login : System.Windows.Window { public Login() { InitializeComponent(); KUtil.SetOwnerWindow(this); LoadFromConfig(); } private void Button_Click_1(object sender, RoutedEventArgs e) { Close(); } private void SetLoginVisible(string userName) { Visibility v; if (KUtil.IsEmpty(userName)) { v = Visibility.Hidden; } else { //KUtil.Log("用户已登录:" + userName); v = Visibility.Visible; } l_UserName.Content = userName; rec_Logout.Visibility = v; b_Logout.Visibility = v; l_UserName.Visibility = v; l_UserNameTitle.Visibility = v; b_Login.IsEnabled = KUtil.IsEmpty(userName); } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) this.DragMove(); } private void DoLogin(object sender, RoutedEventArgs e) { string userName = tb_UserName.Text; string password = tb_Password.Password; string character = tb_Character.Text; bool autoLogin = cb_AutoLogin.IsChecked.Value; string host = KUtil.GetConfigValue(KConfigure.LOGIN_SECTION, KConfigure.HOST); try { TCUtil.Login(host, userName, password, character, autoLogin); SetLoginVisible(TCUtil.userName); } catch (Teamcenter.Schemas.Soa._2006_03.Exceptions.InvalidCredentialsException e1) { MessageBox.Show(this, "登录失败,用户名或密码错误", "提示"); KUtil.LogErr(e1); } catch (System.Exception ex) { MessageBox.Show(this, "登录失败:\n" + ex.Message, "提示"); KUtil.LogErr(ex); } //new Thread(() => { // Action loginAction = new Action(LoginInThread); // this.Dispatcher.BeginInvoke(loginAction, host, userName, password, character); //}).Start(); } private void LoadFromConfig() { SetLoginVisible(TCUtil.userName); tb_UserName.Text = KUtil.GetConfigValue(KConfigure.LOGIN_SECTION, KConfigure.LOGIN_USER); tb_Character.Text = KUtil.GetConfigValue(KConfigure.LOGIN_SECTION, KConfigure.LOGIN_ROLE); if (!KUtil.IsEmpty(KUtil.GetConfigValue(KConfigure.LOGIN_SECTION, KConfigure.LOGIN_PWD))) { cb_AutoLogin.IsChecked = true; } //new Thread(() => { // this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() { // SetLoginVisible(TCUtil.userName); // }); //}).Start(); } private void DoLogout(object sender, RoutedEventArgs e) { try { TCUtil.Logout(); SetLoginVisible(TCUtil.userName); //new Thread(() => { // this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { // SetLoginVisible(TCUtil.userName); // }); //}).Start(); } catch (System.Exception ex) { MessageBox.Show(this, "注销出错:\n" + ex.Message, "提示"); KUtil.LogErr(ex); } } //protected override void OnDeactivated(EventArgs e) { // base.OnDeactivated(e); // Close(); //} } }