/// <summary> /// 初始化单一实例应用程序对象。这是执行的创作代码的第一行, /// 已执行,逻辑上等同于 main() 或 WinMain()。 /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; using (var db = new Data.PasswordContext()) { db.Database.Migrate(); } using (var db = new Data.HistoryContext()) { db.Database.Migrate(); } }
private async void AddressBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { if (args.Reason == AutoSuggestionBoxTextChangeReason.SuggestionChosen) { return; } string text = sender.Text; string[] result; using (var db = new Data.HistoryContext()) { var query = from Data.HistoryEntry item in db.Histories where item.Url.Contains(text) orderby item.Time descending select item.Url; result = await query.Take(10).Distinct().ToArrayAsync(); } sender.ItemsSource = result; }
/// <summary> /// 导航到指定的地址。需要登录时自动弹出登录界面。导航失败时,自动导航到失败页面。 /// 不抛出异常。 /// </summary> /// <param name="address">要导航到的地址</param> /// <returns>导航是否成功</returns> private async Task <bool> NavigateAsync(Uri address) { await ftpSemaphore.WaitAsync(); try { errorMessage.Visibility = Visibility.Collapsed; progressBar.Visibility = Visibility.Visible; progressBar.IsIndeterminate = true; currentAddress = address; // 显示新的地址 UriBuilder uriBuilder = new UriBuilder(currentAddress); uriBuilder.UserName = string.Empty; uriBuilder.Password = string.Empty; addressBox.Text = uriBuilder.Uri.ToString(); // 不是ftp协议则抛出异常 if (address.Scheme != "ftp" && address.Scheme != "ftps") { throw new InvalidOperationException("只支持ftp协议"); } // Host改变时重新连接 if (client == null) { client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps"); await FtpConnectAsync(client); } else if (client.Host != address.Host || (client.EncryptionMode != FluentFTP.FtpEncryptionMode.None && address.Scheme == "ftp") || (client.EncryptionMode != FluentFTP.FtpEncryptionMode.Explicit && address.Scheme == "ftps") || (client.Port != 21 && address.Port < 0) || (client.Port != address.Port && address.Port >= 0) || !string.IsNullOrEmpty(address.UserInfo)) { client.Disconnect(); client.Dispose(); client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps"); await FtpConnectAsync(client); } // FTP路径可能包含#号,#号后面的内容会被归入Fragment。 string remotePath = address.LocalPath + address.Fragment; listItemsVM.Clear(); foreach (var item in (await client.GetListingAsync(remotePath)).OrderBy(x => x.Name)) { listItemsVM.Add(await FtpListItemViewModel.FromFtpListItemAsync(item)); } using (Data.HistoryContext db = new Data.HistoryContext()) { Data.HistoryEntry h = new Data.HistoryEntry { Time = DateTimeOffset.Now, Url = uriBuilder.Uri.ToString() }; await db.AddAsync(h); await db.SaveChangesAsync(); } return(true); } catch (FluentFTP.FtpCommandException exception) { if (exception.CompletionCode == "530") { errorMessage.Text = "认证失败,请尝试登录。"; loginButton.Flyout.ShowAt(loginButton); loginErrorMessage.Visibility = Visibility.Visible; } else { errorMessage.Text = string.Format( "发生错误,FTP返回代码:{0}。详细信息:\n{1}", exception.CompletionCode, exception.Message); } errorMessage.Visibility = Visibility.Visible; listItemsVM.Clear(); return(false); } catch (Exception exception) { errorMessage.Text = string.Format("发生错误。详细信息:\n{0}", exception.Message); errorMessage.Visibility = Visibility.Visible; listItemsVM.Clear(); return(false); } finally { progressBar.Visibility = Visibility.Collapsed; ftpSemaphore.Release(); } }