我试图将lambda函数转换为java并且我没有成功。我拥有的C#代码是
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DelegateEventsWindowsFormsApp
{
public partial class Form1 : Form
{
public delegate void RecordUpdated(int iRecordId, EventArgs e);
public event RecordUpdated recordUpdated;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.recordUpdated += (iRecord, Arg) =>
{
MessageBox.Show(iRecord.ToString());
};
}
private void button1_Click(object sender, EventArgs e)
{
if (recordUpdated != null)
recordUpdated(100, null);
}
}
}
我怎么能把它转换成java?预期的行为是表单1有一个按钮,当我点击按钮时,如果recordUpdated
已订阅,那么我显示100.如果它为null,则recordUpdated
更新为100。
我已经完成了以下java代码,
由于存在异步调用,我做了一些事情,
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private String EventrecordUpdated;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.EventrecordUpdated = null;
EventServiceClass obj = new EventServiceClass();
Toast.makeText(getApplicationContext(), "Calling Service",
Toast.LENGTH_LONG).show();
obj.testService("TEST");
Toast.makeText(getApplicationContext(), "DOne Service",
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main); // here we initialize the
// component. ( our UI)
}
public void delegateRecordUpdated(String iRecordId, String e) {
Toast.makeText(getApplicationContext(), iRecordId, Toast.LENGTH_LONG)
.show();
}
public void EventrecordUpdate(String iRecordId, String e) {
this.EventrecordUpdated = iRecordId;
Toast.makeText(getApplicationContext(), "Subscribed to the Event",
Toast.LENGTH_LONG).show();
}
public void button1_Click(View view) { // this is the
setContentView(R.layout.activity_main);
new LongOperation().execute("123");
/*
* if (EventrecordUpdated != null) { delegateRecordUpdated("100", null);
* } else { EventrecordUpdate("100", null); }
*/
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
System.out.println(params);
return "Executed";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "Executed",
Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
答案 0 :(得分:0)
在Java中执行此操作的方法如下(使用包含的帮助程序类&#39; EventSimulation&#39;) - 请注意,您需要在Java中选择自己的首选项,以便&#39; Form&#39;以及相应的Form_Load和button1_Click,但这将显示与C#事件等效的通用Java:
package DelegateEventsWindowsFormsApp;
public class Form1 extends Form //you need your own 'Form' equivalent
{
@FunctionalInterface
public interface RecordUpdated
{
void invoke(int iRecordId);
}
public EventSimulation<RecordUpdated> recordUpdated = new EventSimulation<RecordUpdated>();
private void Form1_Load()
{
this.recordUpdated.addListener((iRecord) ->
{
//substitute your own Java preference for the message box:
//javax.swing.JOptionPane.showConfirmDialog(null, iRecord.toString());
});
}
private void button1_Click()
{
if (recordUpdated != null)
{
for (RecordUpdated listener : recordUpdated.anonymousListeners)
{
listener.invoke(100);
}
//uncomment the following if you also subscribe to the event using method names:
//for (RecordUpdated listener : recordUpdated.namedListeners.values())
//{
// listener.invoke(100);
//}
}
}
}
public final class EventSimulation<T>
{
public java.util.List<T> anonymousListeners = new java.util.ArrayList<T>();
public void addListener(T unnamedEventHandlerMethod)
{
anonymousListeners.add(unnamedEventHandlerMethod);
}
//you don't need the following for your example, but this would be required for the
//Java equivalent to the more common C# event subscribing/unsubscribing via method names:
public java.util.Map<String, T> namedListeners = new java.util.HashMap<String, T>();
public void addListener(String methodName, T namedEventHandlerMethod)
{
if (!namedListeners.containsKey(methodName))
namedListeners.put(methodName, namedEventHandlerMethod);
}
public void removeListener(String methodName)
{
if (namedListeners.containsKey(methodName))
namedListeners.remove(methodName);
}
}