MVP パターンを実装しようとしていますが、イベント ハンドラーに問題があります。もともと私はこれを部分クラスで宣言しました。ロジックが含まれているため、プレゼンターに移動する必要があると想定していますか?
ただし、InvokeRequired/invoke をプレゼンターに移動すると、明らかにエラーが発生します。したがって、メソッド全体をビューに残すこととは別に、私が思いついた他の唯一のオプションは、イベントハンドラーをビューに残すことです。そのため、InvokeRequired などに問題はありませんが、EventHandler の本体、つまりアクションデリゲート、プレゼンターへ。ビュー - >プレゼンターの間で通信するためにDI atmを使用しているように、そのようなメソッド呼び出しがどのように機能するかはわかりませんが、プレゼンター - >ビューを取得する方法はわかりません。
public void CompletionReportNotifier(object sender, VerificationStatusEventArgs e)
{
Action action = () =>
{
//Display messages depending on whether it was canceled or not.
if (e.CarriedOutToCompletion == true)
{
string logMessage = string.Format("The data verification operation has been completed and {0} errors were found. Please view the error log for additional information.", inputs.NumberOfErrorsFound.ToString());
_view.UpdateLog(logMessage);
}
else
{
_view.UpdateLog("The data verification has failed. Please view the error log for additional information.");
}
//...
};
if (InvokeRequired)
Invoke(action);
else
action();
}
ItsMattの返信に基づいて編集
発表者コード:
public void CompletionReportNotifier(object sender, VerificationStatusEventArgs e)
{
_view.PermanentCsvFileVerificationCancellation = null;
string logMessage;
bool inputsVisible = false;
//Display messages depending on whether it was canceled or not.
if (e.CarriedOutToCompletion == true)
{
logMessage = string.Format("The data verification operation has been completed and {0} errors were found. Please view the error log for additional information.", inputs.NumberOfErrorsFound.ToString());
}
else
{
logMessage = "The data verification has failed. Please view the error log for additional information.";
}
//Assign values to parameters depending on if it failed or errors were encountered.
if (e.CarriedOutToCompletion != true || inputs.NumberOfErrorsFound > 0)
{
inputsVisible = true;
_view.VerificationCompleted = false;
}
else
{
_view.VerificationCompleted = true;
}
_view.UIUpdate(logMessage, inputsVisible);
}
コードを表示:
public void UIUpdate(string logMessage, bool inputsVisible)
{
Action action = () =>
{
UpdateLog(logMessage);
AccessToCsvFileVerificationInputs(inputsVisible);
btnDataVerification.Text = "Verify Data";
DisplayBusyMouseCursor(false);
VerifyingData = false;
};
if (InvokeRequired)
Invoke(action);
else
action();
}