Code Monkey home page Code Monkey logo

maui_chatgpt's Introduction

学习maui-windows

  • maui-webview
  • maui-桌面角标
  • maui-消息通知

image

image

image

image

js call csharp

基于promise的封装

// 调用csharp的方法
class CsharpMethod {
    constructor(command, data) {
        this.RequestPrefix = "request_csharp_";
        this.ResponsePrefix = "response_csharp_";
        // 唯一
        this.dataId = this.RequestPrefix + new Date().getTime();
        // 调用csharp的命令
        this.command = command;
        // 参数
        this.data = { command: command, data: !data ? '' : JSON.stringify(data), key: this.dataId }
    }

    // 调用csharp 返回promise
    call() {
        // 把data存储到localstorage中 目的是让csharp端获取参数
        localStorage.setItem(this.dataId, this.utf8_to_b64(JSON.stringify(this.data)));
        let eventKey = this.dataId.replace(this.RequestPrefix, this.ResponsePrefix);
        let that = this;
        var promise = new Promise(function (resolve, reject) {
            var eventHandler = function (e) {
                window.removeEventListener(eventKey, eventHandler);
                let resp = e.newValue;
                if (resp) {
                    // 从base64转换
                    let realData = that.b64_to_utf8(resp);
                    if (realData.startsWith('err:')) {
                        reject(realData.substr(4));
                    } else {
                        try {
                            let rspData = JSON.parse(realData)
                            resolve(rspData);
                        } catch (e) {
                            resolve(realData);
                        }
                    }
                } else {
                    reject("unknow error : " + eventKey);
                }
            }
            // 注册监听回调
            window.addEventListener(eventKey, eventHandler);
        });
        // 改变location 发送给csharp端
        window.location = "/api/" + this.dataId;
        return promise;
    }

    // 转成base64 解决中文乱码
    utf8_to_b64(str) {
        return window.btoa(unescape(encodeURIComponent(str)));
    }
    // 从base64转过来 解决中文乱码
    b64_to_utf8(str) {
        return decodeURIComponent(escape(window.atob(str)));
    }

}

使用方法:

const testMethod = new CsharpMethod("test", { name: "yuzd", time: new Date().getTime() });
testMethod.call()
    .then(function (data) {
        alert(data);
    }).catch(function (err) {
        alert(err);
    });

maui_chatgpt's People

Contributors

yuzd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.