在Xamarin / C#的Andr​​oid FileObserver例子吗? [英] Android FileObserver example in Xamarin/C#?

查看:459
本文介绍了在Xamarin / C#的Andr​​oid FileObserver例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创造Xamarin C#中的文件观察者(安卓)指导

I need guidance in creating a file observer in Xamarin c# (Android)

某种可行的例子将是美好的!

Some sort of workable example would be wonderful !

我已经尽力了Java管束转换C#,但由于我缺乏在C#环境的经验,它在编译的时候扔的错误太多..和获取写入引用code在C#VS Java的被证明刺激。

I've tried to convert the java ones over to C# but due to my lack on experience in the C# environment, it's throwing too many errors when being compiled .. and getting the write reference code within C# vs java is proving irritating ..

所以,请!,可能有人在那里我指向某种可行的。

So PLEASE !, may someone out there point me to some sort of workable

这是一个文件观察者的Java示例 https://gist.github.com/shirou/659180

This is a java example of a file observer https://gist.github.com/shirou/659180

推荐答案

创建一个类继承的 Android.OS.FileObserver =nofollow的> <$ C $,你只需要实现<一个href="http://developer.xamarin.com/api/member/Android.OS.FileObserver.OnEvent/p/Android.OS.FileObserverEvents/System.String/"相对=nofollow> 的OnEvent() 和一个(+)构造函数。它的一个非常简单的模式后,你曾看到它... ...; - )

Create a class that inherits from Android.OS.FileObserver, you only need to implement the OnEvent() and one(+) Constructors. Its a really simple pattern after you see it once... ;-)

注:

  • 在腕表上的路径,如果你需要通过文件来进行筛选,这样做在一个OnEvent
  • 请不要让你的FileObserver对象获取GC'd或OnEvents会奇迹般地停止: - /
  • 记得打电话StartWatching()以收到的OnEvent电话
  • Watch on a path, if you need to filter by file, do it in the OnEvent
  • Do not let your FileObserver object get GC'd or your OnEvents will magically stop :-/
  • Remember to call StartWatching() in order to receive OnEvent calls

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)); 
        }
    }
}

示例用法:

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");
    }
};

亚行logcat输出(Test按钮点击时):

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

这篇关于在Xamarin / C#的Andr​​oid FileObserver例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆