我有一个旨在从 OleDB 填充 PersonModel 的方法;
public IEnumerable<PeopleModel> GetPeopleDetails()
{
var constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
using (var dbfCon = new OleDbConnection(constr))
{
dbfCon.Open();
using (var dbfCmd = dbfCon.CreateCommand())
{
dbfCmd.CommandText = "SELECT pp_firstname, pp_surname, pp_title, pp_compnm, pp_hmaddr1, pp_hmaddr2, pp_hmtown, pp_hmcounty, pp_hmpcode, pp_spouse, pp_children FROM people ORDERBY pp_surname";
using (var myReader = dbfCmd.ExecuteReader())
{
var peopleList = new List<PeopleModel>();
while (myReader.Read())
{
var details = new PeopleModel
{
details.Firstname = myReader[0].ToString(),
details.Lastname = myReader[1].ToString(),
details.Title = myReader[2].ToString(),
details.Company = myReader[3].ToString(),
details.Addr1 = myReader[4].ToString(), …Run Code Online (Sandbox Code Playgroud) 我正在 C# 中创建一个文件夹,我希望在创建后立即将其压缩。我环顾四周(如何压缩文件夹),(http://dotnetzip.codeplex.com/)但到目前为止还没有运气。我有点担心使用 dotnetzip,因为它的最后一个版本是 5 年前。
dotnetzip 在 Visual Studio 2015 中是否仍然相关,或者是否有更现代的方式在 C# 中压缩文件夹而不使用包?
这就是我复制文件夹的方式;
private static void CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = …Run Code Online (Sandbox Code Playgroud) 我ObservableCollection在foreach循环期间更新项目时遇到问题.基本上我有一个ObservableCollection员工,他们的模型中有一个领域决定他们是否在建筑物中.
我一直在查看数据库表来检查每个员工,看看这个状态是否有任何变化.这就是我这样做的方式C#;
public ObservableCollection<EmployeeModel> EmployeesInBuilding {get; set; }
public ObservableCollection<EmployeeModel> Employees {get; set; }
var _employeeDataService = new EmployeeDataService();
EmployeesInBuilding = _employeeDataService.GetEmployeesInBuilding();
foreach (EmployeeModel empBuild in EmployeesInBuilding)
{
foreach (EmployeeModel emp in Employees)
{
if (empBuild.ID == emp.ID)
{
if (empBuild.InBuilding != emp.InBuilding)
{
emp.InBuilding = empBuild.InBuilding;
int j = Employees.IndexOf(emp);
Employees[j] = emp;
employeesDataGrid.Items.Refresh();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这正确地选择了两者之间的变化ObseravbleCollections,但是当我去更新现有的时,ObservableCollection我得到一个异常:Collection was modified; enumeration operation may not execute. …
我reallt试图围绕让我的头async/await和Tasks.我试图让一个方法失效,以便我可以在整个程序中使用它.基本上我有自己的版本BusyIndicator,我希望在工作完成时显示.这是一个基本的例子;
private async void OnPageLoad(object sender, RoutedEventArgs e)
{
var waitWindow = new PleaseWaitWindow();
waitWindow.Show();
await LoadCompanyContracts();
waitWindow.Close();
}
private async Task LoadCompanyContracts()
{
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
//Work done here
});
});
}
Run Code Online (Sandbox Code Playgroud)
即使通过这两种方法并尝试实现Tasks我BusyIndicator仍然不旋转,它显示但是被冻结,所以实际上我相信UI线程仍然被阻止.如何修改这段代码以确保所有CPU绑定的工作都没有阻止UI线程?
我已经看到很多关于类似问题的问题,但我还没有找到一个非常具体到我的问题来找到答案.
我允许用户将我的程序的MainWindow移动到另一个监视器上(如果有的话).当他们单击以在MainWindow上添加内容时,会创建一个新窗口,以便他们可以填写表单.
问题是这个新窗口将在操作系统的主屏幕上启动,而不是在MainWindow当前所在的屏幕上启动.
我看了一下这个问题; 如何在与主窗口相同的监视器上打开一个对话框(视图),并看到在这种情况下将所有者设置为MainWindow.所以我补充道
Owner = Application.Current.MainWindow;
进入Window_Loaded方法的新窗口.这确实将Window加载到与MainWindow相同的屏幕上,但随后崩溃,说明Cannot set Owner property after Dialog is shown.
当然,我将所有者属性的设置移动到显示新窗口之前,所以它看起来像这样;
contractAddwindow.Owner = Application.Current.MainWindow;
contractAddwindow.ShowDialog();
然而,这与以前有相同的问题,在contractAddWindow主屏幕上开始.
所以我的问题是,如何强制新窗口加载到与MainWindow相同的监视器而不是主屏幕?
我正在寻找添加参考;
Microsoft.Reporting.WinForms
到我的VisualStudio(2015)项目,但当我去解决方案资源管理器中添加参考时,我找不到它.
最后我添加了它,NuGet但现在我收到了错误消息;
Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)
这让我相信我没有正确添加包.我通过程序功能安装了Microsoft SQL Server数据工具,但仍然没有运气.
我错过了什么?
如果未选中StackPanel中的所有CheckBox,我想执行任务.目前我正在做这样的事情;
foreach (CheckBox c in _employees.Children)
{
if (c.IsChecked == false)
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当然这表示每次遇到CheckBox错误时都会做一些事情.有没有办法我可以说只有在CheckBoxes没有选中所有的东西时才做某事,而不是单独评估它们?
我是这样添加的ComboBoxItems;
foreach (var cntRef in presetList.Where(filteredPreset => filteredPreset.PresetReferenceFoxPro == 1).ToList())
{
var newItem = new ComboBoxItem();
newItem.Content = cntRef.PresetText;
newItem.Tag = cntRef.PresetIDFoxPro;
addCntRef1ComboBox.Items.Add(newItem);
}
Run Code Online (Sandbox Code Playgroud)
这样显示文字没问题。但是我在显示Tag. 当我尝试访问时Tag;
if (addCntRef1ComboBox.Tag.ToString() != null)
{
MessageBox.Show(addCntRef1ComboBox.Tag.ToString());
}
Run Code Online (Sandbox Code Playgroud)
什么都不显示。当我删除null检查程序崩溃时,显然Tag是null. 如何添加ComboBoxItem一个tag我可以访问?
我目前Rectangles在我的应用程序中有一套我这样定义的内容XAML;
<Rectangle Fill="LightBlue"/>
Run Code Online (Sandbox Code Playgroud)
目前我可能<10,Rectangles因此改变Coloura Rectangle不是太大的问题.
然而,随着我的应用程序的增长,我无疑会有更多,更多Rectangles,如果确定他们的颜色需要改变,我可以看到可扩展性将是一个大问题.
什么是存储的最有效的方法Background的Rectangle,这样我可以在一个地方改变所有的改变它Rectangles在我的计划?
这也可以延伸到a的风格TextBox.如果我想为TextBox整个应用程序中的每一个设置一个自定义样式,那么可扩展的方法是什么?
我正在尝试在用户选择网络名时显示用户图像,但是当我尝试查找他们的图像时,我会收到一条错误消息,例如;
Could not find file 'S:\Picture\Chris .jpg'.
Run Code Online (Sandbox Code Playgroud)
你可以告诉你有很多空格,所以我尝试了这个,其中每个空格都将在netname中用"a"替换;
string dbfQuery = "SELECT em_pplid, em_name, STRTRAN(em_netname, ' ', 'a'), em_surname FROM employs WHERE em_netname NOT LIKE ''";
Run Code Online (Sandbox Code Playgroud)
当然,我开始收到类似的新错误消息;
Could not find file 'S:\Picture\Chrisaaaaaaaaaaaaaaa.jpg'.
Run Code Online (Sandbox Code Playgroud)
您会立即认为更改STRTRAN(em_netname, ' ', 'a')为STRTRAN(em_netname, ' ', '')将删除所有空格.虽然这在一定程度上起作用,但首先如果有一个以上的克里斯,例如"ChrisB"和"ChrisC",他们现在都变成了"克里斯".
为了增加这一点,一些空格不会被删除,例如我仍然会收到错误说;
Could not find file 'S:\Picture\YenT .jpg'.
Run Code Online (Sandbox Code Playgroud)
即在某些情况下仍然留有一个空间.
这有什么理由吗?我是否正确使用STRTRAN?我认为可能的一件事是,除了空格之外还有其他隐藏的,不可打印的字符,但不会用"a"替换它们留下空间?
我有一组字符串,都包含字符组合,特别是*?*!.我想用回车来替换我遇到的每一组.到目前为止,我尝试过几种方法,即;
foreach (ContactsModel c in listOfContacts)
{
c.ContactNotes.Replace("*?*!", Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)
和
foreach (ContactsModel c in listOfContacts)
{
c.ContactNotes.Replace("*?*!", "\r\n");
}
Run Code Online (Sandbox Code Playgroud)
但他们都没有取代组合.我在这里做错了吗?肯定有特定*?*!的人物组合ContactNotes.