Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ViewModel回调到视图_C#_View_Model_Callback_Action - Fatal编程技术网

C# 从ViewModel回调到视图

C# 从ViewModel回调到视图,c#,view,model,callback,action,C#,View,Model,Callback,Action,我的视图调用ViewModel中的方法来获取数据。获取数据后,我基于从ViewModel返回的数据构建视图(网格) 视图模型中的getData()方法在BackgroundWorker线程中运行。现在我的问题是,在视图获取完所有数据后,如何返回视图 ViewModel { getData() { WorkerMethods() WorkerCompletedMethod() {

我的视图调用ViewModel中的方法来获取数据。获取数据后,我基于从ViewModel返回的数据构建视图(网格)

视图模型中的getData()方法在BackgroundWorker线程中运行。现在我的问题是,在视图获取完所有数据后,如何返回视图

    ViewModel
    {
       getData()
       {
            WorkerMethods()
            WorkerCompletedMethod()
            {
               Refresh()
            }
       }


       Refresh()
       {
           WorkerMethod()
           WorkerCompleted()
           {
                 data - Retrieved.
                 This is where all the calls are really DONE
           }
       }

     }
从这个角度来看,我会打电话的

View()
{
     VM.getData()
     //Before I call this method, I want to make sure Refresh() is completed
     BuildUI()
}
我希望BuildUI()方法只能在VM.getData()完全执行之后执行,然后再使用Refresh()方法执行,这是动态构建UI所需的数据


这就是我要做的。如果这不是正确的方法,请纠正我

在“代码隐藏”视图中

  View
  {

      public delegate void DelegateRefresh();
      Init()
      {
          DelegateRefresh fetcher = RefreshData;
          fetcher.BeginInvoke(null, null);
      }


      public void RefreshData()
      {
        _viewModel.GetData();
        **while (_viewModel.IsBusy)**
        {               
            continue;
        }
        BuildUI();
      }

      BuildUI()
      {
         //Code to build the UI Dynamically using the data from VM.
      }

您应该在
BackgroundWorker
完成其工作后检索数据。视图模型应该实现接口,并通过视图绑定到的属性公开数据。然后,视图模型可以在数据可用时通知视图(即
后台工作人员
已完成其工作)。

您应该在
后台工作人员
完成其工作后检索数据。视图模型应该实现接口,并通过视图绑定到的属性公开数据。然后,视图模型可以在数据可用时通知视图(即
BackgroundWorker
已完成其工作)。

一种方法是使用消息传递。也就是说,在视图上注册消息,然后从视图模型向视图发送消息,当收到此消息时,您可以调用
BuildUI
方法

例如,如果您使用的是MvvmLight框架,这里有一种方法可以传回一条错误消息以显示在对话框中。您可能不想显示对话框(我手头有这段代码),但过程是一样的,只是注册和发送的消息类型不同

视图模型:

public class ErrorMessage : DialogMessage
{
    // See MvvmLight docs for more details, I've omitted constructor(s)

    /// <summary>
    /// Registers the specified recipient.
    /// </summary>
    /// <param name="recipient">The recipient of the message.</param>
    /// <param name="action">The action to perform when a message is sent.</param>
    public static void Register(object recipient, Action<ErrorMessage> action)
    {
        Messenger.Default.Register<ErrorMessage>(recipient, action);
    }

    /// <summary>
    /// Sends error dialog message to all registered recipients.
    /// </summary>
    public void Send()
    {
        Messenger.Default.Send<ErrorMessage>(this);
    }
 }

public class SomeViewModel : ViewModelBase
{
    public void SendErrorMessage(string message)
    {
        var errorMessage = new ErrorMessage(message);
        errorMessage.Send(); 
        // Or in your case, when the background worker is completed.          
    }
}

一种方法是使用消息传递。也就是说,在视图上注册消息,然后从视图模型向视图发送消息,当收到此消息时,您可以调用
BuildUI
方法

例如,如果您使用的是MvvmLight框架,这里有一种方法可以传回一条错误消息以显示在对话框中。您可能不想显示对话框(我手头有这段代码),但过程是一样的,只是注册和发送的消息类型不同

视图模型:

public class ErrorMessage : DialogMessage
{
    // See MvvmLight docs for more details, I've omitted constructor(s)

    /// <summary>
    /// Registers the specified recipient.
    /// </summary>
    /// <param name="recipient">The recipient of the message.</param>
    /// <param name="action">The action to perform when a message is sent.</param>
    public static void Register(object recipient, Action<ErrorMessage> action)
    {
        Messenger.Default.Register<ErrorMessage>(recipient, action);
    }

    /// <summary>
    /// Sends error dialog message to all registered recipients.
    /// </summary>
    public void Send()
    {
        Messenger.Default.Send<ErrorMessage>(this);
    }
 }

public class SomeViewModel : ViewModelBase
{
    public void SendErrorMessage(string message)
    {
        var errorMessage = new ErrorMessage(message);
        errorMessage.Send(); 
        // Or in your case, when the background worker is completed.          
    }
}

你需要清楚具体地回答你的问题,否则你将得不到有用的答案。请更新您的问题。对不起,我更新了我的原始帖子,如果不清楚,请告诉我。您需要清楚具体地回答您的问题,否则您将无法得到有用的答案。请更新你的问题。对不起,我更新了我的原始帖子,如果不清楚请告诉我。谢谢你的建议。但是我没有使用MvvmLight框架。我们正在使用棱镜。不管怎样,这就是我最后要做的。我创建了一个委托方法并对其调用了BeginInvoke()。在委托方法中,我将检查ViewModel中的IsBusy属性是否为true,一旦为false,我就知道我已经完成了视图模型中的工作。将在原始帖子中发布代码。这将很好。我也使用
IsWaiting
和转换器对鼠标光标进行了类似的操作。谢谢你的建议。但是我没有使用MvvmLight框架。我们正在使用棱镜。不管怎样,这就是我最后要做的。我创建了一个委托方法并对其调用了BeginInvoke()。在委托方法中,我将检查ViewModel中的IsBusy属性是否为true,一旦为false,我就知道我已经完成了视图模型中的工作。将在原始帖子中发布代码。这将很好。我使用
IsWaiting
和转换器对鼠标光标执行类似操作。