搜索时,我会得到所有游戏的列表,然后我在列表中通过AppID搜索游戏。有的游戏画面正常显示,有的则缺失。 当空图像时,我得到
Error when receiving data for a game with AppID 1237970;
1
{"1237970":{"success":false}}
同时,如果我用这个AppID手动去浏览器请求,就会显示信息。 还有,有一次偶然看不清游戏画面是如何正常出现的。
Pastebin:https://pastebin.com/c92PYXr2
public partial class MainPage : Page {
private readonly string _apiKey = "";
SettingsManager settingsManager = new SettingsManager();
AppSettings settings;
public string SteamPath;
public ObservableCollection<App> Apps { get; set; }
public MainPage() {
InitializeComponent();
settings = settingsManager.LoadSettings();
SteamPath = settings.steampath;
Apps = new ObservableCollection<App>();
DataContext = this;
}
private void SettingsBtn_Click(object sender, RoutedEventArgs e) {
FrameApp.FrameMain.Navigate(new SettingsPage());
}
private async void TestBtn_Click(object sender, RoutedEventArgs e) {
if (!SteamPath.Contains("Steam") || SteamPath == "" || SteamPath == String.Empty || SteamPath == null) {
MessageBox.Show("Incorrect path to the Steam folder!");
FrameApp.FrameMain.Navigate(new SettingsPage());
} else {
Debug.WriteLine($"### Getting the list of games started ###");
await GetAllApps();
Debug.WriteLine($"### End of getting the list of games ###");
}
}
private async void SearchTBX_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter) {
string searchTerm = SearchTBX.Text;
await SearchGamesAsync(searchTerm);
}
}
public async Task<AppDetails> GetAppDetails(int appId) {
using (var httpClient = new HttpClient()) {
try {
var response = await httpClient.GetStringAsync($"https://store.steampowered.com/api/appdetails/?appids={appId}");
var appDetails = JsonConvert.DeserializeObject<Dictionary<string, AppDetails>>(response);
if (appDetails.ContainsKey(appId.ToString()) && appDetails[appId.ToString()].Success) {
return appDetails[appId.ToString()];
} else {
Debug.WriteLine($"Error when receiving data for a game with AppID {appId};\n {appDetails.Count}\n{response.ToString()}");
return new AppDetails { Success = false, Data = null };
}
} catch (Exception ex) {
Debug.WriteLine($"Error receiving data: {ex.Message}");
return new AppDetails { Success = false, Data = null };
}
}
}
public async Task SearchGamesAsync(string searchTerm) {
using (var httpClient = new HttpClient()) {
try {
var response = await httpClient.GetStringAsync($"http://api.steampowered.com/ISteamApps/GetAppList/v2/");
var steamApiResponse = JsonConvert.DeserializeObject<SteamApiResponse>(response);
var apps = steamApiResponse.AppList.Apps;
var filteredGames = apps.Where(app => app.AppId.ToString().ToLower().Contains(searchTerm.ToLower())).ToList();
var tasks = filteredGames.Select(app => ProcessGameAsync(app)).ToList();
await Task.WhenAll(tasks);
GamesDG.ItemsSource = filteredGames;
} catch (Exception ex) {
Debug.WriteLine($"Error receiving data: {ex.Message}");
}
}
}
private async Task ProcessGameAsync(App app) {
var appDetails = await GetAppDetails(app.AppId);
if (appDetails.Success) {
app.header_image = appDetails.Data?.header_image;
MessageBox.Show($"Added an image for\nGame: {app.Name} ({app.AppId});\n Image: {app.header_image}");
}
}
}
public class SteamApiResponse {
public AppList AppList { get; set; }
}
public class AppList {
public List<App> Apps { get; set; }
}
public class AppDetails {
public bool Success { get; set; }
public App Data { get; set; }
}
public class App {
public int AppId { get; set; }
public string Name { get; set; }
public string header_image { get; set; }
}
我尝试使用Steam不同地区的API密钥。
问题解决了——都是关于区域和VPN的。无论 API 密钥位于哪个区域,连接本身都必须来自游戏显示的区域。代码可能不专业,但它是正确且有效的。