Exception happening endlessly in content control

Firstly, this is a simplified version using the wizard using MVVM. The problem is simply easier to reproduce, as described below

After a significant reduction, I resolved an infinite exception in my code due to WPF ContentControl. However, I have yet to figure out how to handle this except try-catch, completing all of my possible instance code. Here is an example code that reproduces this ... any help on how to save this endless exception would be greatly appreciated.

additional information

To summarize, the problem is that if the content control modifies its contents and the loaded thing throws an exception, then it will throw and then retry loading, causing a repeated reset.

MainWindow.xaml

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name ="Main">
    <Grid>

        <ContentControl Name="bar" Content="{Binding ElementName=Main, Path=foo}"/>
    <Button Click="ButtonBase_OnClick" Margin="20" Width="50"/>
  </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private UserControl _foo;
    public UserControl foo
    {
        get { return _foo; }
        set { _foo = value; OnPropertyChanged("foo"); }
    }

    public MainWindow()
    {
        InitializeComponent();
        foo = new UserControl1();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        foo = new UserControl2();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

UserControl1

UserControl2.xaml.cs

public UserControl2()
{

  InitializeComponent();
  throw new Exception();
}
+4
1

ContentControl MainWindow. DataTemplates, MainWindow. - - ContentControl Content DataContext MainWindow.

. . - , - TestData.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace fwWpfDataTemplate
{
    // Classes to fill TestData
    public abstract class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }
    public class Student : Person { }
    public class Employee : Person
    {
        float _salary;
        public float Salary
        {
            get { return _salary; }
            set
            {
                _salary = value;
                OnPropertyChanged("Salary");
            }
        }
    }

    public class TestData : ObservableCollection<Person>
    {
        public TestData()
            : base(new List<Person>()
            { 
                new Student { Name = "Arnold" },
                new Employee { Name = "Don", Salary = 100000.0f }
            }) { }
    }
}

DataTemplates MainWindow - MainWindow.xaml:

<Window x:Class="fwWpfDataTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:me="clr-namespace:fwWpfDataTemplate"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type me:Student}">
            <StackPanel>
                <TextBlock Text="Student"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type me:Employee}">
            <StackPanel>
                <TextBlock Text="Employee"/>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="Salary"/>
                <TextBlock Text="{Binding Salary}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="Change Data Context" Click="Button_Click" />
        <ContentControl Grid.Row="1" Content="{Binding}"/>
    </Grid>
</Window>

. StackPanels DataTemplates UserControl1, UserControl2 ..

- MainWindow.cs:

using System.Windows;

namespace fwWpfDataTemplate
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        TestData testData = new TestData();
        int testIndex = -1;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            testIndex = (testIndex + 1) % testData.Count;
            this.DataContext = testData[testIndex];
        }
    }
}

Enjoy.

0

Source: https://habr.com/ru/post/1538438/


All Articles