3

I'm having a problem when I deploy my WPF project, the deployed project crashes on startup and produces a XAML.Parse.Exception with an inner exception of "Cannot have nested BeginInit calls on the same instance" at Line 4 Position 70. The App has full permissions on my computer. I am asking this question because the few questions asked about this had no real solution to the problem.

Here is the XAML code it is referencing with the first couple of lines.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="ASRV.MainWindow"
    Title="ASRV GUI" Height="768" Width="1024" ResizeMode="CanMinimize">
<Window.Resources>
</Window.Resources>
<Window.Background>
    <ImageBrush ImageSource="pack://siteoforigin:,,,/background.png"/>
</Window.Background>
3
  • Why have an empty Window.Resources element? Commented May 23, 2013 at 1:35
  • What's the actual exception? I don't think it is XAML.Parse.Exception. Maybe XamlParseException? Commented May 23, 2013 at 2:40
  • Windows.Resources is empty because I was seeing if it was something in there since I'm new to styling. And yeah that's the exception I meant. Commented May 23, 2013 at 2:48

2 Answers 2

4

My guess is the reason being:

<Window.Background>
    <ImageBrush ImageSource="pack://siteoforigin:,,,/background.png"/>
</Window.Background>

You might be reusing this image elsewhere in the same window or subcontrol.

BeginInit is called in databinding and this is the only databound thing I could see in your sample code. "BeginInit calls on the same instance" points to it being bound twice.

Sign up to request clarification or add additional context in comments.

2 Comments

This was indeed the problem. Thanks!
@basarat Could it also happen when a resource in one ResourceDictionary is BasedOn a resource in another ResourceDictionary?
1

This error is encountered whenever an object is attempted to be bound twice, as @basarat says in his answer. Further to the example seen in the OP, I came across this same error caused in my case by setting a DataContext in the CodeBehind as well as in the xaml:

In the MainWindow.xaml file, I had the following:

<Window xmlns:vm="clr-namespace:MyApp.ViewModel"
    [...more xmlns references skipped for brevity...]
    <!--the next three lines caused a problem, when combined with the code behind-->
    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>
</Window>

And in the MainWindow.xaml.cs file, I had this:

namespace MyApp.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainViewModel MainViewModel { get; set; }
        public MainWindow()
        {
            bool success = false;
            MainViewModel = new MainViewModel();
            // This line caused the problem in combination with the xaml above
            DataContext = MainViewModel;
            InitializeComponent();
        }
    }
}

In order to get rid of the problem, I deleted one of the DataContext setters; it works with either the data context set in xaml or the data cantext set in the code behind, not both.

P.S. I add this answer because this is the first answer to come up on searches for this problem and I felt that further explanation would be useful to other visitors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.