我想将一个从宁静的API中获取的字符串值分配给我的指定值,这是一个“enum”类变量。我试图这样做但是它不起作用我想。可以告诉我如何做得对吗? People.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
namespace ISTE.Models
{
public enum Designation
{
Lecturer,
Professor
}
public class Faculty
{
public BitmapImage _image;
public Uri _IconUri;
public string username { get; set; }
public string name { get; set; }
public string tagline { get; set; }
public string imagePath { get; set; }
public Uri IconUri
{
get;
set; }
public BitmapImage image { get; set; }
public string title { get; set; }
public string interestArea { get; set; }
public string office { get; set; }
public Uri website { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string twitter { get; set; }
public string facebook { get; set; }
public Designation desig { get; set; }
}
public class Staff
{
public string username { get; set; }
public string name { get; set; }
public string tagline { get; set; }
public string imagePath { get; set; }
public string title { get; set; }
public string interestArea { get; set; }
public string office { get; set; }
public Uri website { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string twitter { get; set; }
public string facebook { get; set; }
}
public class People
{
public string title { get; set; }
public string subTitle { get; set; }
public List<Faculty> faculty { get; set; }
public List<Staff> staff { get; set; }
}
}
------------------------------------------------------------------------
DataService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using ISTE.Models;
using System.Net.Http;
namespace ISTE.Services
{
public class PeopleDS
{
public List<Faculty> GetItemDetails()
{
People facItem = new People();
Faculty fa = new Faculty();
List < Faculty > fac = new List<Faculty>();
try
{
using (var client = new HttpClient())
{
//client.DefaultRequestHeaders.Add("X-API-Key", "9ef8ddfc6d254dc3a7b2cac337c6d837");
string uri3 = $"https://ist.xyz.edu/api/people";
var response1 = client.GetAsync(uri3).Result;
var content1 = response1.Content.ReadAsStringAsync().Result;
dynamic item1 = Newtonsoft.Json.JsonConvert.DeserializeObject(content1);
facItem.faculty = item1.faculty.ToObject<List<Faculty>>();
fac = item1.faculty.ToObject<List<Faculty>>();
foreach (Faculty fy in fac)
{
Console.WriteLine("designation \t" + fy.title);
fy.desig = (Designation)Enum.Parse(typeof(Designation), fy.title, true);
// try to parse the string as a TestEnum without throwing an exception
var designation = fy.desig;
if (Enum.TryParse(fy.title, true, out designation))
{
// success
}
else
{
// the string isn't an element of TestEnum
}
// ...
fy.imagePath = fy.imagePath;
fy.IconUri = new Uri(fy.imagePath);
fy.image = new System.Windows.Media.Imaging.BitmapImage(fy.IconUri);
}
}
}
catch (System.Exception ex)
{
return fac;
}
return fac;
}
}
}
答案 0 :(得分:-1)
这样的事情怎么样,在我的头顶,未经测试:
curve(power1, .1, .8, xlab="SMD", ylab="Power")
您可能希望添加错误捕获或者使用Enum.TryParse,并让陷阱异常为您...
答案 1 :(得分:-1)
要将字符串转换为枚举,您可以执行以下操作:
SomeEnum bar;
if (Enum.TryParse(incoming, true, out bar))
{
// parsing succeeded
}