R3b*_*0oT 5 c# android file observers xamarin
我需要在Xamarin c#(Android)中创建文件观察器的指导
某种可行的例子会很精彩!
我试图将java转换为C#,但由于我在C#环境中缺乏经验,它在编译时会抛出太多错误..并且在C#和java中获取写入引用代码会让人感到恼火.
那么请!可能有人在那里指出我某种可行的方法
这是文件观察者https://gist.github.com/shirou/659180的java示例
Sus*_*ver 12
创建一个继承自的类Android.OS.FileObserver,您只需要实现OnEvent()一个(+)构造函数.你看到它之后它是一个非常简单的模式... ;-)
笔记:
FileObserver类:
using System;
using Android.OS;
using Android.Util;
namespace MyFileObserver
{
public class MyPathObserver : Android.OS.FileObserver
{
static FileObserverEvents _Events = (FileObserverEvents.AllEvents);
const string tag = "StackoverFlow";
public MyPathObserver (String rootPath) : base(rootPath, _Events)
{
Log.Info(tag, String.Format("Watching : {0}", rootPath));
}
public MyPathObserver (String rootPath, FileObserverEvents events) : base(rootPath, events)
{
Log.Info(tag, String.Format("Watching : {0} : {1}", rootPath, events));
}
public override void OnEvent(FileObserverEvents e, String path)
{
Log.Info(tag, String.Format("{0}:{1}",path, e));
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
var pathToWatch = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
// Do not let myFileObserver get GC'd, stash it's ref in an activty, or ...
myFileObserver = new MyPathObserver (pathToWatch);
myFileObserver.StartWatching (); // and StopWatching () when you are done...
var document = Path.Combine(pathToWatch, "StackOverFlow.txt");
button.Click += delegate {
if (File.Exists (document)) {
button.Text = "Delete File";
File.Delete (document);
} else {
button.Text = "Create File";
File.WriteAllText (document, "Foobar");
}
};
Run Code Online (Sandbox Code Playgroud)
adb logcat输出(单击测试按钮时):
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1031 次 |
| 最近记录: |