问题
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));
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?
回答1:
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.
From the above link:
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.
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.
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:
Create an interface in your shared code that has the methods you will call that will run the platform specific code
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.
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))]
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.
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");
This call is made from your shared code and the type argument, ITextToSpeech is the name of the interface you created in step 1.
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.
来源:https://stackoverflow.com/questions/42965924/permitting-firebase-xamarin-realtime-streaming