0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macOSとWindowsのコモンパネル対応(Xamarin.Mac (cocoaアプリ) で「ファイルを開く」パネル表示方法)

Last updated at Posted at 2020-06-12

はじめに

Xamarin.MacのCocoaアプリケーションで、Windowsのコモンダイアログのような標準的パネルを取り扱えます。
Windowsとの対応、および「ファイルを開く」サンプルを記載しています。

対応

対象 Mac Win
アラート(メッセージボックス)を表示する NSAlert MessageBox
ファイルを開く NSOpenPanel OpenFileDialog
ファイルを保存する NSSavePanel SaveFileDialog
フォントを選択する NSFontPanel FontDialog
色を選択する NSColorPanel ColorDialog
PDFファイルに出力する NSPDFPanel

コード例

ViewController.cs
using System;

using AppKit;
using Foundation;

namespace sample
{
    public partial class ViewController : NSViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view.
        }

        public override NSObject RepresentedObject
        {
            get
            {
                return base.RepresentedObject;
            }
            set
            {
                base.RepresentedObject = value;
                // Update the view, if already loaded.
            }
        }

        // ボタンクリックされたら呼び出されるよう Action で紐付け
        partial void ButtonClicked(Foundation.NSObject sender)
        {
            using(var pnl = NSOpenPanel.OpenPanel)
            {
                pnl.CanChooseFiles = true;
                pnl.CanChooseDirectories = false;
                if(pnl.RunModal() == 1) // OK : 1, Cancel : 0
                {
                    var path = pnl.Urls[0].Path;
                    var alert = new NSAlert();
                    alert.AlertStyle = NSAlertStyle.Informational;
                    alert.MessageText = "選択されたファイルのパスです";
                    alert.InformativeText = $"{path}";
                    alert.RunModal();
                }
            }
        }
    }
}

表示結果の例
ss.png

おわりに

Xamarinのおかげで、WindowsのC#(.NET)アプリケーション開発者はmacOSのアプリも容易に開発できるようになりました。嬉しい時代です。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?