dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Xamarin #229

Closed quintonv closed 4 years ago

quintonv commented 4 years ago

How can I get this to work in Xamarin? I have added the packages and added most of the implementation found in the WPF Demo project.

It's just the Bootstrap, where should that be registered in Xamarin for the DI to register it? Also would the ThreadDispatcher need to be modified? I did the below for now

    public class XamUIThreadDispatcher : IUIThreadDispatcher
    {
        public async Task InvokeAsync(Action action)
        {
            await Task.Run(() =>
           {
               Application.Current.Dispatcher.BeginInvokeOnMainThread(action);
           });
        }
    }
dsuryd commented 4 years ago

Let me preface this by saying I've never done Xamarin development, and have no plan to support it on my own, but anyone is welcome to try to make it work, I will support your efforts.

Does your Xamarin project have its own DI container? If it does not, call the Bootstrap.Resolve static method from your view model class constructor to get an instance of DotNetifyClient object. And if it does, look at this code on registering the dotNetify client objects to the container.

The UIThreadDispatcher ensures that UI updates are done by the UI thread. You probably don't need to use Task.Run. Try this:

public class XamUIThreadDispatcher : IUIThreadDispatcher
 {
     public Task InvokeAsync(Action action)
     {
        Application.Current.Dispatcher.BeginInvokeOnMainThread(action);
        return Task.CompletedTask;
     }
}
quintonv commented 4 years ago

@dsuryd thanks for the Bootstrap.Resolve suggestion above. I will try that and see if it works. Whenever I call the Dispose I get a error saying that I can't dispose it because it is not connected. So I am thinking the Bootstrap never connects to the server.

quintonv commented 4 years ago

@dsuryd thanks for the assistance. After wiring up the standard Microsoft DI I was able to get your Demo working in Xamarin. I implemented the exact same logic as the WPF app.