LoginSignup
1
0

More than 1 year has passed since last update.

WebView2ランタイム exists

Posted at

WebView2ランタイムの存在確認
レジストリキーを確認するタイプは端末で動きが読めず。
バージョン見るタイプ(下)はインストール・アンインストールに反応。

Browser.cs
class Browser:Form {
    double dpr;
    public Browser() {
        dpr         = DeviceDpi/96.0;
        ClientSize  = GetSize(640,360);

        //https://github.com/MicrosoftEdge/WebView2Feedback/issues/421
        var error = "";
        var version = "";
        try{
            version = CoreWebView2Environment.GetAvailableBrowserVersionString();
        }catch(WebView2RuntimeNotFoundException e) {
            error = e.Message;
        }

        if(version=="") {
            NotFound(error);
        }else{
            var view    = new WebView2(){Size=ClientSize};
            view.Source = new Uri("https://qiita.com/");
            SizeChanged += (s,e)=>view.Size=ClientSize;
            Controls.Add(view);
        }
    }
    public void NotFound(string error) {
        var label = new Label() {
            Text     = "",
            Location = GetPoint(20,80),
            Size     = GetSize(600,80),
        };
        var button = new Button() {
            Text     = "WebView2ランタイム ダウンロードページ",
            Location = GetPoint(20,20),
            Size     = GetSize(240,45),
        };
        button.Click+=(s,e) => {
            OpenBrowser("https://developer.microsoft.com/microsoft-edge/webview2/");
        };
        label.Text += "上記ページから「WebView2ランタイム」をインストールしてください。\n";
        label.Text += "WebView2 ランタイムをダウンロード > エバーグリーン ブートストラップ > 「ダウンロード」\n";
        label.Text += error;
        Controls.Add(button);
        Controls.Add(label);
    }
    public Size GetSize(int w,int h) {
        return new Size((int)(w*dpr),(int)(h*dpr));
    }
    public Point GetPoint(int x,int y) {
        return new Point((int)(x*dpr),(int)(y*dpr));
    }
    public void OpenBrowser(string s) {
        Process.Start(new ProcessStartInfo() {FileName = s,UseShellExecute = true});
    }
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0