1

在我的MainViewModel我订阅Event

var uc = new MyUserControl();
uc.ViewModel.ValueTypeChangedEvent += OnValueTypeChanged;
Control05 = uc;

StartMeasuring2();

BackgroundWorker的是:

private myUserControl Control05 { get; set; }

private void StartMeasuring2()
{
    _backgroundWorkerP2.RunWorkerCompleted += backgroundWorkerCompleted;
    _backgroundWorkerP2.DoWork += BackgroundWorkerDoWork;

    if (_backgroundWorkerP2.IsBusy)
        _backgroundWorkerP2.CancelAsync();
     else
     {    
         _backgroundWorkerP2.RunWorkerAsync();
     }
}

private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs eventArgs)
{
    Thread.Sleep(3000);

    while (!StopMeasuringP2)
    {
        const int measurementStatus = 0;
        var random = new Random();
        double measurementValue = random.Next(1, 9);
        var dateTime = DateTime.Now;
        Control05.ViewModel.UserControl1.ViewModel.LastDayMesswerte(dateTime,measurementValue, measurementStatus);
        Thread.Sleep(7000);
     }
}

编辑:

这是我在用户控件中的 LastDayMesswerte 方法ViewModel

public virtual void LastDayMesswerte(DateTime time, double measurementValue, int measurementStatus)
{
     if(measurementValue>=0 && measurementValue<3) 
          ValueType = "Alarm";

     if(measurementValue>=3 && measurementValue<6)
          ValueType = "Warning";

     else
     {
         ValueType = "Normal";
     }
}

ValueType的财产:

public virtual string ValueType
{
    get { return _valueType; }

    set
    {
        if (_valueType == value) return;
        _valueType = value;
        OnValueTypeChangedEvent(ControlId, MasterId);
    }
}

OnValueTypeChangedEvent

protected virtual void OnValueTypeChangedEvent(int controlId, int masterId)
{
    ValueTypeChangedEventHandler handler;

    lock (_valueTypeChangedEventLock)
    {
        handler = _valueTypeChangedEvent;
    }

    if (handler != null) handler(controlId, masterId);
 }

LastDayMesswerte所以在我调用之前BackgroundWorker_valueTypeChangedEvent不是 NULL。我在方法中的第一个 if 语句上设置了一个断点,LastDayMesswerte并且_valueTypeChangedEvent已经是 NULL。为什么会这样?

4

2 回答 2

1

如果我没记错的话(你的代码很不寻常——我不会从一个 ViewModel 创建一个 UserControl,它在其 ViewModel 中包含另一个 UserControl,它本身也有一个 ViewModel),问题在于

Control05.ViewModel.UserControl1.ViewModel.LastDayMesswerte(...)

你打电话LastDayMesswerteControl05.ViewModel.UserControl1.ViewModel但你从​​来没有在ValueTypeChangedEvent那里。相反,您将其设置为Control05.ViewModel.

于 2013-06-13T09:26:02.227 回答
1

执行后尝试启动后台工作者:

var uc = new MyUserControl();
uc.ViewModel.ValueTypeChangedEvent += OnValueTypeChanged;

并且还要确保您引发事件的视图模型实例是您的用户控制视图模型。希望这个提示可以帮助...

编辑

您是否尝试使用 执行在另一个线程中引发事件的操作SynchronizationContext?像这样:

        //Register the events
        var currentSyncContext = SynchronizationContext.Current;
        var backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork += (_,__) =>
                                       {
                                           //some action in background
                                           currentSyncContext.Send((t) =>
                                                                       {
                                                                           //Action that raise the event
                                                                       }, null);
                                       };
        backgroundWorker.RunWorkerAsync();

也许这行得通,试试看...

于 2013-06-12T13:52:32.577 回答