C# PresentationFramework.dll 中发生“System.Reflection.TargetInvocationException”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12164488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll
提问by
Okay, I have a bit of a weird bug...
好吧,我有一个奇怪的错误......
This works fine:
这工作正常:
private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs e)
{
//comboBoxNormalPoint.SelectedIndex = 0;
//ellipsePoint.Fill = System.Windows.Media.Brushes.Black;
}
This throws System.Reflection.TargetInvocationException:
这抛出System.Reflection.TargetInvocationException:
private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs e)
{
comboBoxNormalPoint.SelectedIndex = 0;
ellipsePoint.Fill = System.Windows.Media.Brushes.Black;
}
Also, it doesn't let me debug it; it crashes as the program loads. If I put a breakpoint anywhere it doesn't hit it; it just errors right away.
此外,它不允许我调试它;它在程序加载时崩溃。如果我在任何地方设置断点,它都不会命中;它只是立即出错。
采纳答案by H.B.
The event is probably raised before the elements are fully loaded or the references are still unset, hence the exceptions. Try only setting properties if the reference is not nulland IsLoadedis true.
该事件可能在元素完全加载之前引发或引用仍未设置,因此是例外。如果引用不是null并且IsLoaded是 ,请仅尝试设置属性true。
回答by Bojin Li
I think you will have fewer problems if you declared a Property that implements INotifyPropertyChanged, then databind IsChecked, SelectedIndex(using IValueConverter) and Fill(using IValueConverter) to it instead of using the Checked Event to toggle SelectedIndexand Fill.
我认为如果你声明一个实现 INotifyPropertyChanged 的属性,然后 databind IsChecked,SelectedIndex(使用 IValueConverter)和Fill(使用 IValueConverter)到它而不是使用 Checked 事件来切换SelectedIndex和Fill。
回答by Bibaswann Bandyopadhyay
If the radiobutton-checked event occurs before the content of the window is loaded fully, i.e. the ellipse is loaded fully, such an exception will be thrown. So check if the UI of the window is loaded (probably by Window_ContentRendered event, etc.).
如果radiobutton-checked事件发生在窗口内容完全加载之前,即椭圆完全加载,这样的异常将被抛出。所以检查窗口的 UI 是否加载(可能通过 Window_ContentRendered 事件等)。
回答by MacGyver
To diagnose this issue, place the line of code causing the TargetInvocationException inside the try block.
要诊断此问题,请将导致 TargetInvocationException 的代码行放在 try 块中。
To troubleshoot this type of error, get the inner exception. It could be due to a number of different issues.
要解决此类错误,请获取内部异常。这可能是由于许多不同的问题。
try
{
// code causing TargetInvocationException
}
catch (Exception e)
{
if (e.InnerException != null)
{
string err = e.InnerException.Message;
}
}
回答by usefulBee
This is often caused by an attempt to process a null object. An example, trying to empty a Bindable list that is null will trigger the exception:
这通常是由尝试处理空对象引起的。例如,尝试清空为 null 的 Bindable 列表将触发异常:
public class MyViewModel {
[BindableProperty]
public virtual IList<Products> ProductsList{ get; set; }
public MyViewModel ()
{
ProductsList.Clear(); // here is the problem
}
}
This could easily be fixed by checking for null:
这可以通过检查 null 轻松解决:
if (ProductsList!= null) ProductsList.Clear();

