locked
Talking to C# based WinRT components that contain UI features like XAML based user controls from a JavaScript based Windows Store app

    Question

  • Can you create a WinRT component written in C# that consists of a library of XAML based controls and then used those controls within a Windows Store app using WinJS or via a JS Cordova plugin for Windows 8.1 & WP8.1? 

    Is this possible or can you only use a C# based WinRT component that contains a library of XAML controls in XAML/C# based Windows Store apps? 

    My understanding is that a C# based WinRT component containing non-UI code (i.e. no XAML controls) can be used in a Windows Store app written C#, VB.NET, JavaScript or VC++, but not sure if that principle would work with XAML controls wrapped via WinRT types. In theory a XAML control is a C# class, so could work, but not sure how would JS code read that and then launch for example a page or user control from/into its HTML based UI?

    Sandip Ahluwalia

    Thursday, February 05, 2015 5:17 PM

Answers

  • No, it's not possible to mix Xaml controls into an HTML app.

    The Xaml controls rely on the Xaml hosting environment which isn't present in an HTML app. (Not that it makes a difference here, but Xaml controls aren't C# classes: they are native Windows Runtime classes)

    Thursday, February 05, 2015 5:27 PM
    Owner
  • Right. The code in your app is C# code, but the actual Xaml controls (like the rest of the Windows Runtime) are native code. They are projected into each language (C#, VB, C++ in the case of Windows.UI.Xaml) in a way that appears natural for that language and the interop layer is hidden.

    You could expose a Xaml control in a Windows Runtime Component written in any of C#, VB, and C++ to an app written in any of C#, VB, and C++, and the control would appear as a normal C#, VB, or C++ runtime class. This won't work for Xaml controls in JavaScript, since the HTML window doesn't have the Xaml hosting infrastructure, but for non-UI code you can write a WRC in C#, VB, or C++ and it will appear as a normal JavaScript class to a JavaScript app.

    Thursday, February 05, 2015 9:55 PM
    Owner

All replies

  • No, it's not possible to mix Xaml controls into an HTML app.

    The Xaml controls rely on the Xaml hosting environment which isn't present in an HTML app. (Not that it makes a difference here, but Xaml controls aren't C# classes: they are native Windows Runtime classes)

    Thursday, February 05, 2015 5:27 PM
    Owner
  • Thanks for the answer. That does indeed make sense.

    I meant when writing a custom Page, ChildWindow or UserControl - the code behind for those XAML controls are partial C# classes that derive from the WinRT classes. So the XAML control is just a class, which I thought can be returned as some sort of WinRT type via the public sealed class interface of a C# based WinRT component. If the Windows Store app was written in XAML/C# then I guess the UserControl from the WinRT component can be used as normal.


    Sandip Ahluwalia


    Thursday, February 05, 2015 5:54 PM
  • Right. The code in your app is C# code, but the actual Xaml controls (like the rest of the Windows Runtime) are native code. They are projected into each language (C#, VB, C++ in the case of Windows.UI.Xaml) in a way that appears natural for that language and the interop layer is hidden.

    You could expose a Xaml control in a Windows Runtime Component written in any of C#, VB, and C++ to an app written in any of C#, VB, and C++, and the control would appear as a normal C#, VB, or C++ runtime class. This won't work for Xaml controls in JavaScript, since the HTML window doesn't have the Xaml hosting infrastructure, but for non-UI code you can write a WRC in C#, VB, or C++ and it will appear as a normal JavaScript class to a JavaScript app.

    Thursday, February 05, 2015 9:55 PM
    Owner