C # WPF data acquisition (RIOT works on behalf of "League") from RIOT API

Micro-channel public number: Dotnet9 , website: Dotnet9 , questions or suggestions: please website message ,
if helpful to you: welcome appreciation .

C # WPF data acquisition (RIOT works on behalf of "League") from RIOT API

Read navigation

  1. This paper background
  2. Code
  3. Reference article

1. This background

RIOT (fist) is an American game developers, was founded in 2006, works on behalf of "Heroes Union."

This article focuses on two knowledge points to explain:

  1. C # using HttpClient API interface to access RIOT, thus providing a summoner profile information;
  2. C # WPF interface display Summoner information search, information profile both interfaces.

RIOT API to access the data show

2. code implementation

Webmaster using .Net CORE 3.1 created called "LoLGoal" of WPF solution and add 3 Nuget package, configuration is as follows:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MaterialDesignColors" version="1.1.1" targetFramework="net45" />
  <package id="MaterialDesignThemes" version="2.5.0.1205" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net45" />
</packages>

The control interface uses MD, the site had Introduction: Introduction .

Only briefly described herein, part of the code, the overall solution directory structure, the text will be given the end Source:

Directory Structure

MD 2.1 introduces control style

[File] App.xaml

<Application x:Class="LoLGoal.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="View/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Purple.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

2.2 Overview of Summoner search interface

[File] MainWindow.xaml code interface layout is simple, simple and elegant overall impression:

<Window x:Class="LoLGoal.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Height="600" Width="400" WindowStartupLocation="CenterScreen" 
        MouseLeftButtonDown="Window_MouseLeftButtonDown"
        ResizeMode="NoResize" WindowStyle="None" Background="#FF410A66">
    <Grid>
        <StackPanel Margin="50">
            <Image Source="/Assets/logo2.png" Width="96" Height="96"/>
            <Border Background="White" Margin="10 20" CornerRadius="5">
                <StackPanel Margin="25">
                    <ComboBox Margin="15" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="地区" Text="{Binding Region}">
                        <ComboBoxItem Content="RU"/>
                        <ComboBoxItem Content="KR"/>
                        <ComboBoxItem Content="BR1"/>
                        <ComboBoxItem Content="OC1"/>
                        <ComboBoxItem Content="JP1"/>
                        <ComboBoxItem Content="NA1"/>
                        <ComboBoxItem Content="EUN1"/>
                        <ComboBoxItem Content="EUW1"/>
                        <ComboBoxItem Content="TR1"/>
                        <ComboBoxItem Content="LA1"/>
                        <ComboBoxItem Content="LA2"/>
                    </ComboBox>
                    <TextBox Text="{Binding SummonerName}" Margin="15" Style="{StaticResource MaterialDesignFloatingHintTextBox}" materialDesign:HintAssist.Hint="召唤者"/>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <Button Margin="15 50" Content="取消"/>
                        <Button x:Name="ButtonSignUp" Margin="15 50" Content="搜索" Click="ButtonSignUp_Click"/>
                    </StackPanel>
                </StackPanel>
            </Border>
        </StackPanel>
    </Grid>
</Window>

Summoner profile search interface

Summoner profile search interface

2.3 summoner profile information display interface

WindowProfile.xaml file [], not many layout code, fresh:

<Window x:Class="LoLGoal.View.WindowProfile"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Height="600" Width="400" 
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize" 
        WindowStyle="None" Background="#FF410A66">
    <Grid>
        <Border Background="White" Margin="20 100 20 20" CornerRadius="15">
            <StackPanel VerticalAlignment="Top" HorizontalAlignment="Stretch">
                <Border Width="100" Height="100" Margin="20 20 0 10" BorderBrush="Gray" HorizontalAlignment="Left" BorderThickness="1" CornerRadius="15">
                    <Border.Background>
                        <ImageBrush ImageSource="{Binding Path=Icon}"/>
                    </Border.Background>
                </Border>
                <TextBlock Margin="20 15" FontSize="30" Text="{Binding Path=SummonerName}" Foreground="DarkGray"/>
                <StackPanel Orientation="Horizontal" Margin="20 0">
                    <StackPanel Margin="5">
                        <TextBlock Text="胜" FontSize="15" FontWeight="Bold" Foreground="Green"/>
                        <TextBlock Text="{Binding Path=Wins}" FontSize="18" Foreground="Gray" HorizontalAlignment="Center"/>
                    </StackPanel>
                    <StackPanel Margin="5">
                        <TextBlock Text="输" FontSize="15" FontWeight="Bold" Foreground="DarkRed"/>
                        <TextBlock Text="{Binding Path=Losses}" FontSize="18" Foreground="Gray" HorizontalAlignment="Center"/>
                    </StackPanel>
                </StackPanel>
                <StackPanel Margin="30 20">
                    <TextBlock Text="水平" FontSize="15" Foreground="LightGray"/>
                    <TextBlock Text="{Binding Path=Level}" HorizontalAlignment="Center" FontSize="80" Foreground="Gray"/>
                </StackPanel>
                <Grid Margin="20 10">
                    <Button x:Name="ButtonSearch" HorizontalAlignment="Left" Style="{StaticResource MaterialDesignFlatButton}" Width="100" Click="ButtonSearch_Click">
                        <materialDesign:PackIcon Kind="Search" Width="24" Height="24"/>
                    </Button>
                    <Button HorizontalAlignment="Right" Width="100" Content="登录"/>
                </Grid>
            </StackPanel>
        </Border>
        <StackPanel HorizontalAlignment="Right" Margin="30 10">
            <Image Source="{Binding Path=Emblem}" Width="200" Height="200">
                <Image.Effect>
                    <DropShadowEffect BlurRadius="40" ShadowDepth="1"/>
                </Image.Effect>
            </Image>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock FontSize="18" Foreground="Gray" Text="{Binding Path=Tier}" Margin="5" VerticalAlignment="Center"/>
                <TextBlock FontSize="20" Foreground="Gray" Text="{Binding Path=Rank}" Margin="5"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

Profile information display interface

Profile information show

2.4 Simple API interface calls package

Directly on the code to see, Key.txt is stored RIOT Developer Key:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace LoLGoal.API
{
    public class Api
    {
        private string Key { get; set; }
        private string Region { get; set; }

        public Api(string region)
        {
            Region = region;
            Key = GetKey("API/Key.txt");
        }

        protected HttpResponseMessage GET(string URL)
        {
            using (HttpClient client = new HttpClient())
            {
                var result = client.GetAsync(URL);
                result.Wait();

                return result.Result;
            }
        }

        protected string GetURI(string path)
        {
            return "https://" + Region + ".api.riotgames.com/lol/" + path + "?api_key=" + Key;
        }

        public string GetKey(string path)
        {
            StreamReader sr = new StreamReader(path);
            return sr.ReadToEnd();
        }
    }
}

2.5 Other Code

View Source: get_profile_data

2.6 The following is the webmaster convenient demo, screenshots, modify the part of the file

Reference source can comparison:

File [API / League_V4.cs]

using LoLGoal.Model;
using System;
using System.Collections.Generic;

namespace LoLGoal.API
{
    public class League_V4 : Api
    {
        public League_V4(string region) : base(region)
        {
        }

        public List<PositionDTO> GetPositions(string summonerId)
        {
            //1、这是正常的API访问
            //string path = "league/v4/positions/by-summoner/" + summonerId;

            //var response = GET(GetURI(path));
            //string content = response.Content.ReadAsStringAsync().Result;

            //if (response.StatusCode == System.Net.HttpStatusCode.OK)
            //{
            //    return JsonConvert.DeserializeObject<List<PositionDTO>>(content);
            //}
            //else
            //{
            //    return null;
            //}

            //2、这是模拟数据,正常访问LOL服务器,需要注册Key
            string[] tiers = { "Bronze", "Challenger", "Diamond", "Gold", "Grandmaster", "Iron", "Master", "Platinum", "Silver" };
            var rd = new Random(DateTime.Now.Millisecond);
            var lst = new List<PositionDTO>();
            for (int i = 0; i < rd.Next(5, 20); i++)
            {
                lst.Add(new PositionDTO
                {
                    Tier = tiers[rd.Next(0, tiers.Length)],
                    Rank = "IV",
                    Wins = rd.Next(2, 100),
                    Losses = rd.Next(2, 100),
                    QueueType = "RANKED_SOLO_5x5"
                });
            }
            return lst;
        }
    }
}

File [API / Summoner_V4.cs]

using LoLGoal.Model;
using System;

namespace LoLGoal.API
{
    public class Summoner_V4 : Api
    {
        public Summoner_V4(string region) : base(region)
        {
        }

        public SummonerDTO GetSummonerByName(string SummonerName)
        {
            //1、这是正常的API访问
            //string path = "summoner/v4/summoners/by-name/" + SummonerName;

            //var response = GET(GetURI(path));
            //string content = response.Content.ReadAsStringAsync().Result;

            //if(response.StatusCode == System.Net.HttpStatusCode.OK)
            //{
            //    return JsonConvert.DeserializeObject<SummonerDTO>(content);
            //}
            //else
            //{
            //    return null;
            //}

            //2、这是模拟数据,正常访问LOL服务器,需要注册Key
            return new SummonerDTO
            {
                ProfileIconId = DateTime.Now.Second,
                Name = SummonerName,
                SummonerLevel = new Random(DateTime.Now.Millisecond).Next(50, 200),
                Id = DateTime.Now.Second.ToString()
            };
        }
    }
}

3. References

  1. 视频一:C# WPF Design UI - #1 - Login,配套源码:LoLGoal
  2. 视频二:C# WPF Design UI - #2 (1/2) - REST API Access,配套源码:get_summoner_data
  3. 视频三:C# WPF Design UI - #2 (2/2) - REST API Access,配套源码:get_summoner_data
  4. 视频四:C# WPF Design UI - #3 - Profile,配套源码:summoner_profile
  5. 视频五:C# WPF Design UI - #4 (1/2) - Get Data From RIOT API,配套源码:get_profile_data
  6. 视频六:C# WPF Design UI - #4 (2/2)- Get Data From RIOT API,配套源码:get_profile_data

The final Source: This article and the source code is almost the same (fifth and sixth video source supporting Github [ get_profile_data ]), the owners are not registered RIOT developer Key, so the code used in analog mode return data only shows the interface effects, read Chinese and English part, easy to show this project.

Download Source: get_profile_data

Except where noted, the article by Dotnet9 finishing release, welcome to reprint.

Address reprint this article please specify: https://dotnet9.com/7026.html

welcome attention Dotnet9 Fanger Wei code scanning at the micro-channel public number, this site will promptly push the latest technology

Dotnet9

Guess you like

Origin www.cnblogs.com/Dotnet9-com/p/12187624.html