简体   繁体   English

将java转换为c#的问题

[英]Issues with converting java to c#

I'm attempting to convert the code located at How to use signalr in android Service from java to c# and have been making some progress. 我正在尝试将位于如何使用android服务中的signalr的代码从java转换为c#,并且已经取得了一些进展。 I'm now stuck at the final method. 我现在坚持最后的方法。 The java code is: 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);
}

Using a java to c# converter, this translated to: 使用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);
        }
    }

This, of course, generated several errors in Visual Studio 2017. 当然,这在Visual Studio 2017中产生了一些错误。

First, the line Platform.loadPlatformComponent(new AndroidPlatformComponent()); 一,行Platform.loadPlatformComponent(new AndroidPlatformComponent()); generated the error Platform is inaccessible due to its protection level . 生成错误Platform is inaccessible due to its protection level Platform in Xamarin for Visual Studio 2017 is indeed protected and is a internal class in System and I cannot change this, so I'm at a loss as how to proceed with it. Platform在Xamarin为Visual Studio 2017年确实是受保护的,是在内部类System ,我无法改变这一点,所以我在一个损失如何与它进行。 The same line generates the error The type or namespace name 'AndroidPlatformComponent' could not be found , these errors a numerous and not unexpected I just can't find an equivalent to AndroidPlatformComponent in Visual Studio 2017 so I'm at a loss as how to solve this one. 同一行生成错误The type or namespace name 'AndroidPlatformComponent' could not be found ,这些错误很多并且不出意外我在Visual Studio 2017中找不到相当于AndroidPlatformComponent所以我不知道如何解决这个问题。

Next, on this line ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.Logger); 接下来,在此行ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.Logger); generates the error The type or namespace name 'ClientTransport' could not be found , I was also unable to find an equivalent to this and again I'm at a loss as to proceed. 生成错误无法找到The type or namespace name 'ClientTransport' could not be found ,我也无法找到相应的这个,并且我再次无法继续。 Also on this line, .Logger is not defined for the hub connection, apparently it's .getLogger() in java, I was unable to find an equivalent for this one as well. 同样在这一行上,没有为集线器连接定义.Logger ,显然它是java中的.getLogger() ,我也无法找到这个的等价物。

Next the line SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);' generates the error 1The type or namespace name 'SignalRFuture<>' could not be found 接下来是SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);' generates the error 1The type or namespace name 'SignalRFuture<>' could not be found SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);' generates the error 1The type or namespace name 'SignalRFuture<>' could not be found , this seemes to be specific to SignalR, again, I am unable to find an equivalent. SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.Start(clientTransport);' generates the error 1The type or namespace name 'SignalRFuture<>' could not be found ,这个看起来特定于SignalR,再次,我无法找到等价物。

The next one has me totally stumped, the line private class SubscriptionHandler2AnonymousInnerClass : SubscriptionHandler2<string, LoginInfo> generates the error The type or namespace name 'SubscriptionHandler2<,>' could not be found . 下一个让我完全难过,行private class SubscriptionHandler2AnonymousInnerClass : SubscriptionHandler2<string, LoginInfo>生成错误The type or namespace name 'SubscriptionHandler2<,>' could not be found I've looked everywhere online and read up on AnonymousInnerClass, but it was not help with this. 我在网上到处查看并阅读AnonymousInnerClass,但这对此没有帮助。

I'm hoping that the users here are more familiar with SignalR and the differences between c# functionality and java functionality. 我希望这里的用户更熟悉SignalR以及c#功能和java功能之间的区别。 I'm not at all familiar with java nor am I familiar with SignalR and foreground services. 我对java并不熟悉,也不熟悉SignalR和前台服务。

As it turns out, the last method in the java code I was converting was wiring up an event to pass the message received from the hub to the activity. 事实证明,我转换的java代码中的最后一个方法是连接一个事件,将从集线器收到的消息传递给活动。 In c# / Visual Studio (2017), that's done very differently which is why I didn't understand/recognize what was going on. 在c#/ Visual Studio(2017)中,这完全不同,这就是为什么我不理解/不知道发生了什么。 So I created a handler in C# and execute a popup message for the message. 所以我在C#中创建了一个处理程序并为该消息执行弹出消息。 This in itself may pose problems, but at least I know what's going on. 这本身可能会带来问题,但至少我知道发生了什么。 This is the code I wrote to start SignalR from within the service and WireUp the handler: 这是我写的从服务中启动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)
        }
    }

This is the WireUp code, this is a method in the client code: 这是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));
        });
    }

As I had anticipated, the popup message won't appear when the app is in the background, so I'm researching a workaround 正如我所料,当应用程序在后台时,弹出消息不会出现,所以我正在研究一种解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM