2016年3月27日日曜日

エクスプローラからのドラッグ

ページ全体でエクスプローラからのファイルドロップを受けるには。
ページの AllowDrop を true にする。
最低でも DragEnter と Drop のイベント処理を行う。

DragEnter では ドラッグされてきたデータの種別をチェックし StorageItems であればドロップを受け付けるようにする。
private void Page_DragEnter(object sender, DragEventArgs e)
{
 if (e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems)) {
  e.DragUIOverride.Caption = "File Open";
  e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
  e.Handled = true;
 }
}

Drop イベントでドロップされたファイルの処理を行う。
private async void Page_Drop(object sender, DragEventArgs e)
{
 var d = e.GetDeferral();

 try {
  var files = await e.DataView.GetStorageItemsAsync();
  var file = files.First();

  Debug.WriteLine(file.Path);
 }
 catch (Exception){ }

 d.Complete();
}

2016年2月17日水曜日

ファイルを指定する(FileOpenPicker)

ボタンハンドラで FileOpenPicker を使用してファイルを指定する。


  private async void OpenFile_Click(object sender, RoutedEventArgs e)
  {
   FileOpenPicker openPicker = new FileOpenPicker();

   // 表示モードはリスト形式
   openPicker.ViewMode = PickerViewMode.List;
   // デフォルトフォルダ
   //openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
   // 拡張子
   openPicker.FileTypeFilter.Add("*");


   // ファイルオープンピッカーを起動する
   StorageFile file = await openPicker.PickSingleFileAsync();
   if (file != null) {
    // ファイル処理
   }
  }

2016年2月11日木曜日

デバッグ時に表示されている左上のカウンタを消す

ここに表示されているのはフレームレートとCPU使用量。
これは、デフォルトで App クラスの OnLaunched メンバの最初で表示されるように設定されている。

if (System.Diagnostics.Debugger.IsAttached)
{
    this.DebugSettings.EnableFrameRateCounter = true;
}
これを false にするか、コメントアウトすればよい。

2016年1月26日火曜日

ContentDialog を自分自身で閉じる

Primary/Secondary ボタン以外で自分自身を閉じるには、Hide メソッドを使用する。

例えばListBoxのダブルタップで Primary ボタンが押された時と同等にするには
ダブルタップのイベントハンドラで次のように記述する。

private void listFileName_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
 if (listFileName.SelectedIndex != -1) {
  ContentDialog_PrimaryButtonClick(null, null);
  Hide();
 }
}