'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

后端 未结 5 656
时光取名叫无心
时光取名叫无心 2021-02-03 21:03

Okay, I have a bit of a weird bug...

This works fine:

private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs e)
{
   //comboBoxNormal         


        
相关标签:
5条回答
  • 2021-02-03 21:22

    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:

    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:

    if (ProductsList!= null) ProductsList.Clear();
    
    0 讨论(0)
  • 2021-02-03 21:26

    To diagnose this issue, place the line of code causing the TargetInvocationException inside the try block.

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-03 21:28

    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 SelectedIndex and Fill.

    0 讨论(0)
  • 2021-02-03 21:37

    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.).

    0 讨论(0)
  • 2021-02-03 21:42

    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 null and IsLoaded is true.

    0 讨论(0)
提交回复
热议问题