我是c#和silverlight-5初学者。
首先,我必须通过反序列化xml字符串来创建对象。我已经成功地做到了这一点,但是现在我的下一步是使用对象元素创建GUI。我有一个想法,我必须使用"IValueConverter“来完成这个任务。但我不知道怎么回事。
包含该对象的Program如下所示:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Collections;
namespace Model.XML
{
public class ProgramControl
{
public static void Main()
{
string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?>
<parameter>
<name>max_amount</name>
<label>Max Amount</label>
<unit>Millions</unit>
<component>
<type>Combo</type>
<attributes>
<type>Integer</type>
<displayed>4</displayed>
<selected>0</selected>
<items>
<item>5</item>
<item>10</item>
<item>20</item>
<item>50</item>
</items>
</attributes>
</component >
</parameter>";
XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));
XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
Parameter parameter = (Parameter)deserializer.Deserialize(reader);
foreach (var item in parameter.Component.Attributes.Items)
{
Debug.WriteLine(item);
}
Debug.WriteLine(parameter.Component.Type);
Debug.WriteLine(parameter.Name);
Debug.WriteLine(parameter.Label);
Debug.WriteLine(parameter.Unit);
}
}
}现在的问题是如何从反序列化(IValueConverter )获得的对象创建GUI?
编辑:我几乎不知道如何实现这个目标:
首先,在包含"IValueConverter“接口的类中,我们必须将对象(通过反序列化获得)转换为参数,然后通过return将这些参数(包含在c#中创建的组合框)通过return传递给xaml编解码器,其中包含容器,用于呈现我们刚刚使用c#创建的GUI。
在Xaml代码中,我们只需要创建容器,它将显示组合框和前面步骤中在c#代码中创建的其他标签和文本。(我们不必在这里使用xaml创建组合框,它是在包含返回UI的c#接口的类内的IValueConverter代码中创建的)。
例如:(让你正确理解这是个粗略的想法,可能会有一些语法错误):
我的"MyValueConverter.cs“类假设如下:
public class MyValueConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
List<parameter> list = value as List<Parameter>;
List<UIElement> result = new List<UIElement>();
foreach(parameter p in list)
{
UIElement newEle = null;
if (p.component.type == "Combo")
{
newEle = new ComboBox();
}
result.add(newEle);
}
return result;
/////////////////////////////////////////////////
//////////////// and so on ://///////////////////
/////////////////////////////////////////////////
}
}而在xaml文件中,我必须创建一个容器来呈现在c#(IValueConverter接口类)中创建的UI。因此,我们必须选择任何容器,这些容器必须能够呈现组合框、标签、文本--快照的GUI中显示的所有数据(conatiner可能是StackPanel,因为需要显示的东西不止一个)。
我的xaml代码将只包含这样一个容器:
<UserControl x:Class="RenderingTest.MainPage"
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:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:this="clr-namespace:RenderingTest.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<this:MyValueConverter x:Key="ImageConverter"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Width="Auto" Height="Auto">
<!-- There should be container here to render the combo box
created using c# code in "MyValueConverter" class -->
</Grid>
</UserControl>能帮上忙吗?请不要犹豫,如果还不能理解的话。
发布于 2014-05-06 09:08:29
为此,可以使用隐式数据类型。
在xaml中,为特定的数据类型定义了一个模板:
<DataTemplate DataType="ComboParameter">
<StackPanel Orientation="Horizontal">
<TextBlock Text={Binding Path=label}" />
<ComboBox ItemsSource="{Binding Path=items}"/>
<TextBlock Text="{Binding Path=unit}"/>
</StackPanel>
</DataTemplate>您最好根据类型元素值创建不同的类型。另一种解决方案是为类型参数创建一个大型模板,并根据参数类型包含的内容显示适当的元素。但我不推荐这种方法。
然后可以使用ItemsControl显示所有参数:
<ItemsControl ItemsSource="{Binding Path=Parameters}" />不同的参数将以不同的方式呈现,这取决于它的类型。
https://stackoverflow.com/questions/23488983
复制相似问题