Visual C#从事件处理程序返回值 [英] Visual C# returning value from an event handler
问题描述
表单上的代码:
public delegate void CustomPreviewCreate();
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if(CustomPreviewCreate_Do!= null)
{
CustomPreviewCreate_Do();
}
}
此事件在另一个类中被处理。我想要实现的是,如果方法正确执行,我可以反馈到表单的某种形式的返回值。
我到目前为止没有找到我结果
这是代码:
public void Initialize ()
{
SubAsstViewPartControl.CustomPreviewCreate_Do + = SubAsstViewPartControl_CustomPreviewCreate_Do;
//这给我一个编译器错误,返回类型是错误的
}
私有bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
//做东西
返回false;
}
有没有直接的方式从事件处理程序返回值,或者我需要使用单独的静态字段来存储事件结果?
更新:
Per @ Jon的评论,对我来说似乎是最简单的,我修改了如下代码:
public delegate bool CustomPreviewCreate() ;
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if(CustomPreviewCreate_Do!= null)
{
bool returnval = CustomPreviewCreate_Do();
}
}
然后:
bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
// do stuff
return true;
}
如果这个可以接受的方法,当我想要返回布尔? / p>
常见的方法是将您的值封装在 EventArgs
你的活动期待。例如,框架的 CancelEventArgs
包含可设置的 bool取消
属性,允许每个 CancelEventHandler
分配一个值。发件人可以在调用事件后读取该属性。如果要从各个事件处理程序中收集单独的值,也可以使用类似容器的
EventArgs
类。例如:
using System;
使用System.Collections.Generic;
命名空间ConsoleApplication1
{
public class SingleValueEventArgs:EventArgs
{
public int Value {get;组; }
}
public class MultiValueEventArgs:EventArgs
{
private List< int> _values = new List< int>(); //私人防止处理程序混淆对方的价值
public IEnumerable< int>值
{
get {return _values; }
}
public void AddValue(int value){_values.Add(value); }
}
public class Exposer
{
public event EventHandler< SingleValueEventArgs> WantSingleValue;
public event EventHandler< MultiValueEventArgs> WantMultipleValues;
public void Run()
{
if(WantSingleValue!= null)
{
var args = new SingleValueEventArgs();
WantSingleValue(this,args);
Console.WriteLine(Last handler generated+ args.Value.ToString());
}
if(WantMultipleValues!= null)
{
var args = new MultiValueEventArgs();
WantMultipleValues(this,args);
foreach(argsValues中的var值)
{
Console.WriteLine(处理程序生成+ value.ToString());
}
}
}
}
public class Handler
{
private int _value;
public Handler(Exposer曝光器,int值)
{
_value = value;
exposureer.WantSingleValue + = exposeer_WantSingleValue;
exposureer.WantMultipleValues + = exposeer_WantMultipleValues;
}
void exposeder_WantSingleValue(object sender,SingleValueEventArgs e)
{
Console.WriteLine(Handler assign+ _value.ToString());
e.Value = _value;
}
void exposureer_WantMultipleValues(object sender,MultiValueEventArgs e)
{
Console.WriteLine(Handler added+ _value.ToString());
e.AddValue(_value);
}
}
类程序
{
static void Main(string [] args)
{
var exposeder =新的Exposer(); (var i = 0; i< 5; i ++)
{
new Handler(曝光者,i);
}
exposureer.Run();
}
}
}
I have a form that has a button to get a method executed in another class.
Code on the form:
public delegate void CustomPreviewCreate();
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if (CustomPreviewCreate_Do !=null)
{
CustomPreviewCreate_Do();
}
}
This event then gets handled in another class. What I would like to achieve is that I can feed back to the form some form of return value if the method correctly executed.
What I tried so far does not get me the result.
Here is the code:
public void Initialize()
{
SubAsstViewPartControl.CustomPreviewCreate_Do += SubAsstViewPartControl_CustomPreviewCreate_Do;
// this gives me a the compiler error that the return type is wrong
}
private bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
// do stuff
return false;
}
Is there any direct way to return value from an event handler or I need to use a separate static field to store the event result in?
Update:
Per @Jon's comment, which seemed the simplest to me, I modified the code as follows:
public delegate bool CustomPreviewCreate();
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if (CustomPreviewCreate_Do !=null)
{
bool returnval = CustomPreviewCreate_Do();
}
}
And then:
bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
// do stuff
return true;
}
Advice please if this an acceptable approach when I want to just return booleans?
The common approach is to encapsulate your value in the type of EventArgs
your event expects. For example, the Framework's CancelEventArgs
contains a settable bool Cancel
property, allowing each CancelEventHandler
to assign a value. The sender can then read the property after the event has been invoked. You could also use a container-like EventArgs
class if you want to collect separate values from individual event handlers. For example:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class SingleValueEventArgs : EventArgs
{
public int Value { get; set; }
}
public class MultiValueEventArgs : EventArgs
{
private List<int> _values = new List<int>(); // Private to prevent handlers from messing with each others' values
public IEnumerable<int> Values
{
get { return _values; }
}
public void AddValue(int value) { _values.Add(value); }
}
public class Exposer
{
public event EventHandler<SingleValueEventArgs> WantSingleValue;
public event EventHandler<MultiValueEventArgs> WantMultipleValues;
public void Run()
{
if (WantSingleValue != null)
{
var args = new SingleValueEventArgs();
WantSingleValue(this, args);
Console.WriteLine("Last handler produced " + args.Value.ToString());
}
if (WantMultipleValues != null)
{
var args = new MultiValueEventArgs();
WantMultipleValues(this, args);
foreach (var value in args.Values)
{
Console.WriteLine("A handler produced " + value.ToString());
}
}
}
}
public class Handler
{
private int _value;
public Handler(Exposer exposer, int value)
{
_value = value;
exposer.WantSingleValue += exposer_WantSingleValue;
exposer.WantMultipleValues += exposer_WantMultipleValues;
}
void exposer_WantSingleValue(object sender, SingleValueEventArgs e)
{
Console.WriteLine("Handler assigning " + _value.ToString());
e.Value = _value;
}
void exposer_WantMultipleValues(object sender, MultiValueEventArgs e)
{
Console.WriteLine("Handler adding " + _value.ToString());
e.AddValue(_value);
}
}
class Program
{
static void Main(string[] args)
{
var exposer = new Exposer();
for (var i = 0; i < 5; i++)
{
new Handler(exposer, i);
}
exposer.Run();
}
}
}
这篇关于Visual C#从事件处理程序返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!