小编Rac*_*hel的帖子

是否有一个C#库,其行为类似于Active Directory的权限和组?

我喜欢权限和组在Active Directory中工作的方式,但我不想将我的应用程序与AD绑定.

是否存在包含与AD相同功能的现有库?特别是,能够创建组,将用户分配到组,添加组的权限,以及查看用户或组的应用权限?

c# permissions authorization usergroups active-directory

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

如何合并XML文件?

我有两个xml文件,它们都具有相同的模式,我想合并到一个xml文件中.是否有捷径可寻?

例如,

<Root>
    <LeafA>
        <Item1 />
        <Item2 />
    </LeafA>
    <LeafB>
        <Item1 />
        <Item2 />
    </LeafB>
</Root>
Run Code Online (Sandbox Code Playgroud)

+

<Root>
    <LeafA>
        <Item3 />
        <Item4 />
    </LeafA>
    <LeafB>
        <Item3 />
        <Item4 />
    </LeafB>
</Root>
Run Code Online (Sandbox Code Playgroud)

=包含新文件

<Root>
    <LeafA>
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
    </LeafA>
    <LeafB>
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
    </LeafB>
</Root>
Run Code Online (Sandbox Code Playgroud)

c# xml

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

如何使网格列始终具有相同的宽度?

如果我将Column的宽度设置为*,则它们最初的宽度相同,但如果某个项目大于允许的数量,则它将拉伸列宽.

如何通过明确定义大小来强制我的Grid保持其列的大小相同?

我不能使用UniformGrid,因为这个Grid正在ItemsControl中使用,而Items需要放在特定的Grid.Row/ Grid.Columnspot中

编辑这是我当前代码的示例.

<DockPanel>

    <!-- Not showing code here for simplicity -->
    <local:ColumnHeaderControl DockPanel.Dock="Top" />
    <local:RowHeaderControl DockPanel.Dock="Left" />

    <ItemsControl ItemsSource="{Binding Events}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" 
                        Value="{Binding DueDate.DayOfWeek, 
                            Converter={StaticResource EnumToIntConverter}}" />
            </Style>
        </ItemsControl.ItemContainerStyle>

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsPanel>
    </ItemsControl>

</DockPanel>
Run Code Online (Sandbox Code Playgroud)

编辑#2这是我的最终解决方案.它使列的大小正确,并在应用程序调整大小时保持大小正确.

<ColumnDefinition Width="{Binding 
    ElementName=RootControl, 
    Path=ActualWidth, 
    Converter={StaticResource MathConverter}, 
    ConverterParameter=(@VALUE-150)/7}" …
Run Code Online (Sandbox Code Playgroud)

wpf grid xaml

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

当我想将CSS样式应用于文本块时,使用的理想标签是什么?

要设置文本样式,我通常使用以下之一:

<span class="header">...</span>
<div class="header">...</div>
<p class="header">...</p>
<label class="header">...</label>
Run Code Online (Sandbox Code Playgroud)

但实际上,我使用哪个标签将css样式应用于文本块是否真的很重要?

通常我<div>用于应该在单个块中的所有内容(如标题或脚注),或<span>用于内联的所有内容(如段落中的强调文本),但是我最近发现自己使用<div>标记来设置文本样式*Required,我认为这似乎是有点傻,因为它只是一个单词,我开始想知道这是否是"正确"的做事方式,或者如果一个标签比另一个标签更好用于这样的简单文本样式.

当我想将css类应用于文本块时,是否存在某种关于使用哪个标记的标准?如果是这样,哪些因素决定使用哪个标签以及何时使用?

html css

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

什么是将DataTable转换为对象的最有效方法[,]?

我有一堆DataTables需要转换为object[,]数组(而不是 object[][]数组).在性能方面,最有效的方法是什么?

我知道我可以通过我的建设做这个object[dt.Rows.Count, dt.Columns.Count]最初,然后通过行循环和解析每一行到阵列中的地方,但我相当肯定还有其他方法,如使用LINQ或System.Data特定的功能,如dataRow.ToItemArray()它可更有效率.

DataTables是可变大小,除了字符串之外,还包含需要格式化的日期和数字.

例如,如果包含我的一个数据表

Id    Name    Date                 Value
1     Rachel  1/1/2013 00:00:00    100.0000
2     Joseph  3/31/2012 00:00:00   50.0000
3     Sarah   2/28/2013 00:00:00   75.5000

那么我想要一个object[,]包含完全相同数据的数组(理想情况下是标题),但是需要格式化的日期和值

arr[x,0] = row[x].Field<int>("Id");
arr[x,1] = row[x].Field<string>("Name");
arr[x,2] = row[x].Field<DateTime>("Date").ToString("M/d/yy");
arr[x,3] = row[x].Field<decimal>("Value").ToString("C2"); // Currency format
Run Code Online (Sandbox Code Playgroud)

c# linq datatable c#-4.0

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

为什么TreeView之间/内部的Tab顺序不起作用?

我有一个跟随xaml的窗口:

<Window x:Class="TestDemoApp.TreeViewWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TreeViewWindow" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="Control" x:Key="FocusedStyle">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle StrokeThickness="1"
                              Stroke="Red"
                              StrokeDashArray="1 2 3 4"
                              SnapsToDevicePixels="true"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="TreeViewItem">
            <Setter Property="IsTabStop" Value="True"/>
            <Setter Property="Focusable" Value="True"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
        </Style>
        <Style TargetType="ListViewItem">
            <Setter Property="IsTabStop" Value="True"/>
            <Setter Property="Focusable" Value="True"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ListView TabIndex="1" BorderThickness="5" Focusable="True" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" FocusVisualStyle="{StaticResource FocusedStyle}">
            <ListViewItem TabIndex="2" Content="List Item …
Run Code Online (Sandbox Code Playgroud)

wpf treeview

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

可以将类型用作字典键吗?

我想创建一个最多可以存储一个对象副本的类。此处存储的所有对象将共享相同的基类,我希望能够根据其类型获取对象。

到目前为止,我已经提出了这个解决方案,但是我觉得使用Type作为Dictionary键会出错。

在多个模块中使用的基类

interface ISessionVariables { }
Run Code Online (Sandbox Code Playgroud)

用于访问的普通单例类的示例

public class SessionVariables
{
    private object _sync = new object();
    private Dictionary<Type, ISessionVariables> _sessionVariables = 
        new Dictionary<Type, ISessionVariables>;

    public T Get<T>()
        where T : ISessionVariable, new()
    {
        lock (_sync)
        {
            ISessionVariables rtnValue = null;
            if (_sessionVariables.TryGetValue(typeof(T), out rtnValue))
                return (T)rtnValue;

            rtnValue = new T();
            _sessionVariables.Add(typeof(T), rtnValue);

            return (T)rtnValue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样我可以从各个模块中这样称呼它

SessionVariableSingleton.Get<ModuleASessionVars>().PropertyA;

SessionVariableSingleton.Get<ModuleCSessionVars>().PropertyC;
Run Code Online (Sandbox Code Playgroud)

这是存储这种数据结构的可接受方法吗?还是有一个更好的选择,使用没有类型键的列表或字典?

c# dictionary .net-3.5 data-structures

6
推荐指数
2
解决办法
2572
查看次数

从4.0升级到4.5后,MSBuild无法再找到依赖项?

我们有一个.Net 3.5应用程序,它是使用一些调用的脚本构建的 msbuild.exe

最近我们所有的机器都开始自动从.Net 4.0更新到.Net 4.5作为公司范围内的政策的一部分,我们的构建脚本开始失败.

给出的错误是他们找不到引用的程序集,如下所示:

error CS0012: The type 'System.Drawing.Image' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

看起来在每种情况下,我们都尝试构建具有对项目B的引用的项目A,并且项目B具有对库X的引用,并且获得项目A需要对库X的引用的错误.

一个临时的解决方法是卸载4.5,卸载4.0,然后重新安装4.0,但这是耗时的,并且在更新通常是静默和自动的环境中不实用.

我已经尝试过使用以下msbuild交换机,没有运气

  • /toolsversion:3.5- Func未定义的例外情况
  • /toolsversion:4.0 - 因为4.5替换了4.0工具不起作用
  • /p:TargetFrameworkVersion="v3.5" - 同样的错误
  • /p:VisualStudioVersion=11.0 - csproj文件中的相同错误*` - 已经存在,同样的错误

.csproj文件已<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>在我的解决方案中的每个csproj文件中指定,并且ToolsVersion="4.0".

我也尝试将更TargetFrameworkVersion改为4.0,这也不起作用.

我发现了一些笔记网上关于微软改变标志OnlyReferenceAndBuildProjectsEnabledInSolutionConfigurationfalsetrue4.0至4.5升级过程中,但是手动设置该标志中的csproj文件还没有解决的问题.

<OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration>
    false
</OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration>
Run Code Online (Sandbox Code Playgroud)

为什么msbuild在从4.0升级到4.5后无法找到这些子程序集,我该如何解决?

更新

我终于找到了问题的根源,但我不知道这是怎么回事.

ProjectA有一个类,它继承自ProjectB中的抽象类,其中一个属性ProejctB.BaseClass是类型System.Drawing.Image.

namespace ProjectA
{
    public …
Run Code Online (Sandbox Code Playgroud)

c# msbuild csc .net-3.5 .net-4.5

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

为什么我的TextBlock/TextBox不能应用基本样式的值?

对于我来说,为数据输入表单的样式编写类似的东西并不罕见,但我的问题是,TextBox并且TextBlock似乎没有实现在中的Setter BaseElementStyle.通常我需要单独定义它们.

为什么是这样?它有办法吗?

我猜它与其他控件模板中常用的事实有关(例如,TextBlock用于大多数控件,TextBox用于DatePickers和ComboBoxes)

<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}">
    <Setter Property="Margin" Value="5" />
    <Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" />
Run Code Online (Sandbox Code Playgroud)

wpf inheritance xaml coding-style basedon

5
推荐指数
2
解决办法
3201
查看次数

如何跟踪循环中完成的异步任务数量?

我有一些代码循环遍历记录列表,为每个记录启动导出任务,每次任务完成时将进度计数器增加1,以便用户知道进程的距离.

但是根据我的循环的时间,我经常看到输出在较低的数字之前显示更高的数字.

例如,我希望看到这样的输出:

Exporting A
Exporting B
Exporting C
Exporting D
Exporting E
Finished 1 / 5
Finished 2 / 5
Finished 3 / 5
Finished 4 / 5
Finished 5 / 5

但相反,我得到这样的输出

Exporting A
Exporting B
Exporting C
Exporting D
Exporting E
Finished 1 / 5
Finished 2 / 5
Finished 5 / 5
Finished 4 / 5
Finished 3 / 5

我不希望输出是准确的,因为当我更新/使用它时我没有锁定值(有时它输出相同的数字两次,或跳过一个数字),但我不希望它倒退.

我的测试数据集是72个值,相关代码如下所示:

var tasks = new List<Task>();
int counter = 0;

StatusMessage = string.Format("Exporting …
Run Code Online (Sandbox Code Playgroud)

c# multithreading task task-parallel-library c#-4.0

5
推荐指数
2
解决办法
2907
查看次数