{"id":90,"date":"2019-12-13T10:24:32","date_gmt":"2019-12-13T01:24:32","guid":{"rendered":"http:\/\/sumomo.ohwaki.jp\/wordpress\/?p=90"},"modified":"2020-02-07T10:25:16","modified_gmt":"2020-02-07T01:25:16","slug":"blazor-js%e3%81%8b%e3%82%89c%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=90","title":{"rendered":"blazor JS\u304b\u3089C#\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057"},"content":{"rendered":"\n<p>asp.net core blazor\u3067Javascript\u304b\u3089C#\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u65b9\u6cd5\u3092MS\u306e\u30b5\u30a4\u30c8\u3084\u3001\u4ed6\u306e\u30b5\u30a4\u30c8\u3067\u691c\u7d22\u3057\u305f\u7d50\u679c\u3001static\u30e1\u30bd\u30c3\u30c9\u3084\u4ed6\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u66f8\u3044\u3066\u3042\u3063\u305f\u304c\u3001\u30da\u30fc\u30b8\u30af\u30e9\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u547c\u51fa\u3057\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3042\u308b\u30b5\u30a4\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u3002<\/p>\n\n\n\n<p>\u30da\u30fc\u30b8\u30af\u30e9\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3079\u308c\u3070\u3001\u30af\u30e9\u30b9\u5909\u6570\u304c\u305d\u306e\u307e\u307e\u4f7f\u7528\u3067\u304d\u308b\u306e\u3067\u3001\u5909\u6570\u3068\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3066\u3044\u308bHTML\u30a8\u30ec\u30e1\u30f3\u30c8\u5024\u3092\u76f4\u63a5\u3044\u3058\u308c\u308b\u306e\u3067\u4fbf\u5229\u3060\u3068\u601d\u3044\u3001\u8272\u3005\u8a66\u3057\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<p>\u30b5\u30a4\u30c8\u306b\u66f8\u304b\u308c\u3066\u3044\u305f\u8aac\u660e\u3067\u306f\u3001\u547c\u51fa\u3055\u308c\u308bC#\u30e1\u30bd\u30c3\u30c9\u306b\u306f[JSInvokable]\u5c5e\u6027\u3092\u3064\u3051\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\u3053\u308c\u306f\u3069\u306e\u30b5\u30a4\u30c8\u3067\u3082\u540c\u3058\u3053\u3068\u304c\u66f8\u304b\u308c\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>static\u30e1\u30bd\u30c3\u30c9\u306e\u5834\u5408\u306f\u3001 \u4e0b\u8a18\u306e\u3088\u3046\u306a\u611f\u3058\u3067Javascript\u304b\u3089\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3002<\/p>\n\n\n\n<pre><code>Dotnet.invokeMethod(&lt;\u30a2\u30d7\u30ea\u540d>,&lt;\u30e1\u30bd\u30c3\u30c9\u540d>[,\u30d1\u30e9\u30e1\u30fc\u30bf1[,\u30d1\u30e9\u30e1\u30fc\u30bf2...]])\n\u307e\u305f\u306f\nDotnet.invokeMethodAsync(\u4e0a\u8a18\u3068\u540c\u69d8)<\/code><\/pre>\n\n\n\n<p>\u305f\u3060\u3057\u3001static\u30e1\u30bd\u30c3\u30c9\u306a\u306e\u3067\u3001\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u305f\u30da\u30fc\u30b8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306b\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u3082\u3046\u4e00\u3064\u4f8b\u304c\u8f09\u3063\u3066\u3044\u308b\u306e\u304c\u3001\u30d8\u30eb\u30d1\u30fc\u30af\u30e9\u30b9\u3092\u4ecb\u3057\u305f\u547c\u51fa\u3057\u3002<\/p>\n\n\n\n<pre><code>C#\u5074\nvar helper = new MyJsHelper();\nawait JSRuntime.InvokeVoidAsync(\"JsFunc\",\n  DotNetObjectReference.Create(helper));\n\nJS\u5074\nfunction JsFunc(helper) {\n  helper.invokeMethodAsync(\u30fb\u30fb\u30fb); \/\/ invokeMethod\u3082\u4f7f\u7528\u53ef\n}<\/code><\/pre>\n\n\n\n<p>\u3067\u3082\u3001\u540c\u3058\u3088\u3046\u306b\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u305f\u30da\u30fc\u30b8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306b\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u3088\u306d\u3002(\u3082\u3061\u308d\u3093\u3001\u30da\u30fc\u30b8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u30d8\u30eb\u30d1\u30fc\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5185\u306b\u4fdd\u5b58\u3059\u308c\u3070\u5927\u4e08\u592b\u304b\u3082\u3057\u308c\u306a\u3044\u3051\u308c\u3069\u30fb\u30fb\u30fb)<\/p>\n\n\n\n<p>\u3067\u3001\u4e0a\u8a18\u3092\u898b\u3066\u3082\u3057\u304b\u3057\u3066\u3068\u601d\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<pre><code>C#\u5074\nprivate string Mesg = \"\";\n\u30fb\u30fb\u30fb\n&#91;JSInvokable]\npublic void CSMethod(int p1, double p2, string p3) {\n  Mesg = $\"p1={p1}, p2={p2}, p3={p3}\";\n  this.StateHasChanged(); \/\/ JS\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u305f\u5834\u5408\u306f\u81ea\u52d5\u66f4\u65b0\u3055\u308c\u306a\u3044\u306e\u3067\n}\n\u30fb\u30fb\u30fb\n  await JSRuntime.InvokeVoidAsync(\"JsFunc\",\n    DotNetObjectReference.Create(<span style=\"color:red;font-weight:bold\">this<\/span>), \n    \"CSMethod\",10,3.14,\"Hello\");\n\nJS\u5074\nfunction JsFunc(helper,func,p1,p2,p3) {\n  helper.invokeMethodAsync(func,p1,p2,p3);\n}<\/code><\/pre>\n\n\n\n<p>\u898b\u4e8b\u306b\u52d5\u3044\u3066\u304f\u308c\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3067\u3001\u3053\u308c\u4f7f\u3063\u3066\u4f55\u3092\u3084\u308a\u305f\u304b\u3063\u305f\u304b\u3068\u3044\u3046\u3068\u3001BingMap\u7b49\u306e\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u3092C#\u3067\u66f8\u304d\u305f\u304b\u3063\u305f\u3060\u3051\u306a\u306e\u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u3001\u30d4\u30f3\u7b49\u3092\u8ffd\u52a0\u3057\u305f\u308a\u3059\u308b\u305f\u3081\u306bJS\u95a2\u6570\u3092\u7d30\u5207\u308c\u306b\u4f5c\u3063\u3066\u3001C#\u304b\u3089\u547c\u3073\u51fa\u3059\u3053\u3068\u306b\u306a\u308b\u3093\u3060\u3051\u3069\u306d\u30fb\u30fb\u30fb<\/p>\n","protected":false},"excerpt":{"rendered":"<p>asp.net core blazor\u3067Javascript\u304b\u3089C#\u30e1\u30bd\u30c3\u30c9\u3092\u547c &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=90\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10,9,11,4],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-blazor","category-c","category-dotnetcore","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":6,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}