【问题标题】:WinRT C++ IMap returning Windows Runtime Object instead of IDictionary into C#WinRT C++ IMap 将 Windows 运行时对象而不是 IDictionary 返回到 C#
【发布时间】:2015-05-30 04:41:45
【问题描述】:

我们的 C# Windows Store 应用程序的 C++ 后端库存在问题。

在 C++ 端,我们有一个这样定义的属性:

property IMap<String^, IMap<String^, String^>^ >^   myVariable;

在 C# 方面,该属性被正确转换为:

public IDictionary<string, IDictionary<string, string>> myVariable { get; set; }

情况:在调试时(设置断点),您可以看到从 C++ 返回的对象是 Windows 运行时对象,而不是预期的字典。我可以遍历返回的对象,并且foreach循环中的代码执行成功。

问题:如果我删除断点(但我仍在调试中),返回的对象没有任何信息。它不为空,因为我没有任何异常,但它是空的(foreach 循环不执行——未到达内部断点)。

更多信息:

  • 字典本身是更大对象的一部分;
  • 其余属性包含信息;
  • 在 C++ Windows Store 应用程序中,字典 (IMap) 正确显示(因此问题可能来自 C++ 和 C# 之间的链接);
  • 我使用异步任务获取后端对象:

BiggerObject myObject = await Task.Factory.StartNew(() => { return _clientInstance.GetMyObject(key); });

您是否知道为什么在调试时无法正确识别字典以及为什么不在调试时它是空的?

感谢您的回答!

问候, 艾诺特

【问题讨论】:

    标签: c# windows-store-apps .net-4.5 c++-cx winrt-async


    【解决方案1】:

    这对我来说很好。

    简单的 C++ 代码:

    标题

    public ref class MapOfMapsTest sealed
    {
    public:
      MapOfMapsTest();
      property IMap<String^, IMap<String^, String^>^>^ Stuff;
    };
    

    实施

    MapOfMapsTest::MapOfMapsTest()
    {
      auto outerMap = ref new Platform::Collections::Map<String^, IMap<String^, String^>^>();
      auto innerMap = ref new Platform::Collections::Map<String^, String^>();
      innerMap->Insert(L"1", L"one");
      innerMap->Insert(L"3", L"three");
      outerMap->Insert(L"odd", innerMap);
    
      innerMap = ref new Platform::Collections::Map<String^, String^>();
      innerMap->Insert(L"2", L"two");
      innerMap->Insert(L"4", L"four");
      outerMap->Insert(L"even", innerMap);
    
      Stuff = outerMap;
    }
    

    简单的 C# 代码(其中 lv 是在 XAML 中声明的 ListView

    private void DoMapThing()
    {
      var test = new MapOfMapsTest();
      foreach (var k in test.Stuff.Keys)
      {
        var innerDict = test.Stuff[k];
        foreach (var k2 in innerDict.Keys)
          lv.Items.Add(String.Format("{0} -> {1} : {2}", k, k2, innerDict[k2]));
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 2011-02-06
      • 2012-08-17
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      相关资源
      最近更新 更多