我正准备为带有单个参数的事件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#是否提供了一个?谢谢你的帮助.
我记得在多次和多个地点阅读时,在解雇典型事件时:
protected virtual OnSomethingHappened()
{
this.SomethingHappened(this, EventArgs.Empty);
}
Run Code Online (Sandbox Code Playgroud)
如果没有有趣的事件参数,则应该是EventArgs.Empty,而不是null.
我遵循了我的代码中的指导,但我意识到我不清楚为什么这是首选技术.为什么声明的合约更喜欢EventArgs.Empty而不是null?
我正在使用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) 我正在编写一个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())?
我见过一些代码,人们通过web控件的commandParameter属性传递参数.然后使用eventargs是什么.
我想知道为什么我应该使用从类继承的EventArgs类而不是使用在传递事件数据时为我做同样工作的自定义类?
使用泛型,是否有理由创建特定的派生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) 该CompositionTarget.Rendering事件是一个普通的旧事件处理程序,以一个普通的旧的EventArgs.但是,在现实生活中,它显然总是获得RenderingEventArgs的实例.因此,您的事件处理程序必须从转换EventArgs开始,以便从中获取有用的信息.
为什么不是类型的事件EventHandler<RenderingEventArgs>,所以我们可以更容易地得到参数(更重要的是,我们甚至可以知道参数在那里)?为什么微软会选择给这个事件带来错误的签名?
我想知道向后兼容性 - 有没有RenderingEventArgs尚未存在的版本? - 但似乎并非如此.根据MSDN,RenderingEventArgs和CompositionTarget在两个平台上的同一版本中引入 - 在WPF中,两者都在.NET 3.0中添加; 在Silverlight中,两者都添加在Silverlight 3.0中.
如果它提供了任何类型的提示,我遇到了一个旧的讨论主题,其中有人说,"代表正在使用EventArgs,因为通过这样做,在编组方面取得了某种性能上的胜利." 如果有人可以解释可能会取得什么样的表现,我愿意接受这一点作为答案.
我理解使用标准MS事件处理程序委托签名的好处,因为它允许您轻松扩展通过事件传递的信息,而不会破坏任何基于旧委托签名的旧关系.
我想知道在实践中人们经常遵循这条规则吗?说我有一个像这样的简单事件
public event NameChangedHandler NameChanged;
public delegate void NameChangedHandler(Object sender, string oldName, string newName);
Run Code Online (Sandbox Code Playgroud)
这是一个简单的事件,我几乎肯定我从NameChanged事件中需要知道的唯一参数是名称已更改的对象,旧名称和新名称.那么创建一个单独的NameChangedEventArgs类是否值得,或者像这样的简单事件是否可以直接通过委托参数返回参数?
我有一个当前定义没有事件参数的事件.也就是说,它发送的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,以便用户更清楚没有额外的事件参数?
我倾向于为我的所有事件创建自定义事件参数,即使事件参数为空.但我想知道是否有人认为让事件参数为空的用户更清楚会更好吗?
非常感谢提前,
麦克风
eventargs ×10
c# ×7
events ×3
.net ×2
generics ×2
wpf ×2
asp.net ×1
browser ×1
button ×1
parameters ×1
silverlight ×1
synchronous ×1