我的Wpf Apllication中的代码行有问题。label.Content = (string)nextfruit["q_text"];
我收到此错误(类型"字符串"无法隐式转换为" System.Windows。 Controls.Label")我认为这应该按照我的方式工作。
有我的代码
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Json;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace MMPI2_Test
{
/// <summary>
/// Interaktionslogik für Patient.xaml
/// </summary>
public partial class Patient : Window
{
public Patient()
{
InitializeComponent();
Loaded += MyWindow_Loaded;
}
public String MyProperty { get; set; }
public String Property { get; set; }
private IEnumerator<JObject> Enumerator { get; set; }
public bool HasNext { get; private set; }
private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
dynamic convert = JsonConvert.DeserializeObject(MyProperty);
string user = MyProperty;
//lbuser.Content = json;
//string tan = "";
MainWindow main = new MainWindow();
// main.alpha = tan;
string html = string.Empty;
string url = @"http://stidl.workcloud.at/?tag=question&token=" + Property;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
// dynamic magic = JsonConvert.DeserializeObject(html);
// string json2 = new JavaScriptSerializer().Serialize(html);
var j = new JavaScriptSerializer().DeserializeObject(user) as Dictionary<string, object>;
var d = j["data"] as Dictionary<string, object>;
lbuser.Content = d["fname"] + " " + d["lname"].ToString();
//JObject QuestionObject = JObject.Parse(html);
//JToken question = QuestionObject["data"].First["q_text"];
//lbquestion.Content = question;
JObject QuestionObject = JObject.Parse(html);
Enumerator = QuestionObject["data"].Children<JObject>().GetEnumerator();
JObject IDObject = JObject.Parse(html);
JToken id = IDObject["data"].First["q_id"];
JToken lastid = IDObject["data"].Last["q_id"];
//JToken nextid = IDObject["data"].First.Next.Next["q_id"];
lbid.Content = "Frage " + id + " von " + lastid;
}
class qq
{
}
private void bt_no_Click(object sender, RoutedEventArgs e)
{
string html = string.Empty;
string url = @"http://stidl.workcloud.at/?tag=question&token=" + Property;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
//JObject IDObject = JObject.Parse(html);
//JToken nextid = IDObject["data"].First["q_id"];
////int result = (int)nextid;
//lbid.Content = nextid;
HasNext = Enumerator.MoveNext();
if (HasNext)
{
JObject QuestionObject = JObject.Parse(html);
Enumerator = QuestionObject["data"].Children<JObject>().GetEnumerator();
JObject nextfruit = Enumerator.Current;
lbquestion = (string)nextfruit["q_text"];
}
else
{
}
}
private void bt_yes_Click(object sender, RoutedEventArgs e)
{
}
//public class QuestionData
//{
// public string Data { get; set; } // this will store the JSON string
// public List<Data> DataList { get; set; } // this will be the actually list.
//}
//public class Data
//{
// public string q_id { get; set; }
// public string q_text { get; set; }
//}
}
}
我的XML代码
<Window x:Class="MMPI2_Test.Patient"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MMPI2_Test"
mc:Ignorable="d"
Title="Patient" Height="560" Width="980">
<Window.Background>
<ImageBrush ImageSource="images/background.jpg" />
</Window.Background>
<Grid>
<Label x:Name="lbquestion" Content="" HorizontalAlignment="Left" Margin="31,229,0,0" VerticalAlignment="Top" Width="906" Height="71"/>
<Button x:Name="bt_yes" Content="Ja" HorizontalAlignment="Left" Margin="534,349,0,0" VerticalAlignment="Top" Width="75" Click="bt_yes_Click" />
<Button x:Name="bt_no" Content="Nein" HorizontalAlignment="Left" Margin="389,349,0,0" VerticalAlignment="Top" Width="75" Click="bt_no_Click"/>
<Label x:Name="lbuser" Content="" HorizontalAlignment="Left" Height="73" Margin="31,151,0,0" VerticalAlignment="Top" Width="906"/>
<Label x:Name="lbid" Content="" HorizontalAlignment="Left" Height="50" Margin="31,69,0,0" VerticalAlignment="Top" Width="783"/>
</Grid>
答案 0 :(得分:1)
if (HasNext)
{
JObject QuestionObject = JObject.Parse(html);
Enumerator = QuestionObject["data"].Children<JObject>().GetEnumerator();
JObject nextfruit = Enumerator.Current;
lbquestion = (string)nextfruit["q_text"]; //lbquestion is a label
}
在我看来,这应该是设置内容。这甚至是你在问题中所说的。
答案 1 :(得分:-1)
更新:我错过了lbQuestion是一个标签。
(string)nextfruit["q_text"]
是一个强制转换字符串的尝试。如果您要转换为字符串,则需要访问lbQuestion.Content
;错误告诉您,您正在尝试将标签等同为字符串,而不是。