允许Firebase Xamarin RealTime流 [英] Permitting Firebase Xamarin RealTime Streaming

查看:60
本文介绍了允许Firebase Xamarin RealTime流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Xamarin中通过实时Firebase数据库集成构建一个跨平台的应用程序.我正在使用 Firebase C#库FirebaseDatabase.net .在FibrebaseDatabase.net文档中,要启用实时流传输,请注意例如:

I am building a cross-platform application within Xamarin with realtime Firebase database integration. I am using the Firebase C# library FirebaseDatabase.net. Within the FibrebaseDatabase.net documentation, to enable real-time streaming it notes to call for example:

var firebase = new FirebaseClient("https://dinosaur-facts.firebaseio.com/");
var observable = firebase
  .Child("dinosaurs")
  .AsObservable<Dinosaur>()
  .Subscribe(d => Console.WriteLine(d.Key));

但是,为了在Xamarin中最大程度地重复使用代码,我将需要从共享项目中调用此代码,而不是在每个独立的iOS和Android应用程序中调用它.我能想到的唯一方法是同步的,因此不允许从Firebase数据库进行实时监视/更新.

However, in order to allow for maximum code reuse within Xamarin, I will need to call this from the shared project, not within each independent iOS and Android app. The only ways I can think would be synchronous and therefore not permit real time monitoring/updating from my firebase database.

如何创建和调用方法以允许从另一个类中对该可观察对象进行异步侦听?

How can I create and call a method to allow for asynchronous listening on this observable from within another class?

推荐答案

如果这是特定于平台的API,则必须在各个应用程序项目中进行这些调用,但是可以使用

If that is a platform specific API, you will have to make those calls in the individual app projects, but you can use a DependencyService (if using Xamarin.Forms) to initiate the calls to the platform specific code from your shared code. If not using Xamarin.Forms, you will want to use some Inversion of Control library to do dependency injection.

从上面的链接:

Xamarin.Forms应用程序需要三个组件才能使用DependencyService:

Xamarin.Forms apps need three components to use DependencyService:

接口–所需的功能由共享代码中的接口定义.

Interface – The required functionality is defined by an interface in shared code.

每个平台的实现–必须将实现该接口的类添加到每个平台项目中.

Implementation Per Platform – Classes that implement the interface must be added to each platform project.

注册–每个实现类都必须通过元数据属性向DependencyService注册.注册使DependencyService能够找到实现类并在运行时提供它来代替接口.

Registration – Each implementing class must be registered with DependencyService via a metadata attribute. Registration enables DependencyService to find the implementing class and supply it in place of the interface at run time.

对DependencyService的调用–共享的代码需要显式调用DependencyService来请求接口的实现.

Call to DependencyService – Shared code needs to explicitly call DependencyService to ask for implementations of the interface.

请注意,必须为解决方案中的每个平台项目提供实现.没有实现的平台项目将在运行时失败.

Note that implementations must be provided for each platform project in your solution. Platform projects without implementations will fail at runtime.

IOW:

  1. 在共享代码中创建一个接口,该接口具有您将调用的将运行平台特定代码的方法

  1. Create an interface in your shared code that has the methods you will call that will run the platform specific code

在特定于平台的项目中创建实现第1步中创建的接口的类,然后使用需要运行的特定于平台的代码在接口中实现方法.

Create classes in your platform specific projects that implement the interface created in step 1. Then implement the method(s) in the interface with the platform specific code that you need to run.

该接口的每个实现都需要使用元数据属性向DependencyService注册.以下代码注册了Windows Phone的实现:

Each implementation of the interface needs to be registered with DependencyService with a metadata attribute. The following code registers the implementation for Windows Phone:

[assembly: Xamarin.Forms.Dependency (typeof (TextToSpeechImplementation))]

它放在步骤2中创建的特定于平台的类的类声明的上方.传递给typeof方法的参数是该类的名称.

This is placed above the class declaration in the platform specific class created in step 2. The argument passed to the typeof method is the name of that class.

  1. 一旦为每个平台设置了通用接口和实现的项目,就可以使用DependencyService在运行时获取正确的实现:

  1. Once the project has been set up with a common interface and implementations for each platform, use DependencyService to get the right implementation at runtime:

DependencyService.Get<ITextToSpeech>().Speak("Hello from Xamarin Forms");

此调用是通过您的共享代码和type参数进行的,ITextToSpeech是您在步骤1中创建的接口的名称.

This call is made from your shared code and the type argument, ITextToSpeech is the name of the interface you created in step 1.

如果您不使用Xamarin.Forms,请搜索控件库的反向版本,并按照提供的文档进行操作.

If you are not using Xamarin.Forms, search around for an inversion of control library and follow the docs provided on how to use it.

这篇关于允许Firebase Xamarin RealTime流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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