小编Pet*_*erM的帖子

无法对类型参数值使用类型断言

我们不能对泛型类型变量使用类型断言。考虑到它是 允许的interface{},但不是受 约束的泛型,这似乎是非常奇怪的行为interface{}。想知道是否有任何解决方法?

// This works
func isInt(x interface{}) bool {
    _, ok := x.(int)
    return ok;
}

// Compile Error
// invalid operation: cannot use type assertion on type parameter 
// value x (variable of type T constrained by interface{})
func isInt2[T interface{}](x T) bool {
    _, ok := x.(int)
    return ok;
}
Run Code Online (Sandbox Code Playgroud)

generics go

31
推荐指数
1
解决办法
1万
查看次数

WPF拖拽 - DragLeave什么时候开火?

从父控件拖动到子控件时,我得到DragLeave事件.我只希望在超出控件范围时获得此事件.我该如何实现呢?

请参阅这个简单的示例应用程序.

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Height="50" >Hilight and Drag this text</TextBox>
        <Border BorderBrush="Blue" BorderThickness="2">
            <StackPanel AllowDrop="True" Name="Stack" >
                <Label >If I drag text across the gray line, Stack.DragLeave will fire.</Label>
                <Separator></Separator>
                <Label>I only expect to get this event when leaving the blue rectangle. </Label>
            </StackPanel>
        </Border>
        <TextBlock >Stack.DragLeave Count: <Label x:Name="countLabel" /></TextBlock>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

并在后面的代码中

Class MainWindow

    Private Sub Stack_DragLeave(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs) Handles Stack.PreviewDragLeave
        countLabel.Content = countLabel.Content + 1 …
Run Code Online (Sandbox Code Playgroud)

.net vb.net wpf

15
推荐指数
1
解决办法
5648
查看次数

同步等待而不阻止UI线程

是否有一个同步等待函数不会占用.NET WPF中的UI线程?就像是:

Sub OnClick(sender As Object, e As MouseEventArgs) Handles button1.Click
     Wait(2000) 
     'Ui still processes other events here
     MessageBox.Show("Is has been 2 seconds since you clicked the button!")
End Sub
Run Code Online (Sandbox Code Playgroud)

.net vb.net wpf multithreading wait

8
推荐指数
1
解决办法
5898
查看次数

正则表达式查找缺少字母的单词

我试图用正则表达式匹配最多两个缺少字母的单词.例如,如果感兴趣的单词是'hello',我想匹配以下字符串:

你好地狱helo hllo ello hel heo elo llo

我可以使用正则表达式了吗?l?o?匹配这些,但这也将匹配0,1和2个字母字符串.如何要求匹配长度为3-5个字符?

谢谢您的帮助.

regex

6
推荐指数
1
解决办法
2529
查看次数

为什么我不能在事件触发器中放置一个Setter

我试图为支持拖放的列表框添加一些可视化反馈.好像我应该能够将一些setter添加到EventSetter并完成.但是,事件排定器不支持setter.我是否真的必须制作故事板才能实现此行为?

微软对此的理性是什么?

   <Style TargetType="{x:Type ListBox}">
        <Style.Triggers>
             <EventTrigger RoutedEvent="DragEnter">
                 <!--WHy Can't i Add seters here? e.g.
                <Setter Property="ForeColor" Value="Red"> 
                -->
            </EventTrigger>
        </Style.Triggers>
    </Style>
Run Code Online (Sandbox Code Playgroud)

.net wpf styles

5
推荐指数
1
解决办法
5069
查看次数

为什么用-fpic和-pie编译的程序有重定位表?

如果使用以下命令编译一个简单的程序:

arm-none-eabi-gcc -shared -fpic -pie --specs=nosys.specs simple.c -o simple.exe
Run Code Online (Sandbox Code Playgroud)

并使用以下命令打印重定位条目:

arm-none-eabi-readelf simple.exe -r
Run Code Online (Sandbox Code Playgroud)

有一堆重定位条目部分(见下文)。

由于 -fpic / -pie 标志会导致编译器生成位置无关的可执行文件,因此我天真的(并且显然不正确)假设不需要重定位表,因为加载程序可以将可执行映像放置在任何地方而不会出现问题。那么为什么那里有一个重定位表,这是否表明代码实际上不是位置无关的?

Relocation section '.rel.dyn' at offset 0x82d4 contains 37 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
000084a8  00000017 R_ARM_RELATIVE   
000084d0  00000017 R_ARM_RELATIVE   
00008508  00000017 R_ARM_RELATIVE   
00008510  00000017 R_ARM_RELATIVE   
0000855c  00000017 R_ARM_RELATIVE   
00008560  00000017 R_ARM_RELATIVE   
00008564  00000017 R_ARM_RELATIVE   
00008678  00000017 R_ARM_RELATIVE   
0000867c  00000017 R_ARM_RELATIVE   
0000870c  00000017 R_ARM_RELATIVE   
00008710  00000017 R_ARM_RELATIVE   
00008714  00000017 R_ARM_RELATIVE   
00008718  00000017 R_ARM_RELATIVE   
00008978  00000017 R_ARM_RELATIVE   
000089dc  00000017 R_ARM_RELATIVE   
000089e0 …
Run Code Online (Sandbox Code Playgroud)

c gcc position-independent-code

5
推荐指数
1
解决办法
1540
查看次数

在 WPF 中获取 TreeViewItem 的 Header 的高度

我试图找到 TreeViewItem 的标题部分的高度以绘制装饰器。但是,我有一个 TreeViewItem 的实例,我不确定如何在代码中获取标题元素的 VisualTreeRootNode。我正在寻找类似的东西:

FrameworkElement headerElement = 
     _myTreeViewItem.HeaderTemplate.GetVisualTreeRootNode();
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

.net c# wpf

4
推荐指数
1
解决办法
1205
查看次数

在C++中初始化引用和变量

给出以下c ++函数:

int& ReturnAReference() {
   /* Do something here */
}
Run Code Online (Sandbox Code Playgroud)

这两个陈述之间有什么区别:

int normalVariable = ReturnAReference();
int& referenceVariable = ReturnAReferene();
Run Code Online (Sandbox Code Playgroud)

一个版本比另一个更受欢迎吗?

c++

4
推荐指数
1
解决办法
137
查看次数

GoLang Struct Initializer 的执行顺序

想知道 GoLang 中是否保证结构初始化的执行顺序。

以下代码是否总是产生

obj.a == 1或者obj.b == 2这是未指定的行为?

num := 0

nextNumber := func() int {
    num += 1
    return num
}

type TwoNumbers struct {
    a int
    b int
}

obj := TwoNumbers{
    a: nextNumber(),
    b: nextNumber(),
}
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
1
解决办法
466
查看次数

指定在.net中实现2个接口的变量

是否可以在.net中实现具有2个接口的类型说明符?就像是:

Public Sub New(obj as ICollection and INotifyCollectionChanged)
    ''Initialize Code
End Sub
Run Code Online (Sandbox Code Playgroud)

.net vb.net clr

2
推荐指数
1
解决办法
169
查看次数

通用GDI +错误

我使用以下代码获得通用GDI错误.通常它符合并执行得很好但有时会因Generic GDI + Error而失败.有没有办法解决这个问题,或者是一种不使用inter-op拍摄截图的方法?

Public Function CopyImageFromScreen(region as Int32Rect) As BitmapSource

    Dim img As New System.Drawing.Bitmap(region.Width, region.Height)
    Dim gfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(img)
    gfx.CopyFromScreen(region.X, region.Y, 0, 0, New System.Drawing.Size(region.Width, region.Height))
    img.LockBits(New System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, img.PixelFormat)

    Dim hObject As IntPtr = img.GetHbitmap 'This Line Causes the Error

    Dim WpfImage As BitmapSource = Interop.Imaging.CreateBitmapSourceFromHBitmap(hObject, IntPtr.Zero, _
        System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())

    Return WpfImage

End Function
Run Code Online (Sandbox Code Playgroud)

.net vb.net wpf gdi+

2
推荐指数
1
解决办法
2238
查看次数

标签 统计

.net ×6

wpf ×5

vb.net ×4

go ×2

c ×1

c# ×1

c++ ×1

clr ×1

gcc ×1

gdi+ ×1

generics ×1

multithreading ×1

position-independent-code ×1

regex ×1

styles ×1

wait ×1