I currently use inject IJSRuntime along with navigator.geolocation, navigator.mediaDevices But is it correct? Isn't it possible to access these functions without javascript using only C#? What do you think about it ? Thank you for your feedback. Daniel Kminek
2 Answers
I'm am researching this question right now, and the answer is No you have to use JSInterop. And Yes it can be done through Interop. For an example of GeoLocation, see Ed Charbeneau's ebook Blazor: A Beginners Guide and I've linked to the backing source code. Also checkout AspNetMonsters/Blazor.Geolocation I've used it for months and it is great.
Regarding MediaDevices: I've written some JS Interop/Blazor code and verified Blazor can see it, but not much further progress. Can't find any examples, unlike Geolocation.
I can't really shed any light why you can't access the devices from C#. Above my pay grade. But I guess it could be API's not available from the Browser (which is really what has access to the hardware), or the underlying Blazor circuit. If I make any headway, I'l update.
You can use this Nuget package it can help you access Browsers Geolocation services as well. See Geo JS docs as well as demo app.
