用于设置警报的MvvmCross插件

时间:2014-01-13 14:22:54

标签: xamarin mvvmcross

我想编写一个跨移动平台应用程序,通过指定日期和时间等必需参数来设置警报。我只想设置一次而不是重复。 我无法在mvvmcross或Xamarin中找到任何现成的插件?

请帮忙

1 个答案:

答案 0 :(得分:1)

由于MVVMCross中没有现有的插件,您可能想要编写自己的插件。您可以在这里找到文档:

https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins

因为您想指定一些参数,所以您需要看到以下部分:

https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#writing-a-configurable-plugin

总的来说,这就是你可能会做的事情:

常规界面

public interface IAlarm
{
    void SetupAlarm(); 
}

public class PluginLoader
    : IMvxPluginLoader
{
    public static readonly PluginLoader Instance = new PluginLoader();

    public void EnsureLoaded()
    {
        var manager = Mvx.Resolve<IMvxPluginManager>();
        manager.EnsurePlatformAdaptionLoaded<PluginLoader>();
    }
}

Android实施

public class DroidAlarmConfiguration 
    : IMvxPluginConfiguration
{
    public AlarmLength { get; set;}
}

public class DroidAlarm : IAlarm
{
    public TimeSpan AlarmLength { get; set; }

    public void SetupAlarm()
    {
        //ALARM IMPLEMENTATION HERE. NOTE THIS IS SOME JAVA SYNTAX!!!!
        var globals = Mvx.Resolve<Cirrious.CrossCore.Droid.IMvxAndroidGlobals>();
        var alarm = globals.ApplicationContext
                              .GetSystemService(Context.ALARM_SERVICE)
                              as AlarmManager;

        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        alarmLength, alarmIntent);
    }
}

public class Plugin
    : IMvxPlugin
{
private _alarmLength = **Your Value Here**;

public void Configure(IMvxPluginConfiguration configuration)
{
    if (configuration == null)
        return;

    var droidConfiguration = (DroidAlarmConfiguration)configuration;
    _alarmLength = droidConfiguration.AlarmLength;
}

public void Load()
{
      var instance = new DroidAlarm();
      instance.AlarmLength = _AlarmLength;
    Mvx.RegisterSingleton<IAlarm>(instance);
}
}  

Setup.cs - 为所有android / ios / windows设置一个核心位置的值

protected override IMvxPluginConfiguration GetPluginConfiguration(Type plugin)
{
    if (plugin == typeof(Yours.Alarm.Droid.Plugin))
    {
        return new Yours.Alarm.Droid.DroidAlarmConfiguration()
        {
            AlarmLength = **YOUR VALUE HERE**
        };
    }

    return null;
}

然后,您将按照iOS和Windows Phone的相同Droid步骤进行操作。我希望这有帮助!