C# 如何将方法转换为事件?
如何使此方法成为事件 BarcodeScannerRenderer.cs:C# 如何将方法转换为事件?,c#,xamarin.forms,barcode-scanner,manateeworks,C#,Xamarin.forms,Barcode Scanner,Manateeworks,如何使此方法成为事件 BarcodeScannerRenderer.cs: void IScanSuccessCallback.barcodeDetected(MWResult result) { if (result != null) { try { var scan = Element as BarcodeScannerModal; if (scan == null) return;
void IScanSuccessCallback.barcodeDetected(MWResult result)
{
if (result != null)
{
try
{
var scan = Element as BarcodeScannerModal;
if (scan == null)
return;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
并将结果的值传递到另一个类中,具体如下所示:
(BarcodeScanner.cs)
public异步任务GetResult()
{
TaskCompletionSource tcs=新的TaskCompletionSource();
scanPage.OnScanResult+=异步(结果)=>
{
object[]scanResult=新对象[2];
SharedAppSettings.Sounds.PlayBeep();
scanResult[0]=resultFinal.text;
scanResult[1]=resultFinal.typeText;
等待PopupNavigation.PopAsync();
tcs.SetResult(扫描结果);
};
返回等待任务;
}
如果你想知道我用的是哪种条形码扫描器,<强>它的海牛工作条形码扫描器。< /强>
这个答案可能必须适应这个问题的变化,所以不要认为它是完整的: 要举办一个活动,您可以这样做:// Given you have a custom EventArgs class ...
// Define an event on which clients can register their handlers
public event EventHandler<BCDetectedEventArgs> BarcodeDetected;
// infoObject should probably be of the type what `scan` is.
protected virtual void OnBarcodeDetected( object infoObject )
{
// Check if there is at least one handler registered
var handler = BarcodeDetected;
if( handler != null )
{
handler(this, new BCDetectedEventArgs(infoObject));
}
}
void IScanSuccessCallback.barcodeDetected(MWResult result)
{
if (result != null)
{
try
{
var scan = Element as BarcodeScannerModal;
if (scan == null)
return;
else
OnBarcodeDetected( scan );
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
//如果您有一个自定义的EventArgs类。。。
//定义客户端可以注册其处理程序的事件
检测到公共事件EventHandler条码;
//infoObject的类型可能与'scan'的类型相同。
检测到受保护的虚拟无效OnBarCode(对象infoObject)
{
//检查是否至少注册了一个处理程序
var handler=检测到条码;
if(处理程序!=null)
{
处理程序(这是新的BCDetectedEventArgs(infoObject));
}
}
void IScanSuccessCallback.barcodeDetected(MWResult结果)
{
如果(结果!=null)
{
尝试
{
var scan=元件为条形码扫描模式;
如果(扫描==null)
返回;
其他的
检测到OnBarcode(扫描);
}
捕获(例外情况除外)
{
系统.诊断.调试.写入线(例如消息);
}
}
}
另请参见以供参考
因为您的代码片段建议使用“轮询”设计,所以BarcodeScanner.cs中的部分有点棘手。首先,您必须从上面的代码片段注册到事件,并用适当的处理程序方法处理该事件。
scanPage.OnScanResult
这不是已经是一个事件了吗?还有void IScanSuccessCallback.barcodeDetected(MWResult result)
除了检查null之外,似乎对result
没有任何实际作用。这就是我请求帮助的原因,而不是仅仅为了说“这真的错了”。此外,如果知道这些事情,我不介意再问了。基本上这应该是这个网站的主要目标,我想对于那些新手来说,我是对的?我真的不知道为什么人们只是在没有给出答案的情况下进行向下投票,我想这对他们来说很简单,因为他们经历了很多这样的事情,但并不是一个思想开放的人,因为他们太自负。但是感谢@Fildor的回复,我真的很感激。啊,不要把向下投票当成个人的。您可以对照检查,看看是否可以改进您的问题以避免它们。事实上,我没有写答案,因为这个问题其实不那么容易回答。给你一个如何使第一个片段成为事件的例子是相当容易的。但是您的第二个代码片段显示您的设计是基于轮询结果的。这与任何基于事件的方法相矛盾。例如,最好知道什么是scanPage
,因为您似乎已经在上面注册了一个事件。同样在第一个代码片段中:Element
似乎是从天而降的。它从何而来?这就是所谓的“问题的答案”。谢谢。我真的很感激。你节省了我的时间。:-)
// Given you have a custom EventArgs class ...
// Define an event on which clients can register their handlers
public event EventHandler<BCDetectedEventArgs> BarcodeDetected;
// infoObject should probably be of the type what `scan` is.
protected virtual void OnBarcodeDetected( object infoObject )
{
// Check if there is at least one handler registered
var handler = BarcodeDetected;
if( handler != null )
{
handler(this, new BCDetectedEventArgs(infoObject));
}
}
void IScanSuccessCallback.barcodeDetected(MWResult result)
{
if (result != null)
{
try
{
var scan = Element as BarcodeScannerModal;
if (scan == null)
return;
else
OnBarcodeDetected( scan );
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}