{"id":806,"date":"2024-03-08T11:20:06","date_gmt":"2024-03-08T02:20:06","guid":{"rendered":"https:\/\/dnetcore.ohwaki.jp\/wordpress\/?p=806"},"modified":"2024-11-01T10:15:17","modified_gmt":"2024-11-01T01:15:17","slug":"maui-blazor","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=806","title":{"rendered":"MAUI Blazor"},"content":{"rendered":"\n<p>\u4ee5\u524d\u3001Blazor Hybrid\u306e\u4f8b\u3067\u3001Windows Forms\u3068Blazor\u3092\u8a66\u3057\u3066\u307f\u305f\u304c\u3001\u4eca\u56de\u306fMAUI\u3067\u8a66\u3057\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<p>MAUI\u306e\u5834\u5408\u3082\u3001Windows Forms\u3068\u540c\u69d8\u306bMAUI\uff5eBlazor\u9593\u306e\u9023\u643a\u304c\u53ef\u80fd\u3002<\/p>\n\n\n\n<p>\u5171\u6709\u3059\u308b\u3001Singleton\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u4f5c\u6210\u3057\u3001MauiProgram.cs\u5185\u3067\u30b5\u30fc\u30d3\u30b9\u767b\u9332\u3059\u308b\u3053\u3068\u3067\u3001\u76f8\u4e92\u3067\u540c\u3058\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5171\u6709\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"HOST I\/F\" data-lang=\"C#\"><code>namespace MAUIBlApp;\n\npublic interface IHostIF {\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u5171\u6709\u30c7\u30fc\u30bf\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;value&gt;\u30ab\u30a6\u30f3\u30c8\u5024&lt;\/value&gt;\n    public int Count { get; set; }\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\n    \/\/\/ &lt;\/summary&gt;\n    public event EventHandler ComponentEvent;\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u304b\u3089\u30a4\u30d9\u30f3\u30c8\u767a\u751f\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;sender&quot;&gt;&lt;\/param&gt;\n    \/\/\/ &lt;param name=&quot;e&quot;&gt;&lt;\/param&gt;\n    public void FireClientEvent(object sender, EventArgs e);\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30db\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\n    \/\/\/ &lt;\/summary&gt; &lt;summary&gt;\n    \/\/\/ \n    \/\/\/ &lt;\/summary&gt;\n    public event EventHandler HostEvent;\n    \/\/\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30db\u30b9\u30c8\u5074\u304b\u3089\u30a4\u30d9\u30f3\u30c8\u767a\u751f\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;value&gt;&lt;\/value&gt;\n    public void FireHostEvent(object sender, EventArgs e);\n}\n\/\/\/ &lt;summary&gt;\n\/\/\/ IHostIF\u306e\u5b9f\u88c5\n\/\/\/ &lt;\/summary&gt;\npublic class HostInterface : IHostIF {\n    public int Count { get; set; } = 0;\n    public event EventHandler ComponentEvent = null!;\n    public void FireClientEvent(object sender, EventArgs e) {\n        ComponentEvent?.Invoke(sender,e);\n    }\n    public event EventHandler HostEvent = null!;\n    public void FireHostEvent(object sender, EventArgs e) {\n        HostEvent?.Invoke(sender,e);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"MauiProgram.cs\" data-lang=\"C#\"><code>using Microsoft.Extensions.Logging;\n\nnamespace MAUIBlApp;\n\npublic static class MauiProgram\n{\n\tpublic static MauiApp CreateMauiApp()\n\t{\n\t\tvar builder = MauiApp.CreateBuilder();\n\t\tbuilder\n\t\t\t.UseMauiApp&lt;App&gt;()\n\t\t\t.ConfigureFonts(fonts =&gt;\n\t\t\t{\n\t\t\t\tfonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);\n\t\t\t});\n\n\t\tHostInterface hst = new HostInterface();\n\t\tbuilder.Services.AddSingleton&lt;IHostIF&gt;(hst);\n\t\tbuilder.Services.AddMauiBlazorWebView();\n\n\u30fb\u30fb\u30fb<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"App.xaml.cs\" data-lang=\"C#\"><code>namespace MAUIBlApp;\n\npublic partial class App : Application\n{\n\tpublic App(IHostIF hif)\n\t{\n\t\tInitializeComponent();\n\t\t\/\/ MainPage\u306e\u30ed\u30fc\u30c9(IHostIF(Singleton)\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3059)\n\t\tMainPage = new MainPage(hif);\n\t}\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"MainPage.xaml.cs\" data-lang=\"C#\"><code>namespace MAUIBlApp;\n\npublic partial class MainPage : ContentPage\n{\n\tprivate readonly IHostIF _hostif;\n\t\/\/\/ &lt;summary&gt;\n\t\/\/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf(IHostIF\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97)\n\t\/\/\/ &lt;\/summary&gt;\n\t\/\/\/ &lt;param name=&quot;hostif&quot;&gt;&lt;\/param&gt;\n\tpublic MainPage(IHostIF hostif)\n\t{\n\t\tInitializeComponent();\n\t\t_hostif = hostif;\n\t\t\/\/ Client(Blazor Component)\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u8a2d\u5b9a\n\t\t_hostif.ComponentEvent += OnClientEvent;\n\t\tlblNumber.Text = _hostif.Count.ToString();\n\t}\n\t\/\/\/ &lt;summary&gt;\n\t\/\/\/ \u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af\u6642\u51e6\u7406\n\t\/\/\/ &lt;\/summary&gt;\n\t\/\/\/ &lt;param name=&quot;sender&quot;&gt;&lt;\/param&gt;\n\t\/\/\/ &lt;param name=&quot;e&quot;&gt;&lt;\/param&gt;\n\tpublic void OnBtnClick(object sender, EventArgs e) {\n\t\t\/\/ \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\n\t\t_hostif.Count++;\n\t\tlblNumber.Text = _hostif.Count.ToString();\n\t\t\/\/ Client(Blazor Component)\u3078\u30a4\u30d9\u30f3\u30c8\u901a\u77e5\n\t\t_hostif.FireHostEvent(sender,e);\n\t}\n\t\/\/\/ &lt;summary&gt;\n\t\/\/\/ Client(Blazor Component)\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\n\t\/\/\/ &lt;\/summary&gt;\n\t\/\/\/ &lt;param name=&quot;sender&quot;&gt;&lt;\/param&gt;\n\t\/\/\/ &lt;param name=&quot;e&quot;&gt;&lt;\/param&gt;\n\tpublic void OnClientEvent(object? sender, EventArgs e) {\n\t\tlblNumber.Text = _hostif.Count.ToString();\n\t}\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-razor\" data-file=\"Blazor\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\" data-lang=\"RazorPage\"><code>@page &quot;\/&quot;\n\n@inject IHostIF HostInterface\n\n&lt;h2&gt;Counter&lt;\/h2&gt;\n\n&lt;p role=&quot;status&quot;&gt;Current count:\n@if (Numbers != null && Numbers.Count != 0) {\n    foreach(var v in Numbers) {\n        string imgname = $&quot;\/images\/number_{v}.png&quot;;\n        &lt;img src=&quot;@imgname&quot; style=&quot;width:42px;height:49px&quot;\/&gt;\n    }\n}\n&lt;\/p&gt;\n&lt;br\/&gt;\n\n&lt;button class=&quot;btn btn-primary&quot; @onclick=&quot;IncrementCount&quot;&gt;Click me&lt;\/button&gt;\n\n@code {\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30ab\u30a6\u30f3\u30bf\u306e\u30a4\u30e1\u30fc\u30b8Index\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n    protected List&lt;int&gt; Numbers = new () {0};\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Initialize\n    \/\/\/ &lt;\/summary&gt;\n    protected override void OnInitialized() {\n        \/\/ Host\u30a4\u30d9\u30f3\u30c8\u306e\u30cf\u30f3\u30c9\u30e9\u8a2d\u5b9a\n        HostInterface.HostEvent += async (_,_) =&gt; await InvokeAsync(()=&gt;{CreateNumberImages();StateHasChanged();});\n    }\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30dc\u30bf\u30f3\u30a4\u30d9\u30f3\u30c8\n    \/\/\/ &lt;\/summary&gt;\n    private void IncrementCount()\n    {\n        \/\/ Interface\u306e\u30ab\u30a6\u30f3\u30c8\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\n        HostInterface.Count++;\n        \/\/ \u30ab\u30a6\u30f3\u30bf\u21d2\u30a4\u30e1\u30fc\u30b8\u756a\u53f7\u306e\u4f5c\u6210\n        CreateNumberImages();\n        \/\/ \u30db\u30b9\u30c8\u306b\u901a\u77e5\n        HostInterface.FireClientEvent(this,new EventArgs());\n    }\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u30a4\u30e1\u30fc\u30b8\u756a\u53f7\u306e\u751f\u6210\n    \/\/\/ &lt;\/summary&gt;\n    private void CreateNumberImages() {\n        Numbers = new();\n        string s = HostInterface.Count.ToString();\n        for(int i=0;i &lt; s.Length; i++) {\n            Numbers.Add(Convert.ToInt32(s[i].ToString()));\n        }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001MAUI\u5074\u304b\u3089\u3067\u3082Blazor\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5074\u304b\u3089\u3067\u3082\u540c\u3058\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53c2\u7167\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/dnetcore.ohwaki.jp\/wordpress\/wp-content\/uploads\/2024\/03\/MAUI-Blazor.mp4\"><\/video><\/figure>\n\n\n\n<p>\u6700\u521d\u3001MainPage\u306e\u547c\u51fa\u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u3044\u3066\u60a9\u3093\u3060\u304c\u3001\u3088\u304f\u3088\u304f\u30a8\u30e9\u30fc\u3092\u898b\u308b\u3068\u3001MainPage.xaml.cs\u3067\u306f\u7121\u304f\u3001App.xaml.cs\u3067MainPage\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3057\u3066\u3044\u306a\u3044\u30a8\u30e9\u30fc\u3060\u3063\u305f\u3002<\/p>\n\n\n\n<p>App\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u6307\u5b9a\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u30b5\u30fc\u30d3\u30b9\u767b\u9332\u3055\u308c\u305fSingleton\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3067\u304d\u308b\u306e\u3067\u3001\u3053\u308c\u3092MainPage\u4f5c\u6210\u6642\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u6e21\u3059\u3053\u3068\u3067\u89e3\u6c7a\u3067\u304d\u305f\u3002<\/p>\n\n\n\n<p>\u203bMAUI\u3092VS Code\u3067\u4f5c\u6210\u3059\u308b\u306a\u3089\u300c.NET MAUI\u62e1\u5f35\u300d\u3092\u5165\u308c\u3066\u304a\u304f\u3068\u4fbf\u5229\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u3001Blazor Hybrid\u306e\u4f8b\u3067\u3001Windows Forms\u3068Blazor &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=806\">\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":[18,10,9,33,4],"tags":[],"class_list":["post-806","post","type-post","status-publish","format-standard","hentry","category-net","category-blazor","category-c","category-maui","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/806","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=806"}],"version-history":[{"count":7,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":857,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/806\/revisions\/857"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}