差分

ナビゲーションに移動 検索に移動

Xamarin.Macで通知センターを使用する

1,240 バイト除去, 2017年11月17日 (金) 02:15
編集の要約なし
[[Xamarin.Mac]]でも[[NLog]]を使えると捗る。
==実装1==[[Xamarin.MacmacOS]]でもの標準設定では3分以内に同じ通知を繰り返し送信するとポップアップ表示されない(右上の通知アイコンをクリックすると一覧にはいる)。これを知らず下記の[[NLogソースコード]]はなんら問題なく使用できる。のAPIの呼び方が間違っていてポップアップ通知がされない[[NuGetバグ]]から一発だと思って[[インストールググって]]である。試してはいないが[[MonoMac]]でも同じだと思う。しまった。<source lang="csharp"> var userNotifycationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;
ただし[[ファイル]]に出力する場合に注意が必要で、[[Mac OS X]]ではあらゆる[[ファイル]]は[[アプリ]]の外部に保存する必要があるため、NLog // 通知がクリックされたとき userNotifycationCenter.configファイルの設定例でよく見かける「basedir変数」以下に書き込む設定は使えない。DidActivateNotification += (sender, e) => { Console.WriteLine("DidActivateNotification: " + e.Notification); };
// 通知されたとき userNotifycationCenter.DidDeliverNotification +=(sender, e) ==ファイルに出力するNLog> { Console.configの設定例===WriteLine("DidDeliverNotification");そこで以下の例では[[Mac OS X]]では定番の「~/Library/Logs/」以下に出力してみた。 };
<source lang // create var userNotification ="xml">new NSUserNotification();<?xml version="1 userNotification.0" encodingTitle ="utf-8タイトル" ?>;<nlog xmlns="http://www.nlog-project.org/schemas/NLog userNotification.xsd" xmlns:xsiSubtitle ="http://www.w3.org/2001/XMLSchema-instanceサブタイトル"; xsi:schemaLocation="http://www userNotification.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReloadInformativeText ="true本文"; throwExceptions userNotification.UserInfo =NSDictionary.FromObjectsAndKeys(new[] { "false" internalLogLevel=val"Off}, new[] { " internalLogFile="c:\temp\nlog-internal.logkey" >});
<variable name="appname" value // 10秒後に通知 userNotification.DeliveryDate ="monobook"/>NSDate.Now.AddSeconds(10);
<targets> <target xsi:type="File" name="logfile" fileName="${specialfolder:folder=MyDocuments} /Library/Logs通知実行 /${appname}/${shortdate}userNotifycationCenter.log"DeliverNotification(un); 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> userNotifycationCenter.ScheduleNotification(userNotification);
</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:MonoMac]]
匿名利用者

案内メニュー