Partilhar via


关于JavaScript调用Windows RT 组件要注意的问题

我们知道通过Visual Studio 2012可以创建C#,VB和C++语言的Windows RT组件,供其他Windows Store的应用来调用。但有一点要注意就是JavaScript编写的Store应用Reference Win RT组件的Property和Method时,首字母必须是小些的。不论在Win RT组件定义时大小写如何。

比如有个 C#编写的WinRT组件类申明如下:-

-----------------------------------------------------------------------------------------------------------------------------------------------------

 public sealed class Class1
    {
        public string AppName
        {
            get
            {
                return "My Hybrid App  12";
            }
        }

        public static IList<Group> GetGroups()
        {
            List<Group> groups = new List<Group>();

            groups.Add(new Group() { Name = "My Hybrid Group 1", Count = 3 });
            groups.Add(new Group() { Name = "My Hybrid Group 2", Count = 5 });

            return groups;
        }
    }

---------------------------------------------------------------------------------------------------------------------------------------------------------

而在JavaScript中调用时时,AppName和GetGroups都必须是小写。比如

------------------------------------------------------------------------------------------------------------------------------------------------------------

// TODO: 3 - Populating page title from C# class.
            element.querySelector(".titlearea .pagetitle").textContent = new CSWinRTLib.Class1().appName;

var myGroups = CSWinRTLib.Class1.getGroups();

-------------------------------------------------------------------------------------------------------------------------------------------------------------

这点常常容易被忽略,附件是包含上述代码的一个完整例子,供大家参考。

 

 

Demo5_UsingWinRTComponents.zip