我正在使用条形码进入窗口wpf应用程序,它没有显示弹出" MessageBox.Show(" Item Not Found");"当项目不存在于db
中时我已经实现了日志,根据日志它进入了其他循环,但没有显示消息框。
请让我知道你对同样的想法
我正在使用以下代码:
private void txtBarcode_TextChanged(object sender, TextChangedEventArgs e)
{
string urlLogPath = @"C:\11VideoWorking\Log\CashDrawerLog.txt";
try
{
if (txtBarcode.Text.Length == 13)
{
File.AppendAllText(urlLogPath, "Befor Get Product \n");
objProductList = ObjGasLogic.GetSearchedProduct();
File.AppendAllText(urlLogPath, "Befor Product Check \n");
if (objProductList != null)
{
File.AppendAllText(urlLogPath, "Before Condition \n");
if (objProductList.Any(CheckProduct => CheckProduct.Barcode == txtBarcode.Text.Trim()))
{
File.AppendAllText(urlLogPath, "Item Found \n");
objProductDetails = objProductList.Where(Product => Product.Barcode == txtBarcode.Text.Trim()).SingleOrDefault();
objProductDetails.Quantity = 0;
BindCurrentlySelectedProduct();
}
else
{
File.AppendAllText(urlLogPath, "Item Not Found ,\n");
MessageBox.Show("Item Not Found");
File.AppendAllText(urlLogPath, "After Item Not Found \n");
}
//ClearCurrentSelectedProductDetails();
}
else
{
File.AppendAllText(urlLogPath, "6 \n");
MessageBox.Show("Item Not Found");
ClearCurrentSelectedProductDetails();
}
}
File.AppendAllText(urlLogPath, "End of Try \n");
}
catch (Exception ex)
{
ObjExceptionLogic.LogException(ex.Message, Convert.ToInt32(User.Instance.User_ID));
File.AppendAllText(urlLogPath, "In catch\n" + ex.Message);
}
}
答案 0 :(得分:0)
好的,我试图稍微清理你的代码以减少cyclomatic complexity
private void txtBarcode_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (txtBarcode.Text.Length == 13)
{
var product = ObjGasLogic.GetProduct(txtBarcode.Text.Trim());
if (product != null)
{
objProductDetails = product;
objProductDetails.Quantity = 0;
BindCurrentlySelectedProduct();
}
else
{
MessageBox.Show("Item Not Found");
ClearCurrentSelectedProductDetails();
}
}
}
catch (Exception ex)
{
ObjExceptionLogic.LogException(ex.Message, Convert.ToInt32(User.Instance.User_ID));
}
}
这将要求您在objGasLogic类中创建一个新方法
ObjGasLogic.GetProduct(txtBarcode.Text.Trim())
采取条形码但应该降低此方法的复杂性。
有了这个实现,你应该更好地了解问题所在,我认为在这里写一个日志文件有点不必要,一个断点就足够了。