我有一些代码返回我的字典中的唯一元素,但我还想返回重复元素的计数.基本上将字典[key,uniqueelement]更改为字典[uniqueelement,count].这是我的代码,只返回唯一元素.
var uniqueItems = deviceInstances.Children.GroupBy(pair => pair.Value.Information.UnderlyingDeviceType)
.Select(group => group.First())
.ToDictionary(pair => pair.Key, pair => pair.Value.Information.UnderlyingDeviceType.ToString());
Run Code Online (Sandbox Code Playgroud) 我试图在一个通用的EventArgs类中保存一个对象,但由于EventHandler有一个接口,我很难完成这个.有什么办法让这样的工作吗?
我的EventArgs类:
public class PositionChangedEventArgs<T>
{
public PositionChangedEventArgs(byte position, T deviceArgs)
{
Position = position;
DeviceArgs = deviceArgs;
}
public byte Position { get; private set; }
public T DeviceArgs { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
正在使用的接口:
public interface IMoveable
{
event EventHandler<PositionChangedEventArgs<T>> PositionChanged;
}
Run Code Online (Sandbox Code Playgroud)
示例类用法:
public class SomeDevice : IMoveable
{
public event EventHandler<PositionChangedEventArgs<DeviceSpecificEventMessageArgs>> PositionChanged; //Compiler doesn't like this
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何将自定义数据类型绑定到TreeView.
数据类型基本上是包含其他arraylists的对象的arraylist.访问将看起来像这样:
foreach (DeviceGroup dg in system.deviceGroups)
{
foreach (DeviceType dt in dg.deviceTypes)
{
foreach (DeviceInstance di in dt.deviceInstances)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望TreeView看起来像这样:
DeviceGroup1
--> DeviceType1
--DeviceInstance1
--DeviceInstance2
--> DeviceType2
--DeviceInstance1
Run Code Online (Sandbox Code Playgroud)
DeviceGroup2
--> DeviceType1
--DeviceInstance1
--> DeviceType2
Run Code Online (Sandbox Code Playgroud) 似乎不允许这样的事情。任何解决方法?
<Style x:Key=MyDerivedStyle TargetType="{x:Type Button}"
BasedOn="{DynamicResource GlobalButtonStyle}" />
<Style x:Key="GlobalLabelStyle" TargetType="{x:Type Button}">
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:无法在“样式”类型的“BasedOn”属性上设置“DynamicResourceExtension”。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。
如果我将其更改为 StaticResource,则该样式不会出现在我的控件中。
我有一个Person类型的列表,我想基于DisplayName更新DisplayValue.我怎么能做到这一点?
public class Person
{
public string DisplayName { get; set; }
public string DisplayValue { get; set; }
... other properties
}
Run Code Online (Sandbox Code Playgroud) 我有一个函数调用每个对象的测试.如果当前测试失败,我希望能够重新测试.
foreach (TestObject test in Tests)
{
test.RunTest()
}
//This is in the TestObject class
RunTest()
{
if (failure)
{
//Want to be able to run RunTest() again without interrupting the foreach loop.
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为ApiCalls()的函数,它包含在一个锁定器中,因为我使用的api不是多线程安全的.偶尔api调用无法返回,我想不出办法处理这种情况.我正在考虑在锁定对象上创建一个计时器,但似乎锁定器没有这样的东西.