如何从Droid项目触发事件到Portable项目Xamarin.Forms

时间:2016-12-08 12:40:49

标签: xamarin.forms

假设我在Droid项目中有一个ScanData()函数,当我按下设备中附带的扫描按钮时会触发该函数。无论我通过Android设备(Motorola TC70)扫描的是什么,此功能都可以为我提供条形码的扫描值。

现在可以在Droid项目中使用依赖注入或Messaging Center类型类触发扫描值时同时触发Portable项目的XAML表单中的事件。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我知道这是一个老线程,但我自己解决了这个问题,所以这是一个解决方案。 你确实可以使用MessagingCenter,虽然它可以让它工作变得麻烦。 这就是我解决它的方法:

在我的App.xaml.cs中,我有这个播放器对象:

public static Interfaces.IAudioPlayer player = DependencyService.Get<Interfaces.IAudioPlayer>();

在我的界面的Android实现中,我有这个:

public AudioPlayerImplementation()
    {
        InitializePlayer();
        Player.Prepared += Player_Prepared;
    }
private void Player_Prepared(object sender, EventArgs e)
    {
        Player.Start();
        MessagingCenter.Send<IAudioPlayer>(this, "PlayerPrepared");
    }

在我想收到消息的PCL课程中,我有这个:

MessagingCenter.Subscribe<Interfaces.IAudioPlayer>(App.player, "PlayerPrepared", (args) =>
        {
            PlayerPrepared();
        });

这很有效,我花了一段时间来弄明白。

相关问题