小编Wal*_*ams的帖子

.NET Windows服务需要使用STAThread

我创建了一个将调用某些COM组件的Windows服务,因此我将[STAThread]标记为Main函数.但是,当计时器触发时,它会报告MTA并且COM调用失败.我怎样才能解决这个问题?

using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Timers;



namespace MyMonitorService
{
    public class MyMonitor : ServiceBase
    {
        #region Members
        private System.Timers.Timer timer = new System.Timers.Timer();
        #endregion

        #region Construction
        public MyMonitor ()
        {
            this.timer.Interval = 10000; // set for 10 seconds
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
        }
        #endregion

        private void timer_Elapsed (object sender, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("MyMonitor", String.Format("Thread Model: {0}", Thread.CurrentThread.GetApartmentState().ToString()), EventLogEntryType.Information);
        }

        #region Service Start/Stop
        [STAThread]
        public static void Main ()
        {
            ServiceBase.Run(new MyMonitor());
        }

        protected override void …
Run Code Online (Sandbox Code Playgroud)

.net windows-services sta

14
推荐指数
3
解决办法
2万
查看次数

C++ 和 C# 双精度十六进制值之间的差异

我正在用 C# (net6.0) 替换一些写入二进制文件的 C++ 代码,并且我注意到写入文件的值之间存在差异。

如果我的双精度值等于 0.0,C++ 将字节写入为:

00 00 00 00 00 00 00 00

然而,C#(使用 BinaryWriter)写入的值与以下内容相同:

00 00 00 00 00 00 00 80

如果我使用 System.BitConverter.GetBytes() 将 0.0 转换为字节,我会得到所有 0x00。如果我使用 BitConverter 将 0x80 的字节转换为双精度,它仍然给我 0.0。

byte[] zero = System.BitConverter.GetBytes(0.0); // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
bool equal = 0.0 == System.BitConverter.ToDouble(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}); // true
Run Code Online (Sandbox Code Playgroud)

这似乎不会造成任何问题,但我想了解发生了什么。

.net c# c++ byte

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

允许用户在WPF中调整Expander的大小

我有很多C#和WinForms经验,但我是WPF的新手.我有一个带扩展器的窗口向下扩展.就像我正在输入的问题框一样,我希望用户能够通过点击底部的字形(如此问题框)并将扩展器拖动到所需的大小来动态调整Expander的大小.

任何人都可以提供XAML(以及任何其他代码)来执行此操作吗?

这是我到目前为止:

<Expander Header="Live Simulations" Name="expandLiveSims" Grid.Row="0" ExpandDirection="Down" IsExpanded="True">
    <Expander.Background>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="White" Offset="0" />
            <GradientStop Color="LightGray" Offset="0.767" />
            <GradientStop Color="Gainsboro" Offset="1" />
        </LinearGradientBrush>
    </Expander.Background>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <DataGrid Height="250" Margin="5" Name="gridLiveProducts" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0">
        </DataGrid>
        <GridSplitter Grid.Row="0" Grid.Column="1" Width="3" VerticalAlignment="Stretch" HorizontalAlignment="Center">
            <GridSplitter.Background>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="White" Offset="0" />
                    <GradientStop Color="DarkGray" Offset="0.25" />
                    <GradientStop Color="DarkGray" Offset="0.75" />
                    <GradientStop Color="Gainsboro" …
Run Code Online (Sandbox Code Playgroud)

wpf wpf-controls wpf-4.0

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

垂直列出的WPF RibbonComboBox项

我正在开发一些我有一个RibbonComboBox的XAML:

<RibbonComboBox SelectionBoxWidth="150" Grid.Row="0">
    <RibbonGallery SelectedItem="{Binding SelectedUtilityRun, Mode=TwoWay}">
        <RibbonGalleryCategory ItemsSource="{Binding UtilityRunLabels}" />
    </RibbonGallery>
</RibbonComboBox>
Run Code Online (Sandbox Code Playgroud)

当它显示时,它按照我的预期水平而不是垂直显示项目: 在此输入图像描述

如何设置样式以垂直放置项目?

c# wpf ribbon

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

将稀疏矩阵写入 csv 时遇到问题

我是 Python 新手,我正在尝试将矩阵中的数据写入 CSV 文件。该变量定义为:

(Pdb) trainFeatures
<1562936x312116 sparse matrix of type '<type 'numpy.float64'>'
with 43753231 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)

我有一行代码:

numpy.savetxt("feature_train.csv", trainFeatures, delimiter=',')
Run Code Online (Sandbox Code Playgroud)

当我运行该行时,我收到一条错误消息:

ncol = X.shape[1]
IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)

我确信矩阵的格式不正确,但我不知道如何得到它。谁能指出我需要在这里做什么?

python csv numpy python-2.7 export-to-csv

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

使用C#创建新的Excel公式/函数

我们希望能够以编程方式创建一个Excel工作簿,该工作簿将从单元格中调用自定义代码.单元格看起来像:

=MyCode(A1:A10)
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是使用VBA,但由于该算法是专有的,因此希望它受到保护.我可以在上面输入一个密码,但是有关如何绕过这些密码的详细记录(在StackOverflow上).

我的第二个想法是在Visual Studio中创建一个Excel 2013 Workbook项目,但我没有找到任何有关如何在C#中公开函数的有用信息,因此可以像我描述的那样调用它.

接下来我想到让VBA调用C#,并在https://msdn.microsoft.com/en-us/library/bb608613.aspx上找到说明.我按照这些说明操作,但当我尝试运行VBA代码时,我得到了GetManagedClass函数的错误:不支持对象库功能.

关于如何做这样的事情有什么好的参考吗?

c# excel vba add-in excel-vba visual-studio-2013

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

在编辑 WPF DataGrid 单元格期间显示弹出窗口

与如何正确在 DataGridTemplateColumn.CellEditingTemplate 中放置弹出窗口相同?,我试图在编辑单元格时让 PopUp 出现在 DataGrid 中的单元格下方,并在不再编辑单元格时消失。最后一点是,PopUp 内容是根据列动态的,并且列是通过绑定动态创建的。

我从以下 XAML 开始,但收到 XamlParseException“向‘System.Windows.Controls.ItemCollection’类型的集合添加值引发了异常”。

<DataGrid ItemsSource="{Binding Path=Options}">
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
                <Grid>
                    <Popup Placement="Bottom" IsOpen="True" Width="200" Height="100">
                        <TextBlock>Somethingn here</TextBlock>
                    </Popup>
                </Grid>
            </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

c# wpf datagrid popup mvvm

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

从上下文菜单绑定ElementName找不到目标

我正在尝试从下拉菜单按钮(来自http://shemesh.wordpress.com/2011/10/27/wpf-menubutton/)中的上下文菜单绑定到元素。即使在上下文菜单外绑定似乎起作用,但上下文菜单内的绑定却无效。

这是XAML(非常简化):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" CanContentScroll="False">
            <ListBox x:Name="lbScenarios"  HorizontalContentAlignment="Stretch">
                <ItemsControl.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ItemsPresenter Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth}"/>
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border>
                            <Expander>
                                <Expander.Header>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Margin="5,0,0,0" Grid.Column="0" VerticalAlignment="Center">Results</TextBlock>
                                        <local:MenuButton Grid.Column="3" Content="Menu" Margin="5,0,0,0" VerticalAlignment="Center">
                                            <local:MenuButton.Menu>
                                                <ContextMenu>
                                                    <MenuItem Header="Save pie chart as image"
                                                                              Command="{Binding SaveChartImageCommand}"
                                                                              CommandParameter="{Binding ElementName=pieChart}" />
                                                    <MenuItem Header="Save bar …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml element-binding

0
推荐指数
1
解决办法
992
查看次数