我需要从我的虚拟 com 端口读取数据并检测消息“Dreq”。一旦我按下连接按钮,它就会连接到我的 COM8 端口并开始读取新线程。我还有一个断开连接按钮,我希望在其中关闭读数并断开与 COM8 端口的连接。但是,我在关闭 BeginRead 时遇到问题。
public partial class Form1 : Form
{
SerialPort sp;
Stream stream;
IAsyncResult recv_result;
private void button1_Click(object sender, EventArgs e)
{
sp = new SerialPort("COM8", 9600);
sp.Open();
sp.ReadTimeout = 50000;
sp.NewLine = "\n\r\0";
stream = sp.BaseStream;
recv_result = stream.BeginRead(new byte[1], 0, 0, new
AsyncCallback(ReadCallBack), stream);
}
private void ReadCallBack(IAsyncResult ar)
{
Stream stream = (Stream)ar.AsyncState;
string temp;
while (stream.CanRead)
{
temp = sp.ReadLine();
// ... do something with temp
}
}
private …Run Code Online (Sandbox Code Playgroud) 下面的代码编译,运行,完全符合我的预期 - 当事件被引发时,GreetingPublisher调用bus.Publish() - 但是Moq设置没有匹配:
using Moq;
using NServiceBus;
using NUnit.Framework;
namespace MyProject.Greetifier.Tests {
[TestFixture]
public class GreetingPublisher_Bus_Integration_Tests {
[Test]
public void Greeting_Is_Published_To_Bus() {
var mockGreeter = new Mock<IGreeter>();
var mockBus = new Mock<IBus>();
mockBus.Setup(bus => bus.Publish<IMessage>(It.IsAny<IMessage>()))
.Verifiable();
var Greetifier = new GreetingPublisher(mockGreeter.Object,
mockBus.Object);
mockGreeter.Raise(m => m.Greet += null, "world");
mockBus.Verify();
}
}
public class HelloMessage : IMessage {
public string Name { get; set; }
public HelloMessage(string name) { this.Name = name; }
}
public class GreetingPublisher {
private …Run Code Online (Sandbox Code Playgroud) 在查询XmlDocument时,我需要在每次调用时传递命名空间管理器.真烦人,但这只是我们生活的东西.真正讨厌的一点是首先创建命名空间管理器.
XmlNamespaceManager nsMan = new XmlNamespaceManager(invoiceTextReader.NameTable);
nsMan.AddNamespace("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
nsMan.AddNamespace("pb", "urn:pierbridge:names:specification:pbl:schema:xsd:tpn-1");
...
Run Code Online (Sandbox Code Playgroud)
要创建它,我不仅要使用nametable为实例设定种子,还要手动指定每个命名空间.这对我来说似乎很愚蠢.如果我必须手动添加它们,那么传递名称表的重点是什么.如果我需要为每个查询传递命名空间管理器,那么传递名称表的重点是什么.为什么它不能直接从文档中包含的内容构建命名空间管理器.看起来只是为了运行一个查询而烦恼不已.
我在http://www.hmailserver.com/documentation/latest/?page=com_example_account_create下面提供了以下代码作为hMailServer的DCOM API提供的代码以下脚本正常工作.它没有任何参考.在安装hMailServer之后,运行以下代码可以创建一个帐户.现在,我在C#中需要相同的东西.他们没有为我提供任何C#我用Google搜索的库,但我没有相关的结果是下面的代码,但根据hMailServer API,他们说你可以将下面的脚本转换成你想要的任何语言.但是怎么样?我甚至不知道如何开始写第一行.有人请帮帮我.
Dim obApp
Set obApp = CreateObject("hMailServer.Application")
' Authenticate. Without doing this, we won't have permission
' to change any server settings or add any objects to the
' installation.
Call obApp.Authenticate("Administrator", "your-main-hmailserver-password")
' Locate the domain we want to add the account to
Dim obDomain
Set obDomain = obApp.Domains.ItemByName("example.com")
Dim obAccount
Set obAccount = obDomain.Accounts.Add
' Set the account properties
obAccount.Address = "account@example.com"
obAccount.Password = "secret"
obAccount.Active = True
obAccount.MaxSize = 100 ' Allow max …Run Code Online (Sandbox Code Playgroud) 让我详细说明一下."items"我指的是你在桌面上看到的所有项目(Windows),其中包括"我的电脑","回收站",所有快捷方式等.如果我选择桌面上的所有项目,我会在属性中获得计数显示.这是我想要的,以编程方式.
我面临的问题:
我们看到的桌面包含我帐户中All Users的项目,还有桌面项目以及"我的电脑","回收站"等其他快捷方式.总共有3件事.所以我不能只从物理路径到Desktop目录获取项目数.所以这失败了:
int count =
Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
.DesktopDirectory)
).Length;
Run Code Online (Sandbox Code Playgroud)
据我所知SpecialFolder.Desktop,我知道代表逻辑桌面.但是再次失败,因为GetFolderPath()再次获得用户桌面的物理路径:
int count =
Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
.Desktop)
).Length;
Run Code Online (Sandbox Code Playgroud)
在用户的桌面上获得总计数的正确方法是什么?
假设我有以下两个类:
public class User : Entity
{
public virtual IList<Item> Items { get; set; }
}
public class Item : Entity
{
public virtual User Owner { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我创建了两个映射类:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
HasMany(x => x.Items);
}
}
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
Id(x => x.Id);
References(x => x.Owner);
}
}
Run Code Online (Sandbox Code Playgroud)
这将导致Item具有列UserId和列的表OwnerId.当我KeyColumn("OwnerId")在HasMany映射上使用时,它只适用于OwnerId列,但我想避免这种情况.有没有办法告诉NHibernate,使用映射创建的列ItemMap …
我在我的项目中使用Fluent验证.
在我的ViewModel中,我有一个string类型的属性,有效值只是表示正整数的字符串.
因此,我创建了一个简单IntegerValidator的函数来检查字符串是否可以解析为整数.这有效.
问题是,如何添加规则,它必须是一个正整数?我想使用现有的Greater Than Validator,但将其链接到我的字符串属性的规则会将其比较为a string,而不是解析int.怎么做到这一点?
我想做的样本(注意ToInt()):
RuleFor(x => x.BatchNumber).SetValidator(new IntegerValidator())
.ToInt().GreaterThan(0);
Run Code Online (Sandbox Code Playgroud) 在我的WPF应用程序中,我使用MVVM模式和依赖注入.
从数据库准备数据的ViewModel获取注入构造函数的存储库.它们还使用构造函数中的存储库中的数据填充属性.
ViewModel都是在ViewModelLocator类的静态构造函数中创建的,所有View都使用它来绑定到它们的ViewModel.
这有以下缺点:
我可以想出两种解决这些问题的方法:
有没有其他方法可以解决这个问题?别人怎么解决这个问题?
我正在尝试在Monodroid项目中使用ISpannable但是我遇到了GetSpans的问题.我最终追求的是富文本编辑器,例如:
https://code.google.com/p/android-richtexteditor/source/browse/?r=4#svn/trunk/src/net/sgoliver
但是,GetSpans的Xamarin文档并不是特别有用.我试图从Java转换为C#的行是:
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
Run Code Online (Sandbox Code Playgroud)
但是,我不知道为最后一个参数传递什么,因为在C#中编写StyleSpan.class会产生"} expected"的编译错误.我可以传递给最后一个参数以获取所有跨度或特定类型的所有跨度?
我是C#活动的初学者和代表.如果你看一下我用来了解事件的URL,那么很少有问题会有意义.
我可以在没有与之关联的代表的情况下举办活动吗?我发现的exmaples总是把它们放在一起.
例如,我不明白如何生成事件.我完全迷失了,我知道Tick被定义为一个事件,但Tick的事件描述是什么?我在哪里定义什么构成Tick?
此外,Tick值在哪里被启动.似乎我所看到的所有示例都没有初始化事件,并且对语句有一个类似的声明:If(Tick!= null)在附加的示例中,但它不是obviuos,其中Tick是初始化的.我环顾四周,我找不到任何答案.
在此先感谢您的帮助