简体   繁体   English

如何在MVVM中使用akka.net的参与者?

[英]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. 在控制台应用程序或单元测试中使用akka.net似乎非常容易。

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. 等待Ask呼叫的答案将阻止UI。 The MainViewModel is already derived from a base class, so make it an actor is not an option. MainViewModel已经从基类派生,因此使其成为actor并不是一种选择。

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" 将收到答案并可以调用mainviewmodel的回调,但是给我InvalidOperationException“从无效线程中调用”
  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. 我想知道如何通过Actor消息填充ListViews或ComboBoxes。

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). 默认情况下,大多数Akka actor安排在后台线程中,因此它们无法在UI上进行直接更新(仅允许应用程序的UI /主线程执行此操作)。 If you want to spawn an actor on UI thread, you need to configure it to use SynchronizedDispatcher . 如果要在UI线程上生成actor,则需要将其配置为使用SynchronizedDispatcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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