ページ「Xamarin.MacでNLogを使う」と「Xamarin.Macで通知センターを使用する」の間の差分

提供: MonoBook
(ページ間の差分)
ナビゲーションに移動 検索に移動
imported>Administrator
(Administrator がページ「Xamarin.Mac/NLogを使う」を「Xamarin.MacでNLogを使う」に移動しました)
 
imported>Administrator
 
1行目: 1行目:
[[Xamarin.Mac]]でも[[NLog]]を使えると捗る。
 
  
==実装1==
+
[[macOS]]の標準設定では3分以内に同じ通知を繰り返し送信するとポップアップ表示されない(右上の通知アイコンをクリックすると一覧にはいる)。
[[Xamarin.Mac]]でも[[NLog]]はなんら問題なく使用できる。[[NuGet]]から一発[[インストール]]である。試してはいないが[[MonoMac]]でも同じだと思う。
+
これを知らず下記の[[ソースコード]]のAPIの呼び方が間違っていてポップアップ通知がされない[[バグ]]だと思って[[ググって]]しまった。
 +
<source lang="csharp">
 +
            var userNotifycationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;
  
ただし[[ファイル]]に出力する場合に注意が必要で、[[Mac OS X]]ではあらゆる[[ファイル]]は[[アプリ]]の外部に保存する必要があるため、NLog.configファイルの設定例でよく見かける「basedir変数」以下に書き込む設定は使えない。
+
            // 通知がクリックされたとき
 +
            userNotifycationCenter.DidActivateNotification += (sender, e) => {
 +
                Console.WriteLine("DidActivateNotification: " + e.Notification);
 +
            };
  
===ファイルに出力するNLog.configの設定例===
+
            // 通知されたとき
そこで以下の例では[[Mac OS X]]では定番の「~/Library/Logs/」以下に出力してみた。
+
            userNotifycationCenter.DidDeliverNotification += (sender, e) => {
 +
                Console.WriteLine("DidDeliverNotification");
 +
            };
  
<source lang="xml">
+
            // create
<?xml version="1.0" encoding="utf-8" ?>
+
            var userNotification = new NSUserNotification();
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
+
            userNotification.Title = "タイトル";
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
            userNotification.Subtitle = "サブタイトル";
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
+
            userNotification.InformativeText = "本文";
      autoReload="true"
+
            userNotification.UserInfo = NSDictionary.FromObjectsAndKeys(new[] { "val" }, new[] { "key" });
      throwExceptions="false"
 
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
 
  
  <variable name="appname" value="monobook"/>
+
            // 10秒後に通知
 +
            userNotification.DeliveryDate = NSDate.Now.AddSeconds(10);
  
  <targets>
+
            // 通知実行
    <target xsi:type="File" name="logfile"
+
            //userNotifycationCenter.DeliverNotification(un);
    fileName="${specialfolder:folder=MyDocuments}/Library/Logs/${appname}/${shortdate}.log"
+
            userNotifycationCenter.ScheduleNotification(userNotification);
        layout="${longdate} ${uppercase:${level}} ${message}" />
 
 
 
    <target xsi:type="Console" name="console" />
 
 
 
  </targets>
 
 
 
  <rules>
 
    <logger name="*" minlevel="Trace" writeTo="logfile" />
 
    <logger name="*" minlevel="Info"  writeTo="console" />
 
  </rules>
 
</nlog>
 
 
</source>
 
</source>
NLogに設定するパスは[[絶対パス]]なので、ホームディレクトリのパスの取得は「[[MonoMac/特殊ディレクトリのパスを取得する]]」を用いてMyDocumentsを指定することで取得した。
 
 
また、NLog.config中でアプリ名を動的に取得する方法がわからなかったので、この例では自前でappname変数を用意している。
 
 
===備考===
 
NLog.configは初期状態でビルド時にコピーされない設定になっていると思うので修正するのを忘れないこと。NLog.configファイルのプロパティの「出力ディレクトリにコピー」の値を「新しい場合のみコピー」または「常にコピー」に設定する。
 
 
===不具合===
 
これは2015年8月3日の情報です。
 
[[Xamarin.Mac]]において「Debugビルド」を行うとアプリ内(***.app内)のMonoBundleディレクトリにNLog.configファイルが生成(コピー)されるが、「Releaseビルド」では生成されないようだ。たぶん[[不具合]]だと思う。
 
 
==関連項目==
 
*[[MonoMac/特殊ディレクトリのパスを取得する]]
 
 
==参考文献==
 
{{reflist}}
 
 
{{stub}}
 
  
[[category:Xamarin.Mac]]
+
[[category: Xamarin.Mac]]
[[category:MonoMac]]
 

2017年11月17日 (金) 02:15時点における最新版

macOSの標準設定では3分以内に同じ通知を繰り返し送信するとポップアップ表示されない(右上の通知アイコンをクリックすると一覧にはいる)。 これを知らず下記のソースコードのAPIの呼び方が間違っていてポップアップ通知がされないバグだと思ってググってしまった。

            var userNotifycationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;

            // 通知がクリックされたとき
            userNotifycationCenter.DidActivateNotification += (sender, e) => {
                Console.WriteLine("DidActivateNotification: " + e.Notification);
            };

            // 通知されたとき
            userNotifycationCenter.DidDeliverNotification += (sender, e) => {
                Console.WriteLine("DidDeliverNotification");
            };

            // create
            var userNotification = new NSUserNotification();
            userNotification.Title = "タイトル";
            userNotification.Subtitle = "サブタイトル";
            userNotification.InformativeText = "本文";
            userNotification.UserInfo = NSDictionary.FromObjectsAndKeys(new[] { "val" }, new[] { "key" });

            // 10秒後に通知
            userNotification.DeliveryDate = NSDate.Now.AddSeconds(10);

            // 通知実行
            //userNotifycationCenter.DeliverNotification(un);
            userNotifycationCenter.ScheduleNotification(userNotification);