C#回调转换

时间:2013-07-30 01:09:56

标签: c# vb.net callback steam

我的C#比我预期的要生气勃勃,所以我正在寻找一些帮助让我的项目开始。我已经尝试了许多C#到VB的转换器,但它们都没有能够处理回调方面提供的SteamKit2示例和我的所有研究,试验WithEvents和其他尝试都失败了。我很抱歉,如果这超出了普通的stackoverflow问题的范围,但最重要的是我正在寻找一个工作的VB.net版本的C#SteamKit2示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SteamKit2;

//
// Sample 2: The Callback Manager
//
// this sample introduces the callback manager
// the callback manager is a class which can simplify the design of your program
//
// the callback manager's task is to modify the callback loop so that the handling of callbacks
// can be done in your own functions, rather than in-line with the loop
//
// in addition to the manager, your code must create an instance of a Callback<T> class
// this class routes a specific callback type to a function, and is bound only to a specific
// callback manager
//

namespace Sample2_CallbackManager
{
    class Program
    {
        static SteamClient steamClient;
        static CallbackManager manager;

        static SteamUser steamUser;

        static bool isRunning;

        static string user, pass;


        static void Main( string[] args )
        {
            if ( args.Length < 2 )
            {
                Console.WriteLine( "Sample2: No username and password specified!" );
                return;
            }

            // save our logon details
            user = args[ 0 ];
            pass = args[ 1 ];

            // create our steamclient instance
            steamClient = new SteamClient();
            // create the callback manager which will route callbacks to function calls
            manager = new CallbackManager( steamClient );

            // get the steamuser handler, which is used for logging on after successfully connecting
            steamUser = steamClient.GetHandler<SteamUser>();

            // register a few callbacks we're interested in
            // these are registered upon creation to a callback manager, which will then route the callbacks
            // to the functions specified
            new Callback<SteamClient.ConnectedCallback>( OnConnected, manager );
            new Callback<SteamClient.DisconnectedCallback>( OnDisconnected, manager );

            new Callback<SteamUser.LoggedOnCallback>( OnLoggedOn, manager );
            new Callback<SteamUser.LoggedOffCallback>( OnLoggedOff, manager );

            isRunning = true;

            Console.WriteLine( "Connecting to Steam..." );

            // initiate the connection
            steamClient.Connect();

            // create our callback handling loop
            while ( isRunning )
            {
                // in order for the callbacks to get routed, they need to be handled by the manager
                manager.RunWaitCallbacks( TimeSpan.FromSeconds( 1 ) );
            }
        }

        static void OnConnected( SteamClient.ConnectedCallback callback )
        {
            if ( callback.Result != EResult.OK )
            {
                Console.WriteLine( "Unable to connect to Steam: {0}", callback.Result );

                isRunning = false;
                return;
            }

            Console.WriteLine( "Connected to Steam! Logging in '{0}'...", user );

            steamUser.LogOn( new SteamUser.LogOnDetails
            {
                Username = user,
                Password = pass,
            } );
        }

        static void OnDisconnected( SteamClient.DisconnectedCallback callback )
        {
            Console.WriteLine( "Disconnected from Steam" );

            isRunning = false;
        }

        static void OnLoggedOn( SteamUser.LoggedOnCallback callback )
        {
            if ( callback.Result != EResult.OK )
            {
                if ( callback.Result == EResult.AccountLogonDenied )
                {
                    // if we recieve AccountLogonDenied or one of it's flavors (AccountLogonDeniedNoMailSent, etc)
                    // then the account we're logging into is SteamGuard protected
                    // see sample 6 for how SteamGuard can be handled

                    Console.WriteLine( "Unable to logon to Steam: This account is SteamGuard protected." );

                    isRunning = false;
                    return;
                }

                Console.WriteLine( "Unable to logon to Steam: {0} / {1}", callback.Result, callback.ExtendedResult );

                isRunning = false;
                return;
            }

            Console.WriteLine( "Successfully logged on!" );

            // at this point, we'd be able to perform actions on Steam

            // for this sample we'll just log off
            steamUser.LogOff();
        }

        static void OnLoggedOff( SteamUser.LoggedOffCallback callback )
        {
            Console.WriteLine( "Logged off of Steam: {0}", callback.Result );
        }
    }
}

new Callback<SteamClient.ConnectedCallback>( OnConnected, manager );是我主要的绊脚石,但据我所知,一些像GetHandler这样的早期部分虽然通过编译也没有翻译过。

无论如何,任何建议(或完整的转换)都将不胜感激!

克里斯

1 个答案:

答案 0 :(得分:0)

对于给你带来麻烦的陈述,你需要这样的东西:

Dim TempConnectedCallback As Callback(Of SteamClient.ConnectedCallback) = New Callback(Of SteamClient.ConnectedCallback)(AddressOf OnConnected, manager)

原因是VB不允许将对象实例化分配给任何东西。