gpt4 book ai didi

java - 将java转换为c#的问题

转载 作者:行者123 更新时间:2023-12-02 04:42:19 25 4
gpt4 key购买 nike

我正在尝试转换位于 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 levelPlatform在 Xamarin for Visual Studio 2017 中确实受到保护,并且是 System 中的内部类我无法改变这一点,所以我不知道如何继续下去。同一行生成错误 The type or namespace name 'AndroidPlatformComponent' could not be found ,这些错误数量众多,并不意外,我只是找不到相当于 AndroidPlatformComponent 的错误。在 Visual Studio 2017 中,所以我不知道如何解决这个问题。

接下来,在这一行 ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.Logger);生成错误 The type or namespace name 'ClientTransport' could not be found ,我也无法找到与此等效的内容,并且我再次不知所措。同样在这条线上,.Logger没有为集线器连接定义,显然它是 .getLogger()在java中,我也无法找到与此相对应的东西。

下一行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和前台服务。

最佳答案

事实证明,我转换的 java 代码中的最后一个方法是连接一个事件,将从集线器接收到的消息传递给 Activity 。在 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));
});
}

正如我预料的那样,当应用程序在后台运行时,不会出现弹出消息,因此我正在研究解决方法

关于java - 将java转换为c#的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56509309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com