简体   繁体   中英

How to use actors of akka.net in MVVM?

It seems that it is very easy to use akka.net in a console app or in unit tests.

But I wanted to try out some actors for a simple login method: open a splash screen, send a login command, close the splash screen when login was ok. Now the answer is sent to the sender, which is an actor. Awaiting the answer of an Ask call would block the UI. The MainViewModel is already derived from a base class, so make it an actor is not an option.

public void OnStartupCommand(EventArgs args)
{
    _splashScreen = new SplashScreenView();
    _splashScreen.Show();
    // [1]
    _actorSystem.ActorSelection("/user/system").Tell(new Login("Me"));
    // [2]
    var mainWindowActor =_actorSystem.ActorOf(Props.Create(() => new 
    MainWindowActor(this)), "mainwindow");
    mainWindowActor.Tell(new Login("me"));
    // [3]
    var result = await _actorSystem.ActorSelection("/user/system").Ask(new Login("me")); 
}
  1. Call the responsible actor directly, answer is send to deadletters. Are there more complex examples?
  2. Would receive the answer and could call callbacks of the mainviewmodel, but gives me InvalidOperationException "Call from invalid thread"
  3. Seems to be the only way to get the result of the call

I am wondering how to fill ListViews or ComboBoxes by Actor messagings.

By default most of akka actors are scheduled in background threads, therefore they cannot make direct updates on the UI (only UI/primary thread of the application is allowed to do that). If you want to spawn an actor on UI thread, you need to configure it to use SynchronizedDispatcher .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM