我正在编写一个将数据传输到USB HID类设备的WinForms应用程序.我的应用程序使用了优秀的Generic HID库v6.0,可以在这里找到.简而言之,当我需要将数据写入设备时,这是被调用的代码:
private async void RequestToSendOutputReport(List<byte[]> byteArrays)
{
foreach (byte[] b in byteArrays)
{
while (condition)
{
// we'll typically execute this code many times until the condition is no longer met
Task t = SendOutputReportViaInterruptTransfer();
await t;
}
// read some data from device; we need to wait for this to return
RequestToGetInputReport();
}
}
Run Code Online (Sandbox Code Playgroud)
当我的代码退出while循环时,我需要从设备中读取一些数据.但是,设备无法立即响应,因此我需要等待此呼叫返回才能继续.因为它目前存在,RequestToGetInputReport()声明如下:
private async void RequestToGetInputReport()
{
// lots of code prior to this
int bytesRead = await GetInputReportViaInterruptTransfer();
}
Run Code Online (Sandbox Code Playgroud)
对于它的价值,GetInputReportViaInterruptTransfer()的声明如下所示:
internal …Run Code Online (Sandbox Code Playgroud) 我有一个WPF 4应用程序,其中包含一个TextBlock,它具有一个单向绑定到一个整数值(在这种情况下,温度为摄氏度).XAML看起来像这样:
<TextBlock x:Name="textBlockTemperature">
<Run Text="{Binding CelsiusTemp, Mode=OneWay}"/></TextBlock>
Run Code Online (Sandbox Code Playgroud)
这适用于显示实际温度值,但我想格式化这个值,因此它包括°C而不是数字(30°C而不是30°).我一直在阅读有关StringFormat的文章,我看过几个这样的通用例子:
// format the bound value as a currency
<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" />
Run Code Online (Sandbox Code Playgroud)
和
// preface the bound value with a string and format it as a currency
<TextBlock Text="{Binding Amount, StringFormat=Amount: {0:C}}"/>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我所看到的所有例子都没有在我试图做的事情中附加一个字符串到绑定值.我敢肯定它必须简单但我找不到任何运气.任何人都可以向我解释如何做到这一点?
我刚刚更新到 Xcode 13.3,并且看到了几个新警告的实例,这是我在以前版本的 Xcode 中未曾见过的。例如,我有一个名为 LabelAndSwitchTableViewCell 的简单表视图单元格,如下所示:
import UIKit
class LabelAndSwitchTableViewCell: UITableViewCell {
private let label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let _switch: UISwitch = {
let _switch = UISwitch()
_switch.translatesAutoresizingMaskIntoConstraints = false
_switch.addTarget(self, action: #selector(didToggleSwitch), for: .valueChanged)
return _switch
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(label)
contentView.addSubview(_switch)
// layout constraints removed for brevity
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc …Run Code Online (Sandbox Code Playgroud) 我被要求重新编写(从头开始)现有的C#winforms应用程序.不幸的是,在过去的三四年里,至少有十几个不同的开发人员修改了这个代码,他们似乎都没有看到任何类型的编码标准.要说这个代码库是乱七八糟的,那就是礼貌地说.考虑到代码的大小(~24k行)以及代码对我来说是全新的事实,我想找到某种实用工具来帮助我更快地理解这个应用程序在高级别上的工作原理.请记住,在这段代码中似乎没有大量优秀的OOP实践,所以我需要比类图更详细的东西.我已经看到了生成序列图的参考,这可能更像我正在寻找但我只有VS2010 Premium,我的印象是这个功能只提供VS Ultimate SKU.我可以访问当前版本的.NET Reflector,我看到有几个人提到有插件可能有用,但我没有任何特定的名称.
我有这样的枚举:
public enum Cities
{
[Description("New York City")]
NewYork,
[Description("Los Angeles")]
LosAngeles,
Washington,
[Description("San Antonio")]
SanAntonio,
Chicago
}
Run Code Online (Sandbox Code Playgroud)
我想将它绑定到一个组合框,我试过这个:
comboBox.DataSource = Enum.GetNames(typeof(Cities));
Run Code Online (Sandbox Code Playgroud)
但是,它显示组合框中的值而不是字符串描述.所以我改用这个:
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
public static IList ToList(this Type type)
{
ArrayList list = new ArrayList();
Array enumValues = Enum.GetValues(type);
foreach (Enum value in enumValues)
{
list.Add(new KeyValuePair<Enum, …Run Code Online (Sandbox Code Playgroud) 我有一个1250字节长的固定长度字节数组.它可能包含以下类型的数据:
对象A由5个字节组成.第一个字节包含字母"A",接下来的四个字节存储1到100000的整数.
对象B由2个字节组成.第一个字节包含字母"B",下一个字节包含1到100的整数.
对象C由50个字节组成.所有50个字节用于存储ASCII编码的字符串,该字符串仅包含数字和以下字符: - +(和)
我不知道每个对象类型有多少在字节数组中,但我知道它们被组合在一起(对象B,对象B,对象A,对象A,对象A,对象C等).大多数情况下,当我解析一个字节数组时,该数组包含一种类型的数据(例如,所有项目都是对象A),所以我确切地知道每个项目包含多少字节,我只是循环处理字节的数组.在这种情况下,我有三种不同类型的数据,它们的长度都不同.我以为我需要做这样的事情:
int offset = 0;
while (offset <= 1250)
{
string objectHeader = Encoding.ASCII.GetString(byteArray, offset, 1);
if (objectHeader.Equals("A"))
{
// read 4 more bytes and then convert into int value (1 - 100000)
index += 5;
}
else if (objectHeader.Equals("B"))
{
// read 1 more byte and then convert into int value (1 - 100)
index += 2;
}
else
{
// read 49 more bytes and then convert into …Run Code Online (Sandbox Code Playgroud) 我是iOS/Swift开发的新手,我正在开发一个向REST API提出多个请求的应用程序.以下是检索"消息"的其中一个调用的示例:
func getMessages() {
let endpoint = "/api/outgoingMessages"
let parameters: [String: Any] = [
"limit" : 100,
"sortOrder" : "ASC"
]
guard let url = createURLWithComponents(endpoint: endpoint, parameters: parameters) else {
print("Failed to create URL!")
return
}
do {
var request = try URLRequest(url: url, method: .get)
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if let error = error {
print("Request failed with error: \(error)")
// TODO: retry failed request
} else if let …Run Code Online (Sandbox Code Playgroud) 我需要在固定大小的FIFO队列周围实现生产者/消费者模式.我认为围绕ConcurrentQueue的包装类可能适用于此,但我不完全确定(我之前从未使用过ConcurrentQueue).这种扭曲是队列只需要保存固定数量的项目(在我的例子中是字符串).我的应用程序将有一个生产者任务/线程和一个消费者任务/线程.当我的消费者任务运行时,它需要在那个时刻出现队列中存在的所有项目并对其进行处理.
对于它的价值,我的消费者处理排队的项目只不过是通过SOAP将它们上传到一个不是100%可靠的网络应用程序.如果无法建立连接或调用SOAP调用失败,我应该丢弃这些项目并返回队列以获取更多信息.由于SOAP的开销,我试图最大化队列中可以在一次SOAP调用中发送的项目数.
有时,我的制作人可能会比我的消费者能够删除和处理它们更快地添加项目.如果队列已满并且我的生产者需要添加另一个项目,我需要将新项目排队,然后将最旧的项目出列,以便队列的大小保持固定.基本上,我需要始终保留队列中生成的最新项目(即使这意味着某些项目不会被消耗,因为我的消费者当前正在处理以前的项目).
关于生产者如果队列中的项目是固定的那样保留数字,我从这个问题中发现了一个潜在的想法:
我目前在ConcurrentQueue周围使用一个包装类(基于该答案)和Enqueue()方法,如下所示:
public class FixedSizeQueue<T>
{
readonly ConcurrentQueue<T> queue = new ConcurrentQueue<T>();
public int Size { get; private set; }
public FixedSizeQueue(int size)
{
Size = size;
}
public void Enqueue(T obj)
{
// add item to the queue
queue.Enqueue(obj);
lock (this) // lock queue so that queue.Count is reliable
{
while (queue.Count > Size) // if queue count > max queue size, then dequeue an item
{
T objOut;
queue.TryDequeue(out objOut);
}
} …Run Code Online (Sandbox Code Playgroud) 如果我有这样的字符串......
"123[1-5]553[4-52]63244[19-44]"
Run Code Online (Sandbox Code Playgroud)
...验证以下条件的最佳方法是什么:
正则表达式是否能够验证所有这些场景?如果没有,LINQ怎么样?
我有一个在XAML中定义的WPF文本框,如下所示:
<Window.Resources>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<TextBox x:Name="upperLeftCornerLatitudeTextBox" Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:LatitudeValidationRule ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
如您所见,我的文本框绑定到我的业务对象上的一个名为UpperLeftCornerLatitude的十进制属性,如下所示:
private decimal _upperLeftCornerLongitude;
public decimal UpperLeftCornerLatitude
{
get { return _upperLeftCornerLongitude; }
set
{
if (_upperLeftCornerLongitude == value)
{
return;
}
_upperLeftCornerLongitude = value;
OnPropertyChanged(new PropertyChangedEventArgs("UpperLeftCornerLatitude"));
}
}
Run Code Online (Sandbox Code Playgroud)
我的用户将在此文本框中输入纬度值,为了验证该条目,我创建了一个如下所示的验证规则:
public class LatitudeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{ …Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
data-binding ×2
ios ×2
swift ×2
validation ×2
wpf ×2
arrays ×1
async-await ×1
asynchronous ×1
byte ×1
bytearray ×1
combobox ×1
enumeration ×1
linq ×1
nsurlsession ×1
parsing ×1
queue ×1
regex ×1
retry-logic ×1
selector ×1
string ×1
textbox ×1
warnings ×1
winforms ×1
xaml ×1
xcode ×1