我试图了解.NET 4.5中的变化,主要是异步功能.为了理解它,我想我会创建一个小应用程序来存档我的大量照片集.我通过这样做来学习最好的应用程序有双重目的.
我已经阅读了很多关于使用异步的MSDN文章,但我认为我对它没有足够的了解(因为它不起作用).我的目的是将源文件夹中的每张照片根据其拍摄日期复制到目标文件夹(或者如果缺少所拍摄的元数据则创建).同时将其重命名为标准命名约定,并在图像框中存档时显示图像.我希望应用程序在工作期间保持响应,这是异步进入的地方.现在应用程序的目的并不重要,整个过程让我的头脑异步.
实际发生的是应用程序没有响应,按预期存档所有图像,但图像框仅显示最终图片.异步开始文件传输,然后继续下一个图像,开始传输然后继续等等,所以我最终得到数百个打开的文件流,而不是等待每个关闭.
任何我错误的地方的指针将不胜感激.我对使用任务的理解很简单,返回任务服务的目的是什么?
imgMain是XAML文件中的图像框.async/await在归档方法中,但显示所有可能相关的代码.
using System;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
namespace PhotoArchive
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string Source
{
get { return txtSource.Text; }
set { txtSource.Text = value; }
}
private string Destination
{
get { return txtDestination.Text; }
set { txtDestination.Text = value; }
}
public MainWindow()
{
InitializeComponent();
} …Run Code Online (Sandbox Code Playgroud) 我需要做什么? 我需要针对XSD文件验证XML文件(传递文件路径/位置)(传递文件路径/位置).我需要检查它是否良好没有非法字符,并且它具有在XSD中定义的所有标签,即没有标签丢失.它匹配xsd中定义的数据类型.完成之后,我需要解析xml文件以获取数据并将其存储在数据库中.
有问题吗?1)使用带有XmlDocument的XmlReaderSetttings和带有Validate方法的XmlReader将帮助我实现我需要的东西吗?有没有人用sampel代码帮我?
2)解析xml文件以获取特定标记的最佳方法是什么?
我是VB.net的新手,所以任何示例代码帮助将不胜感激.谢谢!
我在异步方法中抛出异常时遇到了一些我无法理解的行为.
以下代码将在调用ThrowNow方法时立即抛出异常.如果我将该行注释掉并直接抛出异常,那么吞没异常并且不会在Unobserved事件处理程序中引发异常.
public static async void ThrowNow(Exception ex){
throw ex;
}
public static async Task TestExAsync()
{
ThrowNow(new System.Exception("Testing")); // Throws exception immediately
//throw new System.Exception("Testing"); // Exception is swallowed, not raised in unobserved event
await Task.Delay(1000);
}
void Main()
{
var task = TestExAsync();
}
Run Code Online (Sandbox Code Playgroud)
更令人困惑的是,如果我async从ThrowNow方法中删除关键字,则会再次吞下异常.
我认为异步方法同步运行,直到达到阻塞方法.在这种情况下,似乎删除async关键字使其行为异步.
我在我的Word插件中使用Microsoft.Bcl.Async,我的插件编译为exe(test_addin.exe)文件,从Microsoft Word作为程序集加载,当我直接启动可执行文件时,一切正常,但是当我从Word运行它时,我收到一个错误,说它无法加载Systems.Threading.Tasks程序集.
Could not load file or assembly System.Threading.Tasks...
Run Code Online (Sandbox Code Playgroud)
看起来它与绑定重定向有关,当我尝试从Word运行应用程序时,它期望配置文件位于'C:\Program Files (x86)\Microsoft Office\Office15'文件夹中并被命名WINWORD.exe.config,遗憾的是这是不可能的,因为我可能无法访问该文件夹.
我的test_addin.exe.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我试过设置AppDomain.CurrentDomain.SetupInformation.ConfigurationFile指向正确的路径,但它似乎没有帮助,是否有其他方法使其适用于Office加载项?
我有一个 ListBox 并且想将它的 ItemsSource 设置为我从云中获取的 ObservableCollection。我必须等待这个传入的集合,它导致我的 itemssource 没有更新。
这是我的方法。这是我的 xaml.cs 构造函数:
{
InitializeComponent();
GetEmployeeList();
}
Run Code Online (Sandbox Code Playgroud)
以及它调用的方法:
private async void GetEmployeeList()
{
await EmployeeController.GetAllEmployees().ContinueWith(r =>
{
_employees = (r.Result);
EmployeeListBox.ItemsSource = _employees;
});
}
Run Code Online (Sandbox Code Playgroud)
我的 EmployeeController.GetAllEmployees() 返回一个 ObservableCollection。并且 _employees 得到更新,但是我的 EmployeeListBox 没有显示这些对象。我曾尝试使用静态硬编码集合,它运行良好 - 是因为我的异步吗?有人有建议吗?
- 谢谢。
我有以下类定义:
public class MyData
{
public DateTime Expiration { get; private set; }
public string Name { get; private set; }
public double Price { get; private set; }
public double Quantity { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要加入以下列表:
IEnumerable<int> years= Enumerable.Range(1, 20);
Run Code Online (Sandbox Code Playgroud)
结果将最终显示在网格中,y轴表示Name字段,x轴表示年份.每个单元将汇总Quantity*Price为Name和一年.
我目前正在努力学习语法.我开始加入这些年的实例MyData并分组如下:
var myData = GetData();
var query = from data in myData
join year in years on (data.Expiration.Year - DateTime.Now.Year) + 1 equals year
group data …Run Code Online (Sandbox Code Playgroud) 我有以下几个依赖的基类:
public abstract class ViewModel
{
private readonly ILoggingService loggingService;
public ViewModel(
ILoggingService loggingService,
...)
{
this.loggingService = loggingService;
...
}
}
Run Code Online (Sandbox Code Playgroud)
在我的派生类中,我不想重复此基类构造函数中的所有参数,所以我这样做:
public abstract class ViewModel
{
private readonly IUnityContainer container;
private ILoggingService loggingService;
...
public ViewModel(IUnityContainer container)
{
this.container = container;
}
public ILoggingService LoggingService
{
get
{
if (this.loggingService == null)
{
this.loggingService = this.container.Resolve<IUnityContainer>();
}
return this.loggingService;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在我的派生类只需要将一个东西传递给我的基类构造函数.我也有很好的效果,只有在需要时才能解析我的依赖项.
但是,我已经知道传递一个IOC容器是一个坏主意.什么是最好的替代设计模式,记住传入的许多服务已经作为单身人士在我的IOC容器中注册?
Framework 4.0 Asp.net应用程序
当我运行代码时,我收到一个错误"'RangeValidator'的MaximumValue属性的值'999.9999'无法转换为'Currency'类型.
以下是我的代码:
<asp:RangeValidator Runat='server' ControlToValidate='textEdit'
MinimumValue='0.0001'
MaximumValue='999.9999' Type='Currency'
ErrorMessage='Should be between 0.0001 and 999.9999' id="idValidtor"
display='None' />
Run Code Online (Sandbox Code Playgroud)
请解释一下我的货币值是否小数后不能超过2位数?除非我如何解决这个问题?
给定一个类项目:
public class Item {
private String field1;
private String field2;
private String field3;
private Integer field4;
// getters, constructor...
}
Run Code Online (Sandbox Code Playgroud)
另一个类Group(field1和field2存储Item中的等效字段):
public class Group {
private String field1;
private String field2;
}
Run Code Online (Sandbox Code Playgroud)
我有一个List<Item>我需要聚合到以下结构的地图:
Map<Group, Map<Field3, List<Field4>>>
Run Code Online (Sandbox Code Playgroud)
示例数据:
Field1 | Field2 | Field3 | Field4
------ | ------ | ------ | ------
"f1" | "f2" | "a" | 1
"f1" | "f2" | "a" | 2
"f1" | "f2" | "a" | 3
"f1" | "f2" | "b" …Run Code Online (Sandbox Code Playgroud) 我在资源文件夹(Properties.Resources)中创建了一个文件夹.我怎样才能使用那里的图像?