小编Dav*_*ave的帖子

与创建委托相比,关键字委托如何工作

我正慢慢地围绕代表,因为委托的签名必须与它委派的方法相匹配.

但是,请查看以下代码.

public static void Save()
{
    ThreadStart threadStart = delegate
    {
        SaveToDatabase();
    };
    new Thread(threadStart).Start();
}

private static void SaveToDatabase() { }
Run Code Online (Sandbox Code Playgroud)

我现在感到难过,因为代表返回void(因为那是什么SaveToDatabase())但是,它显然正在返回ThreadStart......或者是它?

如果我要编写自己的委托,我不知道如何实现这一点,因为委托必须是无效的,以匹配返回类型SaveToDatabase().但它不可能; 它会是类型ThreadStart!

我的问题是,我是完全误解了还是通过一些.NET技巧使这成为可能?如果我想写这个方法但是创建我自己的委托,我该怎么办?

c# delegates

7
推荐指数
2
解决办法
270
查看次数

为什么不对所有私有方法使用静态方法?

我试图理解静态方法,我已经达成了一个令人困惑的问题.

仅关注此问题的方法,如果我创建了我的对象的实例(其中类本身不是静态的),那么我通常只能访问public,protected和/或内部方法(取决于范围/封装).换句话说,我无法访问私有方法.

我已经读过,虽然最小的静态方法比非静态方法稍微有效.

因此,在创建返回类型为void的私有方法时,并在从内部创建对象的引用时排除,为什么不将它设置为静态?我见过的所有代码都没有这样做,所以我只能假设我错过了这一点.

.net static

7
推荐指数
2
解决办法
1144
查看次数

无法在资源字典中绑定

我正在运行一个简单的MVVM项目并在第一个障碍中堕落.我使用Josh Smiths Relay Command方法绑定我的命令.

问题是,当按钮在ResourceDictionary中时,按钮不绑定.如果我将代码(完全按原样)移动到我的MainWindow.xaml中,则代码将根据需要执行.

这是我的MainWindow.xaml

<Window x:Class="ForJon.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:ForJon.Ui.ViewModels"
    Title="MainWindow" Height="350" Width="525">
   <Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="160" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

   <Grid.Resources>
        <ResourceDictionary Source="Resources\ResourceDictionary.xaml" />
    </Grid.Resources>

    <Grid.DataContext>
        <vm:MainWindowViewModel />
    </Grid.DataContext>

    <HeaderedContentControl
        Header="Options"
        Style="{StaticResource LeftMenu}" 
        Grid.Column="0"
        / >

    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

和资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:view="clr-namespace:ForJon.Ui.Views"
                    xmlns:viewModel="clr-namespace:ForJon.Ui.ViewModels"
                    >

    <Style x:Key="LeftMenu" TargetType="HeaderedContentControl">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                        <StackPanel>
                            <Button Content="Add" Command="{Binding AddCommand}" />
                        </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Width" Value="160"/>
    </Style>

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

我只能假设在ResourceDictionary中绑定它无法找到ViewModel(虽然我不知道为什么我认为).我认为它试图将额外的水平降低......

无论如何,有人可以解释为什么它不是从资源字典中执行的.

c# wpf mvvm

7
推荐指数
1
解决办法
6740
查看次数

MVVM - 如果选中复选框,则绑定到复选框

我想将字符串值绑定到文本框,但仅在选中复选框时.因此,如果选中该复选框,我希望文本框显示消息1,如果没有,则显示消息2.

做这个的最好方式是什么?是否更好在我的对象中使用List属性,然后取决于是否选中复选框取决于我的List <>中的哪个项目显示

要么

在选中复选框后再更新对象的属性(这次是字符串类型)然后重新绑定会更好吗?

data-binding mvvm

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

从继承的类中覆盖虚方法,但保留基类的注释

可能重复:
C#的注释继承(实际上是任何语言)

在我的应用程序中,我的基类清楚地定义了每个虚方法的目的.

当我继承我的基类并覆盖虚方法时,我不得不重新输入注释.

现在,我知道为什么它不会默认复制注释,这是有道理的但是因为我有许多继承自我的基类的类,所以复制整个XML注释以添加注释<para>底部的附加内容变得很烦人.

这只是它的方式吗?

c# comments

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

绑定到后面的代码中的relativesource

在我的UserControl中,我在XAML中有以下代码

<TextBlock Grid.Row="2" Text="{Binding Path=StartTime,
                               RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorLevel=1, AncestorType=Window}}" />
Run Code Online (Sandbox Code Playgroud)

这只是从父窗口获取属性的值,它工作得很好.

我怎么能在后面的代码中执行此操作?

Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
                                      typeof(Window), 1);
b.Path = "StartTime";

myProperty = b.value;// obviously there is no b.value but this
                     // is what I'm trying to achieve.

//BindingOperations.SetBinding(StartTime, StartTimeProperty, b);
//This does not work sadly, it can't convert string to DependancyObject
Run Code Online (Sandbox Code Playgroud)

c# wpf binding

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

工作流的设计模式

我不认为这是重复但我确实需要一些明智的方法来确认我的问题.

我的模式也将类似(尽管更复杂)下面的图片(我的应用程序从上到下开始).

它需要一个复杂的对象作为构造函数的一部分,然后通过各种进程(保存在不同的.dll中).

我以前没有接受过这样的项目,并且想要做对 - 我知道设计模式旨在帮助和提供指导.

在此输入图像描述

我的问题是,哪些模式可行.我坚持使用.NET 2.0.我的研究表明序列模式.

那么,我是否只限于序列模式或者是否还有其他建议?

c# workflow

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

什么是懒惰的实例化

懒惰的实例化是关于使用更少的代码但得到相同的结果?当然,这通常是一件好事(提供简短/高效的代码不会损害可读性/可维护性).

请参考这个懒惰的实例:

public sealed class Singleton
{
    private Singleton()
    {
    }

    public static Singleton Instance { get { return Nested.instance; } }

    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
} 
Run Code Online (Sandbox Code Playgroud)

Instance(我知道它是隐含的)没有私有财产- 它是否使它变得懒惰 - 事实上我们在public static Singleton Instance财产中没有设置者?

c# lazy-initialization

5
推荐指数
3
解决办法
4069
查看次数

委托 - 委托返回类型是否必须与它委派的方法相匹配?

我除了这似乎是一个显而易见的问题,但委托返回类型是否必须匹配它委托的方法的返回类型?

EG,像这样:

public static void Save()
    {
        TS ts = new TS(SaveToDatabase);
    }

    public delegate void TS();

    private static void SaveToDatabase()
    { }
Run Code Online (Sandbox Code Playgroud)

这将永远不会奏效

public static void Save()
    {
        TS ts = new TS(SaveToDatabase);
    }

    public delegate string TS();

    private static void SaveToDatabase()
    { }
Run Code Online (Sandbox Code Playgroud)

c# delegates

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

ReSharper - 如何恢复到Visual Studio代码模板

在Visual Studio中创建属性时,我使用片段prop,可以TAB在类型和名称之间,Visual Studio在2之间跳转.

ReSharper已经接管了这个功能,我想知道如何将它重置回Visual Studio.我遇到的问题是我不知道在哪里看.在Visual Studio 2012中,我可以看到ReSharper菜单,但有很多选项我不知道在哪里查看.

resharper code-templates code-snippets visual-studio-2012

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