The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps, Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
How to pass C++/CX objects list (say Student class) to C# (wherein it needs to be shown on C# ListView UI using databinding) RRS feed

  • Question

  • The application has C++ model, but we need to use C# for UI to increase development speed. (MVVM=> M is in C++/CX a while V(View) and VM(ViewModel) in C#)

    My question is around how can we pass C++/CX objects to C# UI, example- a list of C++/CX Student  objects, which need to be shown on UI listview. For this we need to pass this C++/CX object list to C# which then displays it using data-binding.

    Tuesday, October 3, 2017 6:59 AM

All replies

  • Hello vivek2k6,

    To achieve this we have already wrote a official doc. Please see the following Walkthrough: Creating a basic Windows Runtime component in C++ and calling it from JavaScript or C#

    Note that for you in C++ the keypoint is here:To add the implementation for ComputeResult  Where it returns a Windows Runtime-compatible type across the ABI. Then in C# you can call this method and then analyze the result you get from this method. You can follow this way to achieve what you need. As you haven't mention what type you will use in your C++ code. I can only share you this general suggestion for you to start.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Azat Tazayan Wednesday, October 4, 2017 6:26 AM
    Wednesday, October 4, 2017 3:35 AM
  • Thanks for the answer.

    Just to confirm if I understood correctly:
    My App Scenario: Student info is present in C++ and UI will be created using Xaml/C# to speedup the UI development.

    The Student(C++Student) info (coming from C++ model class) needs to be shown in UI implemented in Xaml/C#.
    We need to have a C#Student class type at C# end which will provide the Windows Runtime-compatible type across the ABI, so that it can be created by any language, in my case C++/CX using ref new.
    Then we need to create a list of C#Student class objects using ref new C#Student(...params) in C++/CX, where "...params" to the constructor will come from C++Student class model.
    Then this list of student objects (created at C++/CX side) will be passed to C# method as parameter which will populate the C# StudentViewModel class object, which through binding will end up displaying on UI created using Xaml/C#.

    Is this correct?
    Wednesday, October 4, 2017 7:30 AM
  • @vivek2k6,

    Not so accurate. As you are trying to read from C++ windows runtime component, your C++ side need to provide Windows Runtime-compatible type across the ABI. May I confirm that your data is all from C++ side? You C# side is to reading and analyze the returned data from your C++ method. Checking the doc I showed, the C++ method returns a IVector<double> type object. And then in C# the code loops and read it to a List. You will do the same like this. In C# you will call the native method and get the result. Then you will get the data in C#. Maybe you are thinking the whole issue too complex. What about write a sample first based on the doc I showed? You will understand it better.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 5, 2017 9:57 AM
  • Hi Barry,

    Let me rephrase it.

    My App is in C++, and we need to use C# winRT component which will display in UI.

    App data like Student info which is in C++, needs to be passed to C# winRT component so that C# winRT component can display it using databinding.

    But to pass data to C# WinRT component, we can't directly pass C++ objects, but rather we need to wrap the C++ data into C++/CX data objects using ref new, and this C++/CX data can then be passed while calling C# winRT component methods, which then will finally be displayed.

    Is this correct?

    Regards,

    Vivek



    • Edited by vivek2k6 Friday, October 6, 2017 1:29 PM
    Friday, October 6, 2017 1:28 PM
  • @Vivek,

    If you have a C++ app why you don't write XAML directly in your C++ app? Why you need to use C# winrt component and then display in UI?

    Per my understanding your scenario should be:

    1. Your main app is a C# app

    2. Your data comes from your C++ windows runtime component.

    If you want to binding directly in your C++/CX app, just check this sample:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlBind/cpp

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 9, 2017 9:06 AM
  • @Barry,

    My app needs to be in C++ as it's drawing related app which uses many C++ graphics library.

    The need to use C# for UI is to speed-up development for UI. For this we will create C# WinRT component which can be used by C++ App project using C++/CX.

    Can you please confirm my last comment if my understanding is correct?

    Regards,

    Vivek


    • Edited by vivek2k6 Thursday, October 12, 2017 10:58 AM
    Tuesday, October 10, 2017 7:23 AM
  • @Vivek,

    No I cannot confirm that for you. You are writing a C++/CX UWP project but you want to use C# for UI. The problem is that your main app is already a C++/CX app, in that way I have no idea what you want to do and I cannot share you any advice based on your scenario. If you want to speed-up development, my suggestion is to keep C# as your main project and use all your C++ logic in your windows runtime component.

    Best regards,

    Barry 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 7:03 AM