如何制作一个可以添加到 Windows.Foundation.Collections.ValueSet 的类 [英] How to make a class that can be added to the Windows.Foundation.Collections.ValueSet

查看:7
本文介绍了如何制作一个可以添加到 Windows.Foundation.Collections.ValueSet 的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 UWP 应用程序,它利用 AppServiceConnection 将数据发送到 COM 样式的应用程序.AppServiceConnection.SendMessageAsync() 采用 Windows.Foundation.Collections.ValueSet

I am making a UWP application that utilizes an AppServiceConnection to send data to a COM style application. The AppServiceConnection.SendMessageAsync() takes a type of Windows.Foundation.Collections.ValueSet

ValueSet 类是一个集合,类似于存储

The ValueSet class is a collection, similar to a dictionary that stores KeyValuePairs of types <string, object>

我在向 ValueSet 添加数据时遇到问题,每次尝试添加对象时都会收到错误消息:不支持此类型的数据.(来自 HRESULT 的异常:0x8007065E)"

I am having trouble adding data to the ValueSet, and every time I attempt to add an object I receive the error: "Data of this type is not supported. (Exception from HRESULT: 0x8007065E)"

对这个错误的研究表明,要添加的对象必须是可序列化的类型,并且可以实现 Windows.Foundation.Collections.IPropertySet,它本身是一个似乎存储的集合接口键值对.

research on this error reveals that the object to be added must be of a serializable type, and can implement Windows.Foundation.Collections.IPropertySet which is in of itself a collection interface that seems to store keyvaluepairs.

我想知道如何制作一个可以添加到 ValueSet 的类.我是否必须创建一个实现 IPropertySet 的新集合,或者有什么方法可以使类本身可序列化并能够添加到 ValueSet 中?

I would like to know how to make a class that can be added to a ValueSet. Do I have to create a new collection implementing IPropertySet or is there some way to make the class itself serializable and able to be added to the ValueSet?

如果我必须实现 IPropertySet 谁能告诉我有关如何执行此操作的体面文档?

If I have to implement IPropertySet can anyone point me to decent documentation on how to do this?

推荐答案

WinRT 没有可序列化对象的概念;它只支持值类型,如整数、布尔值、字符串、数组、日期时间等以及这些东西的集合.查看 Create* 成员">PropertyValue 示例(尽管您不需要使用这些方法来创建放入集合中的项目).

WinRT has no notion of serializable objects; it only supports value types like integers, bools, strings, arrays, date-times, etc. and collections of those things. Look at the static Create* members of the PropertyValue class for examples (although you are not required to use those methods to create the items you put into the set).

如果您想序列化 WinRT 对象或您自己的 .NET 对象,您可以将其转换为 JSON 或 XML,然后将其放入 ValueSet.

If you want to serialize a WinRT object or your own .NET object, you can turn it onto JSON or XML and then put that into the ValueSet.

例如:

  public void TestValueSet()
  {
    var x = new ValueSet();

    // Integers are OK
    x.Add("a", 42);

    // URIs are not OK - can't be serialized
    try
    {
      x.Add("b", new Uri("http://bing.com"));
    }
    catch (Exception ex)
    {
      Debug.WriteLine("Can't serialize a URI - " + ex.Message);
    }

    // Custom classes are not OK
    var myClass = new MyClass { X = 42, Y = "hello" };
    try
    {
      x.Add("c", myClass);
    }
    catch (Exception ex)
    {
      Debug.WriteLine("Can't serialize custom class - " + ex.Message);
    }

    // Serialized classes are OK
    x.Add("d", Serialize<MyClass>(myClass));

    foreach (var kp in x)
    {
      Debug.WriteLine("{0} -> {1}", kp.Key, kp.Value);
    }
  }

  string Serialize<T>(T value)
  {
    var dcs = new DataContractSerializer(typeof(T));
    var stream = new MemoryStream();
    dcs.WriteObject(stream, value);
    stream.Position = 0;
    var buffer = new byte[stream.Length];
    stream.Read(buffer, 0, (int)stream.Length);
    return Encoding.UTF8.GetString(buffer);
  }
}

[DataContract]
public class MyClass
{
  [DataMember]
  public int X { get; set; }
  [DataMember]
  public string Y { get; set; }
}

这篇关于如何制作一个可以添加到 Windows.Foundation.Collections.ValueSet 的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆