【问题标题】:Transporting and deserialization of Point structure between WPF and XAML在 WPF 和 XAML 之间传输和反序列化点结构
【发布时间】:2014-08-07 14:37:42
【问题描述】:

假设我有 WPF 应用程序和 Windows 8 Store 应用程序,我想在它们之间交换一些数据。所以我需要对它们进行序列化和反序列化,但我目前在我的 DTO 对象中反序列化 Point 结构时遇到问题:

(Windows 8 商店应用定义)

using Windows.Foundation;

namespace DTO
{
    public class ImageMessageDTO : BaseDTO
    {
        public virtual Point ProblematicPoint { get; set; }
    }
}

(WPF 定义)

using System.Windows;

namespace DTO
{
    public class ImageMessageDTO : BaseDTO
    {
        public virtual Point ProblematicPoint { get; set; }
    }
}

如您所见,DTO 对象是相同的,除了在不同平台上定义 Point 的命名空间。

在反序列化为 WPF DTO 时,出现以下错误:

附加信息:第 1 行位置 256 出错。“EndElement” 命名空间中的“问题点” 'http://schemas.datacontract.org/2004/07/DTO' 预计不会。期待元素“_x”。

我应该如何序列化和反序列化对象?

【问题讨论】:

  • 是xml序列化还是数据合约序列化?可以添加序列化对象的xml吗?

标签: c# .net wpf xaml serialization


【解决方案1】:

您的类显然相等,因为它们是在不同的程序集中定义的。序列化程序永远不会将这些单独的类识别为一个单元。

这就是 Portable Class Libraries 的创建目的。

将 DTO 放入 PCL,然后从 WPF 项目和 WinRT 项目中引用该库:

- Solution MyApplication
   |--> MyApplication.Model (Portable Class library)
   |      |--> - ImageMessageDTO
   |
   |--> MyApplication.WPF (WPF Application)
   |      |--> References
   |            |--> MyApplication.Model
   |
   |--> MyApplication.WinRT (WinRT Application)
          |--> References
                |--> MyApplication.Model

【讨论】:

  • 我也考虑过这个选项,但在 PCL 中不支持 Point(以及我在不同 DTO 类中的其他属性,例如 BitmapSource)。你会怎么解决这个问题?
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2019-01-31
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
相关资源
最近更新 更多