标签: eventargs

.NET是否有内置的EventArgs <T>?

我正准备为带有单个参数的事件args创建一个通用的EventArgs类:

public class EventArg<T> : EventArgs
{
    // Property variable
    private readonly T p_EventData;

    // Constructor
    public EventArg(T data)
    {
        p_EventData = data;
    }

    // Property for EventArgs argument
    public T Data
    {
        get { return p_EventData; }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我这样做之前,C#是否具有内置于该语言的相同功能?我似乎记得在C#2.0发布时遇到类似的东西,但现在我找不到它.

换句话说,我是否必须创建自己的通用EventArgs类,或者C#是否提供了一个?谢谢你的帮助.

c# generics eventargs

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

为什么使用EventArgs.Empty而不是null?

我记得在多次和多个地点阅读时,在解雇典型事件时:

protected virtual OnSomethingHappened()
{
    this.SomethingHappened(this, EventArgs.Empty);
}
Run Code Online (Sandbox Code Playgroud)

如果没有有趣的事件参数,则应该是EventArgs.Empty,而不是null.

我遵循了我的代码中的指导,但我意识到我不清楚为什么这是首选技术.为什么声明的合约更喜欢EventArgs.Empty而不是null?

c# events eventargs

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

MVVM将EventArgs作为命令参数传递

我正在使用Microsoft Expression Blend 4
我有一个浏览器..,

[XAML] ConnectionView"后面的空代码"

        <WebBrowser local:AttachedProperties.BrowserSource="{Binding Source}">
            <i:Interaction.Triggers>
                <i:EventTrigger>
                    <i:InvokeCommandAction Command="{Binding LoadedEvent}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Navigated">
                    <i:InvokeCommandAction Command="{Binding NavigatedEvent}" CommandParameter="??????"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </WebBrowser>  
Run Code Online (Sandbox Code Playgroud)

[C#] AttachedProperties类

public static class AttachedProperties
    {
        public static readonly DependencyProperty BrowserSourceProperty = DependencyProperty . RegisterAttached ( "BrowserSource" , typeof ( string ) , typeof ( AttachedProperties ) , new UIPropertyMetadata ( null , BrowserSourcePropertyChanged ) );

        public static string GetBrowserSource ( DependencyObject _DependencyObject )
        {
            return ( string ) _DependencyObject . GetValue ( BrowserSourceProperty …
Run Code Online (Sandbox Code Playgroud)

browser wpf eventargs expression-blend

60
推荐指数
6
解决办法
9万
查看次数

如何使事件处理程序异步运行?

我正在编写一个Visual C#程序,它在辅助线程上执行连续的操作循环.偶尔当该线程完成任务时,我希望它触发一个事件处理程序.我的程序执行此操作但触发事件处理程序时,辅助线程等待事件处理程序完成后再继续执行该线程.如何让它继续?这是我目前的结构方式......

class TestClass 
{
  private Thread SecondaryThread;
  public event EventHandler OperationFinished;

  public void StartMethod()
  {
    ...
    SecondaryThread.Start();      //start the secondary thread
  }

  private void SecondaryThreadMethod()
  {
    ...
    OperationFinished(null, new EventArgs());
    ...  //This is where the program waits for whatever operations take
         //place when OperationFinished is triggered.
  }

}
Run Code Online (Sandbox Code Playgroud)

此代码是我的某个设备的API的一部分.当触发OperationFinished事件时,我希望客户端应用程序能够执行它所需的任何操作(即相应地更新GUI),而不会导致API操作.

另外,如果我不想将任何参数传递给事件处理程序,我的语法是否正确使用OperationFinished(null, new EventArgs())

c# multithreading event-handling synchronous eventargs

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

asp.net:如何在按钮onclick上使用eventargs进行参数传递?

我见过一些代码,人们通过web控件的commandParameter属性传递参数.然后使用eventargs是什么.

asp.net parameters button eventargs

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

为什么不使用自定义类而不是继承EventArgs类

我想知道为什么我应该使用从类继承的EventArgs类而不是使用在传递事件数据时为我做同样工作的自定义类?

.net c# event-handling eventargs

21
推荐指数
3
解决办法
2370
查看次数

现在需要EventArg类,我们有泛型

使用泛型,是否有理由创建特定的派生EventArg类

现在看起来你可以通过通用实现轻松地使用它们.

我应该去看看我的所有示例并删除我的eventArg类(StringEventArgs,MyFooEventArgs等).

public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        m_value = value;
    }

    private T m_value;

    public T Value
    {
        get { return m_value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# generics eventargs

19
推荐指数
1
解决办法
9500
查看次数

为什么CompositionTarget.Rendering使用EventArgs而不是RenderingEventArgs?

CompositionTarget.Rendering事件是一个普通的旧事件处理程序,以一个普通的旧的EventArgs.但是,在现实生活中,它显然总是获得RenderingEventArgs的实例.因此,您的事件处理程序必须从转换EventArgs开始,以便从中获取有用的信息.

为什么不是类型的事件EventHandler<RenderingEventArgs>,所以我们可以更容易地得到参数(更重要的是,我们甚至可以知道参数在那里)为什么微软会选择给这个事件带来错误的签名?

我想知道向后兼容性 - 有没有RenderingEventArgs尚未存在的版本? - 但似乎并非如此.根据MSDN,RenderingEventArgs和CompositionTarget在两个平台上的同一版本中引入 - 在WPF中,两者都在.NET 3.0中添加; 在Silverlight中,两者都添加在Silverlight 3.0中.

如果它提供了任何类型的提示,我遇到了一个旧的讨论主题,其中有人说,"代表正在使用EventArgs,因为通过这样做,在编组方面取得了某种性能上的胜利." 如果有人可以解释可能会取得什么样的表现,我愿意接受这一点作为答案.

silverlight wpf eventargs

17
推荐指数
1
解决办法
1012
查看次数

创建一个没有标准(Obj sender,EventArgs args)签名的事件处理程序委托有多么错误?

我理解使用标准MS事件处理程序委托签名的好处,因为它允许您轻松扩展通过事件传递的信息,而不会破坏任何基于旧委托签名的旧关系.

我想知道在实践中人们经常遵循这条规则吗?说我有一个像这样的简单事件

public event NameChangedHandler NameChanged;
public delegate void NameChangedHandler(Object sender, string oldName, string newName);
Run Code Online (Sandbox Code Playgroud)

这是一个简单的事件,我几乎肯定我从NameChanged事件中需要知道的唯一参数是名称已更改的对象,旧名称和新名称.那么创建一个单独的NameChangedEventArgs类是否值得,或者像这样的简单事件是否可以直接通过委托参数返回参数?

c# events eventargs

13
推荐指数
2
解决办法
4817
查看次数

如果没有参数的Event定义了自己的自定义EventArgs,或者只是使用System.EventArgs?

我有一个当前定义没有事件参数的事件.也就是说,它发送的EventArgs是EventArgs.Empty.

在这种情况下,最简单的方法是将我的事件处理程序声明为:

EventHandler<System.EventArgs> MyCustomEvent;
Run Code Online (Sandbox Code Playgroud)

我不打算在此事件中添加任何事件参数,但是将来可能需要更改任何代码.

因此,我倾向于让我的所有事件始终创建一个空事件args类型System.EventArgs,即使当前不需要事件args也是如此.像这样的东西:

public class MyCustomEventArgs : EventArgs
{
}
Run Code Online (Sandbox Code Playgroud)

然后我的事件定义变为以下内容:

EventHandler<MyCustomEventArgs> MyCustomEvent;
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:定义自己的更好MyCustomEventArgs,即使它没有添加任何东西,除了继承System.EventArgs,以便将来可以更容易地添加事件参数?或者将事件明确定义为返回更好System.EventArgs,以便用户更清楚没有额外的事件参数?

我倾向于为我的所有事件创建自定义事件参数,即使事件参数为空.但我想知道是否有人认为让事件参数为空的用户更清楚会更好吗?

非常感谢提前,

麦克风

.net c# events eventargs

12
推荐指数
2
解决办法
2811
查看次数