Is that possible to call a function in *.xaml.cs by creating a WinMD components as a bridge? RRS feed

  • Question

  • Hi, 

    My windows store app is written by using html/js. In order to solve the customize pages for printing feature, I need to use xaml/C# (described in this document : http://msdn.microsoft.com/en-us/library/windows/apps/hh465225.aspx)

    -"Customized printing features like printing specific pages or custom print settings can only be added to Windows Store apps built for Windows using C++, C#, or Visual Basic and XAML."

    So, I need to combine the html/js and xaml/C# together.

    After investigating the print sample code by xaml/C#, I found that the Xaml page is necessary. But in my demo project, there is a crash (see below screenshot).

    Here is my code:


    <!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <meta charset="utf-8" />
        <!-- WinJS 引用 -->
        <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
        <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
        <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
        <!-- js_cs_print 引用 -->
        <link href="/css/default.css" rel="stylesheet" />
        <script src="/js/default.js"></script>
        <button id="btn_print">print</button>
        <div id="output"></div>


    // http://go.microsoft.com/fwlink/?LinkId=232509
    (function () {
        "use strict";
        WinJS.Binding.optimizeBindingReferences = true;
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    var btn_print = document.getElementById('btn_print');
                    btn_print.onclick = function () {
                        document.getElementById('output').innerHTML = print.PrintUtil.print();
                } else {
        app.oncheckpoint = function (args) {


            <Canvas x:Name="printingRoot" Opacity="0"/>


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    namespace print
        public sealed partial class mainpage : Page
            public mainpage()
            public bool registerForPrint()
                return true;
            protected override void OnNavigatedTo(NavigationEventArgs e)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Graphics.Printing;
    using Windows.UI.Xaml.Printing;
    namespace print
        public sealed class PrintUtil
            public static bool print() 
                var print = new print.mainpage();
                return print.registerForPrint();

    Sun Bin

    Tuesday, December 25, 2012 8:41 AM


  • Hi,

    I think you can't do that. But you could create Windows Runtime Components. By using the Windows Runtime, you can create components (essentially DLLs) in C++, C#, or Visual Basic, and call into them in a simple and natural way from a Windows Store app that's built by using JavaScript. Although you can use JavaScript to access the Windows Runtime APIs, JavaScript cannot easily access native C++ libraries or .NET Framework class library functionality. For example, you might create a component in C++ that uses a third-party library to perform some computationally expensive operations. Or you might want to reuse some Visual Basic or C# code in your Windows Store app built for Windows using JavaScript. In either case, you can use the language of your choice to wrap that functionality in a Windows Runtime Component. Visual Studio features can help you create the component project and reference it from JavaScript.

    #Creating Windows Runtime Components


    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bin.Sun Wednesday, December 26, 2012 3:29 AM
    Wednesday, December 26, 2012 1:54 AM