C# 字符串无法转换为System.Windows.Control.Label
我的Wpf应用程序中的代码行有问题。C# 字符串无法转换为System.Windows.Control.Label,c#,wpf,C#,Wpf,我的Wpf应用程序中的代码行有问题。label.Content=(string)nextfruit[“q_text”]我得到这个错误(类型“string”不能隐式转换为“System.Windows.Controls.Label”),我认为这应该像我所做的那样工作 这是我的代码 using System; using System.IO; using System.Net; using System.Windows; using System.Web.Script.Serialization;
label.Content=(string)nextfruit[“q_text”]代码>我得到这个错误(类型“string”不能隐式转换为“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; }
//}
}
}
<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>
使用系统;
使用System.IO;
Net系统;
使用System.Windows;
使用System.Web.Script.Serialization;
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用System.Json;
使用System.Linq;
使用Newtonsoft.Json.Linq;
名称空间MMPI2_测试
{
///
///Interaktionslogik für Patient.xaml
///
公共部分类患者:窗口
{
公共病人()
{
初始化组件();
已加载+=我的窗口已加载;
}
公共字符串MyProperty{get;set;}
公共字符串属性{get;set;}
私有IEnumerator枚举数{get;set;}
公共bool HasNext{get;private set;}
已加载私有void MyWindow_(对象发送方,路由目标)
{
dynamic convert=JsonConvert.DeserializeObject(MyProperty);
字符串user=MyProperty;
//lbuser.Content=json;
//字符串tan=“”;
MainWindow main=新的MainWindow();
//main.alpha=tan;
string html=string.Empty;
字符串url=@“http://stidl.workcloud.at/?tag=question&token=“+财产;
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression=DecompressionMethods.GZip;
使用(HttpWebResponse=(HttpWebResponse)request.GetResponse())
使用(Stream=response.GetResponseStream())
使用(StreamReader=新StreamReader(stream))
{
html=reader.ReadToEnd();
}
//dynamic magic=JsonConvert.DeserializeObject(html);
//字符串json2=newJavaScriptSerializer().Serialize(html);
var j=新的JavaScriptSerializer()。反序列化对象(用户)作为字典;
var d=j[“数据”]作为字典;
lbuser.Content=d[“fname”]+“”+d[“lname”].ToString();
//JObject QuestionObject=JObject.Parse(html);
//JToken question=QuestionObject[“数据”]。第一个[“q_text”];
//内容=问题;
JObject QuestionObject=JObject.Parse(html);
Enumerator=QuestionObject[“data”].Children().GetEnumerator();
JObject IDObject=JObject.Parse(html);
JToken id=IDObject[“数据”]。第一个[“q_id”];
JToken lastid=IDObject[“数据”]。Last[“q_id”];
//JToken nextid=IDObject[“数据”]。First.Next.Next[“q_id”];
lbid.Content=“Frage”+id+“von”+lastid;
}
qq类
{
}
私有无效bt\u否\u单击(对象发送方,路由目标)
{
string html=string.Empty;
字符串url=@“http://stidl.workcloud.at/?tag=question&token=“+财产;
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression=DecompressionMethods.GZip;
使用(HttpWebResponse=(HttpWebResponse)request.GetResponse())
使用(Stream=response.GetResponseStream())
使用(StreamReader=新StreamReader(stream))
{
html=reader.ReadToEnd();
}
//JObject IDObject=JObject.Parse(html);
//JToken nextid=IDObject[“数据”]。第一个[“q_id”];
////int result=(int)nextid;
//lbid.Content=nextid;
HasNext=Enumerator.MoveNext();
如果(下一步)
{
JObject QuestionObject=JObject.Parse(html);
Enumerator=QuestionObject[“data”].Children().GetEnumerator();
JObject nextfruit=枚举器.Current;
lbquestion=(字符串)nextfruit[“q_text”];
}
其他的
{
}
}
私有无效bt_是_单击(对象发送方,路由目标)
{
}
//公共类问题数据
//{
//公共字符串数据{get;set;}//这将存储JSON字符串
//公共列表DataList{get;set;}//这将是实际的列表。
//}
//公共类数据
//{
//公共字符串q_id{get;set;}
//公共字符串q_text{get;set;}
//}
}
}
我的XML代码
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; }
//}
}
}
<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>
if(HasNext)
{
JObject QuestionObject=JObject.Parse(html);
Enumerator=QuestionObject[“data”].Children().GetEnumerator();
JObject nextfruit=枚举器.Current;
lbquestion=(string)nextfruit[“q_text”];//lbquestion是一个标签
}
在我看来,这应该是设置内容。这甚至是你在问题中所说的。更新:我没有注意到问题是一个标签
(string)nextfruit[“q_text”]
是试图强制转换字符串lbQuestion.Content
是您要转换为字符串时需要访问的内容;错误是告诉您您试图将标签等同于字符串,而事实并非如此 lbquestion
是一个标签
控件。OP包括定义它的XAML。哦,你是对的,但这实际上也解释了它。我会修正我的答案。很抱歉成为一个PITA,但关于C#中强制转换运算符的优先级,您也错了:使用lbquestion.Content=(string)nextfruit[“q#text”]
设置标签的文本。不过,您还有其他错误(Enumerator.MoveNext)没问题,每个人有时都会忽略一些简单的错误。