我正在尝试将位于How to use signalr in android Service的代码从Java转换为c#,并且已经取得了一些进展。我现在停留在最后一种方法上。 Java代码是:
private void startSignalR() {
Platform.loadPlatformComponent(new AndroidPlatformComponent());
mInstance.setmHubConnection();
mInstance.setHubProxy();
ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
Log.e("SimpleSignalR", e.toString());
return;
}
mInstance.sendMessage(MainActivity.unm,"Hello All!");
String CLIENT_METHOD_BROADAST_MESSAGE = "recievedMessage";
mInstance.mHubProxy.on(CLIENT_METHOD_BROADAST_MESSAGE,
new SubscriptionHandler2<String,LoginInfo>() {
@Override
public void run(final String msg,final LoginInfo loginInfo) {
final String finalMsg = loginInfo.FullName + " says " + loginInfo.Password;
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", finalMsg);
sendBroadcast(intent);
}
}
, String.class,LoginInfo.class);
}
使用Java到C#转换器,将其转换为:
private void startSignalR()
{
Platform.loadPlatformComponent(new AndroidPlatformComponent());
mInstance.setmHubConnection();
mInstance.setHubProxy();
ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.Logger);
SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);
try
{
signalRFuture.get();
}
catch (Exception e) when (e is InterruptedException || e is ExecutionException)
{
// Log.e("SimpleSignalR", e.ToString());
return;
}
mInstance.sendMessage("", "Hello All!");
string CLIENT_METHOD_BROADAST_MESSAGE = "recievedMessage";
//String CLIENT_METHOD_BROADAST_MESSAGE = "messageReceived";
mInstance.mHubProxy.on(CLIENT_METHOD_BROADAST_MESSAGE, new SubscriptionHandler2AnonymousInnerClass(this)
, typeof(string), typeof(LoginInfo));
}
private class SubscriptionHandler2AnonymousInnerClass : SubscriptionHandler2<string, LoginInfo>
{
private readonly SignalRSrv outerInstance;
public SubscriptionHandler2AnonymousInnerClass(SignalRSrv outerInstance)
{
this.outerInstance = outerInstance;
}
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public void run(final String msg,final LoginInfo loginInfo)
public override void run(string msg, LoginInfo loginInfo)
{
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String finalMsg = loginInfo.FullName + " says " + loginInfo.Password;
string finalMsg = loginInfo.FullName + " says " + loginInfo.Password;
Intent intent = new Intent();
intent.Action = MY_ACTION;
intent.PutExtra("DATAPASSED", finalMsg);
sendBroadcast(intent);
}
}
这当然会在Visual Studio 2017中产生一些错误。
首先,第Platform.loadPlatformComponent(new AndroidPlatformComponent());
行生成了错误Platform is inaccessible due to its protection level
。 Xamarin for Visual Studio 2017中的Platform
确实受到保护,并且是System
中的内部类,我无法更改它,因此我对如何进行操作感到困惑。同一行会生成错误The type or namespace name 'AndroidPlatformComponent' could not be found
,这些错误很多且并非意外,我在Visual Studio 2017中找不到与AndroidPlatformComponent
等效的错误,所以我无所适从。
接下来,在此行ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.Logger);
上产生错误The type or namespace name 'ClientTransport' could not be found
,我也找不到与此对应的错误,因此我不知所措。同样在这一行上,没有为集线器连接定义.Logger
,显然在Java中它是.getLogger()
,我也找不到与之等效的连接。
下一行SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);' generates the error 1The type or namespace name 'SignalRFuture<>' could not be found
,这似乎是SignalR特有的,同样,我也找不到对应的内容。
下一个完全让我感到困惑,行private class SubscriptionHandler2AnonymousInnerClass : SubscriptionHandler2<string, LoginInfo>
产生错误The type or namespace name 'SubscriptionHandler2<,>' could not be found
。我在网上到处都看过,并在AnonymousInnerClass上阅读,但这没有帮助。
我希望这里的用户对SignalR以及c#功能和java功能之间的区别更加熟悉。我一点也不熟悉Java,也不熟悉SignalR和前台服务。
答案 0 :(得分:0)
事实证明,我正在转换的Java代码中的最后一个方法是连接一个事件,以将从集线器接收的消息传递给活动。在c#/ Visual Studio(2017)中,这做的非常不同,这就是为什么我不了解/无法识别发生了什么的原因。因此,我在C#中创建了一个处理程序,并为该消息执行了一条弹出消息。这本身可能会带来问题,但至少我知道发生了什么。这是我编写的用于从服务内部启动SignalR和WireUp处理程序的代码:
private void startSignalR()
{
// Company, Department, and section are private variables
// their values are pulled from the intent bundle
// in the OnBind method, that code is:
// Bundle bundlee = intent.GetBundleExtra("TheBundle");
// MyUser = bundlee.GetParcelable("MyUser") as User;
// This information is put into the bundle when the user is logged in.
// I then pass that information to the SignalR client
// when I set the hub connection and placed on the querystring to the hub.
mInstance.setmHubConnection(username, firstname,lastname,company,department,section);
mInstance.setHubProxy();
try
{
// Connect the client to the hup
mInstance.mHubConnection.Start();
// Set the event handler
mInstance.WireUp();
}
catch (System.Exception e) when (e is InterruptedException || e is ExecutionException)
{
ShowMessage("Error: + e.Message)
}
}
这是WireUp
代码,这是客户端代码中的方法:
public void WireUp()
{
// set the event handler
mHubProxy.On("broadcastMessage", (string platform, string message) =>
{
if (OnMessageReceived != null)
OnMessageReceived(this, string.Format("{0}: {1}", platform, message));
});
}
如我所料,当应用程序在后台运行时,不会显示弹出消息,因此我正在研究一种解决方法