国产午夜男女在线|欧美日本一道高清国产|亚洲日韩乱码中文字幕|麻豆国产97在线精品一区|日韩一区2区三区另类图片|亚洲精品国产99在线观看|亚洲国产午夜福利精品大秀在线|一级做a爰片性色毛片免费网站

          1. <form id="n2a4a"><nav id="n2a4a"></nav></form>
          2. 您當(dāng)前的位置 :寧夏資訊網(wǎng) > 資訊 >  內(nèi)容正文
            投稿

            C# WPF從RIOT API獲取數(shù)據(jù)(RIOT代表作品《英雄聯(lián)盟》)

            寧夏資訊網(wǎng) 2020-03-28 07:04:00 來源: 閱讀:-

            閱讀導(dǎo)航

            1. 本文背景
            2. 代碼實現(xiàn)
            3. 本文參考

            1. 本文背景

            RIOT(拳頭)是一家美國網(wǎng)游開發(fā)商,成立于2006年,代表作品《英雄聯(lián)盟》。

            本文重點要講解兩個知識點:

            1. C# 使用 HttpClient 訪問 RIOT 提供的 API 接口,獲取召喚者概況信息;
            2. C# WPF界面展示召喚者信息搜索、概況信息兩個界面。

            2. 代碼實現(xiàn)

            站長使用 .Net CORE 3.1 創(chuàng)建名為 “LoLGoal” 的WPF解決方案,并添加3個Nuget包,配置如下:

            &lt;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&gt;&lt;packages&gt;  &lt;package id=&#34;MaterialDesignColors&#34; version=&#34;1.1.1&#34; targetFramework=&#34;net45&#34; /&gt;  &lt;package id=&#34;MaterialDesignThemes&#34; version=&#34;2.5.0.1205&#34; targetFramework=&#34;net45&#34; /&gt;  &lt;package id=&#34;Newtonsoft.Json&#34; version=&#34;12.0.1&#34; targetFramework=&#34;net45&#34; /&gt;&lt;/packages&gt;

            界面使用的MD控件,本站曾有介紹:介紹 。

            本文只簡單說明部分代碼,整體解決方案目錄結(jié)構(gòu)如下,源碼文末會給出:

            2.1 引入MD控件樣式

            文件【App.xaml】

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

            2.2 召喚者概況搜索界面

            文件【MainWindow.xaml】代碼,界面布局簡單,給人的感覺整體簡潔大方:

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

            召喚者概況搜索界面



            2.3 召喚者概況信息展示界面

            文件【W(wǎng)indowProfile.xaml】,布局代碼也不多,清爽:

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

            概況信息展示界面



            2.4 簡單的API接口調(diào)用封裝

            直接上代碼看,Key.txt是存儲的RIOT開發(fā)者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(&#34;API/Key.txt&#34;);        }        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 &#34;https://&#34; + Region + &#34;.api.riotgames.com/lol/&#34; + path + &#34;?api_key=&#34; + Key;        }        public string GetKey(string path)        {            StreamReader sr = new StreamReader(path);            return sr.ReadToEnd();        }    }}

            2.5 其他代碼

            查看源碼:get_profile_data

            2.6 以下是站長方便演示、截圖,修改的部分文件

            可參考源碼對比:

            文件【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&lt;PositionDTO&gt; GetPositions(string summonerId)        {            //1、這是正常的API訪問            //string path = &#34;league/v4/positions/by-summoner/&#34; + summonerId;            //var response = GET(GetURI(path));            //string content = response.Content.ReadAsStringAsync().Result;            //if (response.StatusCode == System.Net.HttpStatusCode.OK)            //{            //    return JsonConvert.DeserializeObject&lt;List&lt;PositionDTO&gt;&gt;(content);            //}            //else            //{            //    return null;            //}            //2、這是模擬數(shù)據(jù),正常訪問LOL服務(wù)器,需要注冊Key            string[] tiers = { &#34;Bronze&#34;, &#34;Challenger&#34;, &#34;Diamond&#34;, &#34;Gold&#34;, &#34;Grandmaster&#34;, &#34;Iron&#34;, &#34;Master&#34;, &#34;Platinum&#34;, &#34;Silver&#34; };            var rd = new Random(DateTime.Now.Millisecond);            var lst = new List&lt;PositionDTO&gt;();            for (int i = 0; i &lt; rd.Next(5, 20); i++)            {                lst.Add(new PositionDTO                {                    Tier = tiers[rd.Next(0, tiers.Length)],                    Rank = &#34;IV&#34;,                    Wins = rd.Next(2, 100),                    Losses = rd.Next(2, 100),                    QueueType = &#34;RANKED_SOLO_5x5&#34;                });            }            return lst;        }    }}

            文件【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 = &#34;summoner/v4/summoners/by-name/&#34; + SummonerName;            //var response = GET(GetURI(path));            //string content = response.Content.ReadAsStringAsync().Result;            //if(response.StatusCode == System.Net.HttpStatusCode.OK)            //{            //    return JsonConvert.DeserializeObject&lt;SummonerDTO&gt;(content);            //}            //else            //{            //    return null;            //}            //2、這是模擬數(shù)據(jù),正常訪問LOL服務(wù)器,需要注冊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.參考

            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。

            最終源碼:本文代碼幾乎和源碼一致(第五和第六個視頻配套Github源碼 【get_profile_data】),站長未注冊RIOT開發(fā)者Key,所以代碼中采用模擬返回數(shù)據(jù)的方式,只展示了界面效果,并將部分英文改為中文,便于向大家展示此工程。

            點擊下載源碼:get_profile_data

            除非注明,文章均由 Dotnet9 整理發(fā)布,歡迎轉(zhuǎn)載。
            轉(zhuǎn)載請注明本文地址:https://dotnet9.com/7026.html

            (正文已結(jié)束)

            推薦閱讀:蘋果7p好還是8好

            免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點,文章真實性請瀏覽者慎重核實!任何投資加盟均有風(fēng)險,提醒廣大民眾投資需謹(jǐn)慎!

            網(wǎng)站簡介 - 聯(lián)系我們 - 營銷服務(wù) - 老版地圖 - 版權(quán)聲明 - 網(wǎng)站地圖
            Copyright.2002-2019 寧夏資訊網(wǎng) 版權(quán)所有 本網(wǎng)拒絕一切非法行為 歡迎監(jiān)督舉報 如有錯誤信息 歡迎糾正
            观塘区| 普洱| 郴州市| 襄城县| 馆陶县| 南康市| 祁连县| 华池县| 长顺县| 福安市| 新民市| 浪卡子县| 集贤县| 理塘县| 竹北市| 瑞安市| 颍上县| 青河县| 赫章县| 香格里拉县| 两当县| 淄博市| 建阳市| 广东省| 闸北区| 盐山县| 临沂市| 岑溪市| 双桥区| 当阳市| 常熟市| 府谷县| 惠水县| 祁东县| 仙游县| 斗六市| 桐庐县| 沈丘县| 龙州县| 裕民县| 香格里拉县|