简体   繁体   English

创建接收消息的视图

[英]Creating view on receiving a message

I have a MVVM view and viewmodel. 我有一个MVVM视图和viewmodel。 In the constructor of my viewmodel I pass a list of IObservable messages and subscribe to them through a simple class sitting outide of my viewmodel and view 在我的viewmodel的构造函数中,我传递了一个IObservable消息列表,并通过我的viewmodel和view的一个简单类来订阅它们

Outside class 课外

{
     viewModel =
            new ViewModelClass(
                responseHandler.AsObservable());

     viewModel.PropertyChanged += ViewModel_PropertyChanged;
 }

 private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
 {

     if (e.PropertyName == nameof(ViewModelClass.MyProperty))
     {
         // Error here
         view = new MyViewClass() { DataContext = viewModel };

     }
 }

In the view model constructor 在视图模型构造函数中

        subscription = receiveMessages.Subscribe(MessageReceived);

        private void MessageReceived(GvsMessage message)
        {
            MyProperty = true;

        }

On receiving a message I want to create my view not before that. 在收到消息后,我想在此之前创建我的视图。 Although the viewmodel is created before to handle property change etc 尽管在处理属性更改等之前创建了viewmodel

The problem is that I get "the calling thread must be sta because many ui components require this". 问题是我得到“调用线程必须是sta,因为许多ui组件需要这个”。 Could someone please help 请有人帮忙

As we spoke in the comments you need to use a Dispatcher which can be acquired from different parts of the app. 正如我们在评论中所说,您需要使用可以从应用程序的不同部分获取的Dispatcher
To initialize the dispatcher you can use this snippet: 要初始化调度程序,您可以使用以下代码段:

protected static Dispatcher _d;
if (Application.Current != null)
{
   _d = Application.Current.Dispatcher;
}
else
{
   _d = Dispatcher.CurrentDispatcher;
}  

Explanation: 说明:
The first dispatcher is done when the application has UnitTests when you run the tests this dispatcher will not be null, 第一个调度程序在应用程序具有UnitTests时完成,当您运行测试时,此调度程序将不为null,
The second one is the current Dispatcher used by your application. 第二个是您的应用程序使用的当前Dispatcher
After you have this in your code when initializing your VM now you can send messages Actions , Events to the UI Thread. 现在,在初始化VM时,在代码中有了这个,您可以向UI线程发送消息ActionsEvents
I have a little method just for that: 我有一个方法就是这样:

public static void UIThread(Action action)
    {
        if (_d == null)
        {
            if (Application.Current != null)
            {
                _d = Application.Current.Dispatcher;
            }
            else
            {
                _d = Dispatcher.CurrentDispatcher;
            }
        }
        _d.Invoke(action);
    }  

This function will accept lambda, like so: 这个函数将接受lambda,如下所示:

UIThread(() =>
        {
            Processing = true;
            Message = "Working ...";
            //in your case you would raise the Loaded event here
        });  

This way you EventHandler in your view will have no problem showing that view. 这样,您的视图中的EventHandler将显示该视图没有问题。
If you need any more info let us know. 如果您需要更多信息,请告诉我们。
HTH HTH

I resolved this by creating the view and the viewmodel in the constructor. 我通过在构造函数中创建视图和viewmodel来解决这个问题。 In the propertychanged event I just set a property IsVisible to 'true' binds the window visibility 在propertychanged事件中,我只是将属性IsVisible设置为'true'来绑定窗口可见性

<Window.Visibility>
    <Binding Path="IsVisible" Converter="{StaticResource BoolToVisibilityConverter}" Mode="TwoWay"/>
</Window.Visibility>

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

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