2013-07-30 36 views
0

我的C#比我預期的要複雜得多,所以我正在尋找一些幫助讓我的項目脫離實際。我已經嘗試了許多C#到VB轉換器,但他們都不能處理以回調和我的所有研究方式提供的SteamKit2示例,嘗試使用WithEvents和其他嘗試都失敗了。我道歉,如果這超出了正常的計算器問題的範圍,但底線是我找的C#SteamKit2例子的工作VB.net版本:C#回調轉換

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這樣的早期部分,雖然他們通過了編譯,但並沒有進行翻譯。

無論如何,任何意見(或完整的轉換)將不勝感激!

克里斯

回答

0

你需要這樣的事對於那些給你的麻煩語句:

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

這樣做的原因是,VB不允許沒有分配到任何對象實例。

+0

謝謝,那就是訣竅......現在我有一條更加粗糙的鋤頭之路,沒有SteamKit2被記錄下來,它們的樣本被綁定到沒有分組的回調,可能甚至不是實現我正在計劃大聲笑的功能 –