「Xamarin.Mac/NSViewでドラッグアンドドロップを受け付ける」の版間の差分
ページの作成:「MonoMacにおいて、NSView(およびその派生クラス)でFinderからのファイルのドラッグ&ドロップを受け付けれる方法を模索中。...」 |
imported>Administrator 編集の要約なし |
||
| (同じ利用者による、間の1版が非表示) | |||
| 1行目: | 1行目: | ||
[[ | [[Xamarin.Mac]]において、NSView(およびその派生クラス)でFinderからのファイルのドラッグ&ドロップを受け付けれる方法を模索中。 | ||
==実装1== | == 実装1 == | ||
Finderからのファイルのドラッグ&ドロップを受け付け入れるにはNSView(またはその派生クラス)を継承し、処理を追加してやる必要がある。 | Finderからのファイルのドラッグ&ドロップを受け付け入れるにはNSView(またはその派生クラス)を継承し、処理を追加してやる必要がある。 | ||
=== 受付登録 === | |||
まず、RegisterForDraggedTypesメソッドでドロップを許可する種類を指定して受付を開始する。 | |||
これでドラッグを受け付けるようになる。 | |||
NSPasteboardには様々なタイプが用意されている。 | |||
<source lang="csharp"> | <source lang="csharp"> | ||
void Initialize() | |||
{ | |||
this.RegisterForDraggedTypes(new string[]{ | |||
NSPasteboard.NSFilenamesType | |||
}); | |||
} | |||
</source> | |||
=== 受入処理 === | |||
public class | 以下の2つのドラッグ・アンド・ドロップ受入処理を実装する。 | ||
* DraggingEntered - 受け入れ可能かを返す | |||
*: DraggingEnteredメソッドで許可されたものだけがPerformDragOperationメソッドに飛んでくる。 | |||
* PerformDragOperation - 受け入れ時の処理を実装する | |||
*: このメソッドが未実装だとアプリごと落ちる。 | |||
=== 全体像 === | |||
<source lang="csharp"> | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using Foundation; | |||
using AppKit; | |||
namespace XamarinMacExamples | |||
{ | |||
public partial class SampleView : AppKit.NSView | |||
{ | { | ||
public | #region Constructors | ||
// Called when created from unmanaged code | |||
public SampleView(IntPtr handle) : base(handle) | |||
{ | { | ||
Initialize(); | Initialize(); | ||
} | } | ||
public | // Called when created directly from a XIB file | ||
[Export("initWithCoder:")] | |||
public SampleView(NSCoder coder) : base(coder) | |||
{ | { | ||
Initialize(); | Initialize(); | ||
} | } | ||
// Shared initialization code | |||
void Initialize() | void Initialize() | ||
{ | { | ||
| 34行目: | 57行目: | ||
// ※※※ これ重要 ※※※ | // ※※※ これ重要 ※※※ | ||
// | // | ||
RegisterForDraggedTypes(new string[]{ | // ドラッグ・アンド・ドロップの受付を開始する | ||
} | // NSPasteboardには他にもいろいろなタイプがあるよ! | ||
this.RegisterForDraggedTypes(new string[]{ | |||
NSPasteboard.NSFilenamesType | |||
}); | |||
} | |||
public override NSDragOperation DraggingEntered(NSDraggingInfo sender) | public override NSDragOperation DraggingEntered(NSDraggingInfo sender) | ||
{ | { | ||
Console.WriteLine("DraggingEntered"); | |||
var pasteboard = sender.DraggingPasteboard; | |||
if (pasteboard.Types.Contains("NSFilenamesPboardType")) | |||
{ | |||
return NSDragOperation.Link; | |||
} | |||
return NSDragOperation.None; | |||
} | } | ||
public override bool PerformDragOperation(NSDraggingInfo sender) | public override bool PerformDragOperation(NSDraggingInfo sender) | ||
{ | { | ||
Console.WriteLine("PerformDragOperation"); | |||
var pasteboard = sender.DraggingPasteboard; | |||
if (pasteboard.Types.Contains("NSFilenamesPboardType")) | |||
{ | |||
foreach (var item in pasteboard.PasteboardItems) | |||
{ | |||
Console.WriteLine(item.GetStringForType("public.file-url")); | |||
} | |||
return true; | |||
} | |||
return false; | |||
} | } | ||
} | } | ||
} | |||
</source> | </source> | ||
== | == 関連項目 == | ||
* [[Interface Builder/基本的な使い方]] | |||
* | * [[Xamarin.Mac/マウスの中ボタン(ホイール)のクリックを取得する]] | ||
* [[Xamarin.Mac/ウインドウ座標をビュー座標に変換する]] | |||
[[ | |||
*[[ | |||
==参考文献== | == 参考文献 == | ||
{{reflist}} | {{reflist}} | ||
{{stub}} | {{stub}} | ||
[[category:MonoMac]] | [[category: MonoMac]] | ||
[[category: Xamarin.Mac]] | |||