我正在尝试在WPF应用程序中使用自定义控件,并且使用StringFormat绑定时遇到一些问题.
这个问题很容易重现.首先,让我们创建一个WPF应用程序并将其命名为"TemplateBindingTest".在那里,添加一个只有一个属性(Text)的自定义ViewModel,并将其分配给Window的DataContext.将Text属性设置为"Hello World!".
现在,向解决方案添加自定义控件.自定义控件尽可能简单:
using System.Windows;
using System.Windows.Controls;
namespace TemplateBindingTest
{
public class CustomControl : Control
{
static CustomControl()
{
TextProperty = DependencyProperty.Register(
"Text",
typeof(object),
typeof(CustomControl),
new FrameworkPropertyMetadata(null));
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
public static DependencyProperty TextProperty;
public object Text
{
get
{
return this.GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将自定义控件添加到解决方案时,Visual Studio会自动创建一个带有generic.xaml文件的Themes文件夹.我们将控件的默认样式放在那里:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TemplateBindingTest">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<TextBlock Text="{TemplateBinding Text}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
现在,只需将控件添加到窗口,并使用StringFormat在Text属性上设置绑定.还要添加一个简单的TextBlock以确保绑定语法正确:
<Window x:Class="TemplateBindingTest.MainWindow" …Run Code Online (Sandbox Code Playgroud) 我正在用Java编写一段代码(我对Java很新),我之前用C#编写过.这是C#中的代码和示例.
ushort number = 0xAABB; // 43707
byte[] data = new byte[2];
EndianBitConverter.Big.CopyBytes(number, data, 0); // value[0] = 170, value[1] = 187
Run Code Online (Sandbox Code Playgroud)
我在.NET中使用自定义位转义器,因为它默认为小端.无论如何,根据我对java的理解,如果我想使用与byte []相同的结果,我应该期望我的值(170和187)小一点(Byte.MAX_VALUE + 1),即(42,59) - 由于.net和java具有不同的字节范围.这是我用Java编写的模仿上述逻辑的内容.
public class Ushort {
private int value = 0;
public Ushort(int i) {
value = i - (Short.MAX_VALUE + 1);
}
public int get() {
return value;
}
public byte[] getBytes() {
byte[] result = new byte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value};
return …Run Code Online (Sandbox Code Playgroud) 我有一个场景,我需要以特定的间隔运行某个任务,但是,我希望能够重置/重新启动计时器而不重新实例化.这是我的代码,以便更好地理解.
private TimerTask beatTask = new TimerTask() {
@Override
public void run() {
beatDetected();
}
};
public void beatDetected() {
timeoutTimer.cancel();
// handle my stuff and restart the timer.
timeoutTimer.schedule(beatTask, 2000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
(timeoutTimer = new Timer()).schedule(beatTask, 2000);
return Service.START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
这个实现背后的想法是beatDetected()可以从外部事件调用,在这种情况下,下一个计时器滴答应该从那一刻起发生,即应该重置下一个任务的经过时间.但是,我只得到第一个滴答,从那时起,计时器就不起作用了.我不限于使用Timer类,任何可以解决上述场景的方法都可以.我正在使用postDelayed,但这段代码位于一个服务中,我真的不需要UI线程感知更新.
我正在寻找一些快速有效的方法来合并数组中的项目.这是我的情景.该集合按From排序.相邻元素不一定相差1,即最后一个To和下一个From之间可能存在间隙,但它们从不重叠.
var list = new List<Range>();
list.Add(new Range() { From = 0, To = 1, Category = "AB" });
list.Add(new Range() { From = 2, To = 3, Category = "AB" });
list.Add(new Range() { From = 4, To = 5, Category = "AB" });
list.Add(new Range() { From = 6, To = 8, Category = "CD" });
list.Add(new Range() { From = 9, To = 11, Category = "AB" }); // 12 is missing, this is ok
list.Add(new Range() …Run Code Online (Sandbox Code Playgroud) 我有这个方法,我需要在我的应用程序中调用和使用,但我不知道真的知道如何完成它.
这是我需要调用的功能.
[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(int Position, int Length, char* Buffer, int* DataRead);
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我有这个功能,我错过了它的实现.
internal static void GetDataToBuffer(int position, int length, out byte[] data, out int dataRead)
{
unsafe
{
// the code I need
}
}
Run Code Online (Sandbox Code Playgroud)
我认为大部分都是非常自我规划的.我需要实现后一个函数,这样我才能将数据读入缓冲区和读取的数据量(实际上应与data.Length相同,但制造商将此作为单独的选项,所以我需要它).有人可以帮忙吗?这够清楚了吗?
谢谢
编辑:这是.h文件中的非托管声明.希望能帮助到你.
extern NAG_DLL_EXPIMP int DTS_GetDataToBuffer(int Position,
int Length,
unsigned char *Buffer,
int *DataRead );
Run Code Online (Sandbox Code Playgroud)
编辑#2:位置 - 星形读取数据的位置.长度 - 要读取的数据量(这将是缓冲区大小).DataRead - 读取的实际数据大小.
我一直试图在谷歌和SO上找到答案
但我发现的所有地方都使用匿名类型的结果列表
我想要实现的是采取一个 List<SecondaryStandard>
并创建一个分组列表 SecondaryStandard
每个都SecondaryStandard看起来像这样
public class SecondaryStandard
{
public string Id { get; set; }
public int IdNumeric { get; set; }
public string IdText { get; set; }
public Sample Sample { get; set; }
public string StandardName { get; set; }
public DateTime DateCompleted { get; set; }
public SamplePoint SamplingPoint{ get; set; }
public Instrument Instrument{ get; set; }
public string ContainerId { get; set; }
public double Value { get; set; …Run Code Online (Sandbox Code Playgroud)