小编Mar*_*kus的帖子

C#ActionCommand的翻译:ICommand到VB.net

我发现了一个C#类ActionCommand,它实现了ICommand并基于Execute和CanExecute的委托.到目前为止看起来对我来说非

  public class ActionCommand : ICommand
  {
    private readonly Action<object> _executeHandler;
    private readonly Func<object, bool> _canExecuteHandler;

    public ActionCommand(Action<object> execute, Func<object, bool> canExecute)
    {
      if (execute == null)
        throw new ArgumentNullException("Execute cannot be null");
      _executeHandler = execute;
      _canExecuteHandler = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter)
    {
      _executeHandler(parameter);
    }
    public bool CanExecute(object parameter)
    {
      if (_canExecuteHandler == null)
        return true;
      return _canExecuteHandler(parameter);
    }
  } …
Run Code Online (Sandbox Code Playgroud)

c# vb.net translation icommand

13
推荐指数
3
解决办法
2878
查看次数

Linq没有在VB.Net中分组

出于教育目的,我尝试将以下Linq表达式从"Linq in action"转换为VB.net

原创C#

var list = 
  from book in SampleData.Books
  group book by new { book.Publisher.Name, book.Subject }
    into grouping
  select new {
    Publisher = grouping.Key.Publisher,
    Subject = grouping.Key.Subject,
    Book = grouping
  };
Run Code Online (Sandbox Code Playgroud)

我的尝试:

Dim list = _books.GroupBy(Function(book) New With {.Publisher = book.Publisher.Name,
                                                    book.Subject}).
                  Select(Function(grouping) New With {.Publisher = grouping.Key.Publisher,
                                                      .Subject = grouping.Key.Subject,
                                                      .Books = grouping})

For Each item In list
  Console.WriteLine("Publisher:" & item.Publisher & ", Subject:" & item.Subject)
  For Each Book In item.Books
    Console.WriteLine("  " & Book.Title)
  Next …
Run Code Online (Sandbox Code Playgroud)

linq c#-to-vb.net

10
推荐指数
2
解决办法
4748
查看次数

如何在WPF中为listviewitem添加垂直行

我正在尝试在ListViewItem中的列之间放置垂直线.我试过给出的解决方案.但它不起作用.任何人都可以帮我解决这个问题.我为ListViewItem创建了一个单独的样式.我需要在样式中添加一些属性吗?

代码是这样的

<ListView x:Key="ListView1" ItemContainerStyle="{DynamicResource ListViewItemStyle1}">
<ListView.View>
    <GridView>
        <GridViewColumn Header="{TemplateBinding GridView.ColumnCollection}">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Border BorderBrush="#FF000000" BorderThickness="1,0,0,0" Margin="-6,-2,-6,-2">
                        <StackPanel Margin="6,2,6,2">
                            <TextBlock Text="{TemplateBinding Content}"/>
                        </StackPanel>
                     </Border>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)

字面上它应该工作,但它不起作用.我无法使用上面的代码添加垂直线.

wpf

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

将数字显示为来自绑定源的二进制

我需要将数字显示为二进制字符串(例如8 => 1000).当然我可以使用BitConverter转换它,并在代码隐藏文件中自己设置我的TextBox文本.但这看起来有些难看.是否可以将TextBox绑定到某个源并自动转换它?

binary binding ivalueconverter

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

VB.net中奇怪的调试器行为

一位同事在他的VB.net解决方案中发现了有线调试器行为.我承认这将是一个学术问题,因为这只会影响调试时突出显示的语句序列,而不会影响代码的整体行为.所以对于所有好奇的人:

我们将其拆除为以下最小控制台应用程序:

Private Sub PlayWithExceptions
    Dim a = 2
    Try
        throw new Exception("1")
    Catch ex As Exception
        If a = 2 Then
            Dim x = New XElement("Dummy")
        Else
            throw
        End If
    End Try
End Sub

Sub Main()
    Try
        PlayWithExceptions()
    Catch ex As Exception
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

很明显,调试器会抛出异常("1"),调试器会跳转到PlayWithExceptions方法的catch子句中.在那里,由于"a"始终为2,调试器跳转到一些虚拟代码(New XElement ...),从那里跳到"End If",最后返回到Else-leaf到throw语句.我承认Visual Studio不会重新抛出异常,但它看起来很奇怪.

将条件"If a = 2"更改为"If True"会消除此行为.

重构为条件捕获也消除了这种行为.

Private Sub PlayWithExceptions
    Dim a = 2
    Try
        throw new Exception("1")
    Catch ex As Exception When a = 2
        Dim …
Run Code Online (Sandbox Code Playgroud)

c# vb.net debugging exception

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