我正在尝试将位于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和前台服务。
参考方案
事实证明,我正在转换的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));
});
}
如我所料,当应用程序在后台运行时,不会显示弹出消息,因此我正在研究一种解决方法
为什么我的应用在启动时总是崩溃-Android - java刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…
绑定Java库Xamarin.Android - java我花了两天时间在每个论坛,文档,tuto,博客等上寻找答案。我为实习生启动了一个Android应用程序,因为我不懂Java,所以用xamarin C#开发了它。直到最近一切都还不错,但现在我需要集成一个SDK才能在应用程序中使用POS(销售点),但是该库是用Java编写的,即使跟随文档或辅导老师,我也无法将其与xamarin绑定(我什至无法调试)。这里有人已…
不需要选择回收站查看项目 - javaviewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.dra…
Java Double与BigDecimal - java我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…
如何自定义Xamarin形式的滑块? - java我要自定义滑块,使其看起来如下图所示:我在.xaml文件中的代码:<Slider MaximumTrackColor="Black" MinimumTrackColor="Black" HeightRequest="50" ThumbColor="Black" ThumbI…