WinRTError: Class not registered

丶灬走出姿态 提交于 2019-12-08 10:43:46

问题


I'm new in TypeScript. I'm getting error when trying to instantiating the class. Below is my sample code, actual code is different can't share.

 module ABC {
    export class A {    
       public execute<T>(action: string, data?: any, callerContext?: any): IAsyncResult<T> {
        // CODE::   
        var requestMessage = new Common.ClientClasses.ClientRequestMessage(); **// **ERROR- "WinRTError: Class not registered"****
        requestMessage.requestUri = actionRequest;
        requestMessage.method = "POST";
        requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
        Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
         .done((result: Common.ClientClasses.ClientResponeMessage) => {
             // CODE:
        }
        // Code::
      }
   }
}

declare module Common.ClientClasses {
    class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }

    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }

    class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any;
    }
}

回答1:


I did some improvements, should work:

module ABC {

    export class A {

        public execute<T>(action: string, data?: any, callerContext?: any) {
            var requestMessage = new Common.ClientClasses.ClientRequestMessage();
            requestMessage.method = "POST";
            requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
            Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
        }

     }
}

module Common.ClientClasses {

    export class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }

    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }

    export class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any {
            console.log("test");
        }
    }
}

Then it can be run as following:

var a = new ABC.A();
a.execute("some string");

declare module creates a definition file used for Intellisense but it doesn't provide any implementation that's why I changed your code so this fragment can work.

Also if you want to use any classes from the module, you must export them so they can be visible from outside of that module.



来源:https://stackoverflow.com/questions/30435863/winrterror-class-not-registered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!