Hello
I try to create a small app with xamarin forms and our awesome library. For a first try I created a console application with your example code
var url = "http://localhost:9696/";
if (args.Length > 0)
url = args[0];
// Our web server is disposable.
using (var server = new WebServer(url))
{
server.RegisterModule(new LocalSessionModule());
server.RegisterModule(new StaticFilesModule("C:\\test"));
server.Module<StaticFilesModule>().UseRamCache = true;
server.Module<StaticFilesModule>().DefaultExtension = ".html";
server.Module<StaticFilesModule>().DefaultDocument = "index.html";
server.RunAsync();
#if DEBUG
var browser = new System.Diagnostics.Process()
{
StartInfo = new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true }
};
browser.Start();
#endif
Console.ReadKey(true);
}
This console application works fine and I can access the website in a browser. Great so far.
After this I created a Xamarin.Forms application for iOS, Android and UWP. I used a similar code I only changed
server.RegisterModule(new StaticFilesModule("C:\\test"));
to
string folderName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test");
server.RegisterModule(new StaticFilesModule(folderName));
and I set the source from the webview to:
dwWebView.Source = new Uri("http://localhost:9696");
and I also removed the Debug and ReadKey lines because it didn't worked.
Now I get on Android device a connection refused error. On iOS device it's a blank page.
* What I Tried*
Access to locale files are allowed because I stored and check if the index.html file exists.
Internet access is also allowed because when I set a different URL in the webview and it worked.
It works when the URL for the webview is set to the locale file like
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test");
path = Path.Combine(path, "index.html");
What did I forget to do or did I wrong? Thanks in advance.
Your application has to wait for incoming connections, usually when after RunAsync you need to keep your application alive (for example with Console.ReadKey(true); for console apps).
How are you handling this? Is the application still running when you open the browser?
Yes the application is still running because Embedio runs in the same application as the webview.
One try was to start the embedio server in a additional task and I keep the task in a hacky way
while(true) {
Thread.Sleep(1000);
}
alive. (I'm new to C#) Without the while loop it is also not working.
The other try was to init and start embedio in the mainThread().
You can try to create a separated thread or task in your entry point or using the OnStart Method (https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/app-lifecycle).
protected override void OnStart()
{
Task.Factory.StartNew(async() => {
using (var server = new WebServer(url))
{
server.RegisterModule(new LocalSessionModule());
server.RegisterModule(new StaticFilesModule("C:\\test"));
server.Module<StaticFilesModule>().UseRamCache = true;
server.Module<StaticFilesModule>().DefaultExtension = ".html";
server.Module<StaticFilesModule>().DefaultDocument = "index.html";
await server.RunAsync();
}
});
}
Thanks a lot. It works for UWP and iOS but in android it shows the connection refused message. I think the problem on android is not related to embedio and for that the issues can be closed and later I let you know what the problem was.
Ok, thank you.
@deMichael I'm having the same trouble as you... On iOS, it runs perfectly, but on Android I can't manage to show the correct screen... Have you resolved this problem? I think that it could be some of Android permissions maybe... Please, if you have resolved it, share the solution with us :)
@allistoncarlos sorry I had no time to work future to solve the problem.
Permission is a good point and I tried for that the each at its own (webview for internet access and file operations). I don't know which else could be needed.
I don't know if this is related but this blog post may explain something:
https://spin.atomicobject.com/2018/01/08/xamarin-forms-embedded-web-server/
I've managed to resolve it! In my case, I've put the dist folder into Resources folder, same as the iOS project. But Android doesn't work that way, and then I've put the dist folder into Assets folder, making the app work as expected!
Hello everybody, I will appreciate a sample project and/or some guidance on how to use Xamarin Forms to include in the repository.
Thank you!
I can help you with that @geoperez. I'll fork the project and by the end of the next week I can create something basic. And thank you, for the helpful project!
Cool, thank you!
Hello @allistoncarlos , sorry for bother you, but did you get a chance to develop a working sample?
Another user is having issues with Android.
@geoperez I'm really sorry, but I have to be honest: I completely forgot to implement the solution... The good news is that I'm finishing it right now! Do you have a sample dist folder for showing how to use Angular, or a simple index.html file is ok?
A simple index will be enough. Thanks
On Sat, Oct 27, 2018, 12:41 PM Alliston notifications@github.com wrote:
@geoperez https://github.com/geoperez I'm really sorry, but I have to
be honest: I completely forgot to implement the solution... The good news
is that I'm finishing it right now! Do you have a sample dist folder for
showing how to use Angular, or a simple index.html file is ok?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/unosquare/embedio/issues/192#issuecomment-433640633,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABsYsJd_v_5-5JWnEBp2LVP6iAQAQkQ-ks5upJqugaJpZM4We9UG
.
Thanks for the patience @geoperez, and sorry about the delay... I hope you like the project!
Yes! Thank you! I just will add the fix for localhost with android (https://github.com/unosquare/embedio/issues/207#issuecomment-433437410)