XAML.Parse.Exception on WPF project's deployment - wpf

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>

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.

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.

Related

C# WPF XAML Mouse(Down) events not working anymore as of .NET 5(C#9)

Screen shot of the issue I'm running into after going to .NET 5 (C# 9)
I'm trying to figure out how they want us to implement this simple code now.
It says its an error but it still builds the project. Although, this does prevents designer from building the view from xaml, so unable to edit the xaml properly, also prevents the functionality, in this case trying to drag a window with mouse_down event.
XAML:
<Window x:Class="WPFProject.Window1"
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"
xmlns:local="clr-namespace:WPFProject"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Border MouseDown="CodeBehind_DragFunction">
<TextBlock Text="Hello World" Foreground="Black"/>
</Border>
</Window>
C# Code-behind
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void CodeBehind_DragFunction(object sender_, MouseButtonEventArgs e_)
{
DragMove();
}
}
This worked in previous versions..
Suggested answer posted by #Pharaz Fadaei
It was an IDE bug that was fixed by following these steps outlined in this post: The event 'foo' is not a RoutedEvent

How to do this.DataContext = this: in XAML....for eg<Window.DataContext> <local:MainWindow/></Window.DataContext>-->

Although the result is same after compiling and running the code, I find out that there is a bit difference in design mode in defining the data context behind the XAML in .cs file like below
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
and doing in a xaml like this.
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
The difference is during design mode in the display window while coding. When the code is in Xaml the view is nice and it shows the elements for eg. for DataGrid its shows the number of rows and column with value its value if its defined.
So, having said that I want to change below code in .cs file to
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
its equivalent in XAML. I tried with the below code
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
but it throw exception on Initialization. Can someone please help me with it. I am still new to WPF Databindings Data Context stuff.
Thank You
This sets the DataContext to a new instance of MainWindow (which will in turn set the DataContext to a new instance and so on):
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
The equivalent of this.DataContext = this would be: <Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}">.
You may also set the design time DataContext:
d:DataContext="{d:DesignInstance Type=local:MainWindow, IsDesignTimeCreatable=True}

Setting Window.Datacontext

I am trying to use my viewmodel as my window's datacontext but am getting the error:
ViewModel is not supported in a Windows Presentation Foundation (WPF) project.
Clearly, I am not understanding something about the syntax and databinding my window to my view model, but I am not sure what it is that I don't know.
Any advice on what I should be reading?
<Window x:Class="SunnyBeam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SunnyBeam" Height="488.358" Width="1014.552">
<Window.DataContext>
<ViewModel/>
</Window.DataContext>
<Grid>
</Grid>
</Window>
Usually I set DataContext through codebehind like that:
public partial class Flor1 : Window
{
public Flor1()
{
var dc = new MyViewModel();
dc.LoadData();
DataContext = dc;
InitializeComponent();
}
}
In place of MyViewModel may be anything you want to bind to.
define class like
public class ViewModel
{
public string Name { get; set; }
public ViewModel()
{
}
}
Use it in xaml like
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ui:ViewModel/>
</Window.DataContext>
<Grid>
</Grid>
It should work.
I thought I would throw in my experience with this error.
I had my datacontext setup same as below, and kept getting an error that ViewModel didn't exist, which I know it did. I refused to set it in code behind, simply rebuilding my project actually fixed this error.
<Window.DataContext>
<ViewModel/>
</Window.DataContext>

How to add UserControl to a Panel on a WPF Window

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.
I've built a very primitive UserControl containing nothing more than a TextBox to use as a log window:
<UserControl x:Class="My.LoggerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>
I don't expect that to be the best way to do it, but it should be good enough for a prototype.
The code-behind is similarly simple:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}
private LogLevel level = LogLevel.Warning;
#region ILogger
public LogLevel Level
{
get { return level; }
set { level = value; }
}
public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}
// ...
#endregion
}
The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>
Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:
A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.
This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.
Any idea how to solve this?
<UserControl x:Class="My.LoggerControl"
xmlns:local="clr-namespace:My.LogTest"
Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:
xmlns:local="clr-namespace:My"
And I think it should work. Otherwise, fix the LoggerControl declaration.

Overspecified Namespace in UserControl fails WPF Build

I have a very simple user control, and I'm trying to instantiate it in XAML. I find that when I go a bit overzealous with the namespacing, I run into problems with x:Name.
Here is my UserControl:
<UserControl x:Class="UserControlTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<Grid>
<Label Name="Label1">Label</Label>
</Grid>
</UserControl>
Here is the code-behind for the UserControl:
Namespace UserControlTest
Partial Public Class UserControl1
End Class
End Namespace
Now, note that I have the root namespace of my VB.Net project set to "UserControlTest". Knowing that, have a look at my main window:
Here is my main window:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:UserControlTest.UserControlTest"
Title="Window1" Height="300" Width="300">
<Grid>
<control:UserControl1 />
</Grid>
</Window>
See how the control alias needs to have "UserControlTest.UserControlTest"? That's because I have the root namespace of my project set to UserControlTest, and I have defined the namespace of the UserControl to be UserControlTest, also. If I don't use a namespace for the UserControl, I don't have any troubles.
However, because I have done this, the build fails should I try to apply an x:Name to the UserControl, as follows:
<control:UserControl1 x:Name="test"/>
That will fail the build, with this error:
Type 'UserControlTest.UserControlTest.UserControl1' is not defined.
Can anybody explain why? Do I need to avoid putting my UserControls into namespaces just so I can give them x:Name values? I'd like to manipulate my UserControls from code-behind, and without an x:Name, I'm up the creek. But I don't want to sacrifice namespace usage just to get it!
Thanks very much.
I had the same problem (after rebuilding the project, first it worked fine...). I put UserControl into separate namespace.
What is the namespace defined as in the code-behind of your user control?
If your project was called Foo and you had a folder called Controls inside that project, any new user control added to that folder would be given the namespace Foo.Controls.
Then in your XAML you can reference it like so:
xmlns:Controls="clr-namespace:Foo.Controls"
...
<Controls:UserControl1 x:Name="uc1"/>
It seems like you have a naming issue.
EDIT:
Here's how I'm doing it in a project of mine.
StatusBar.xaml.cs
namespace Client.Controls.UserControls
{
public partial class StatusBar : UserControl
{
...
}
}
StatusBar.xaml
<UserControl x:Class="Client.Controls.UserControls.StatusBar">
</UserControl>
Main.xaml.cs
using Client.Controls.UserControls;
namespace Client
{
public partial class Main : Window
{
...
}
}
Main.xaml
<Window x:Class="Client.Main"
xmlns:UserControls="clr-namespace:Client.Controls.UserControls">
<UserControls:StatusBar x:Name="mainStatusBar" />
</Window>
I encountered the same problem in a vb.net project, and despite trying the suggestions here and elsewhere, could not solve the issue. In fact, I was able to take the exact same code out of our project to a new project, and it worked just fine (as far as I could determine all the configuration in the new project was identical). In the solution provided by David, I notice he is using c# - I am wondering if this is some weirdness associated with vb.net.
In the end, the user control I needed to use was quite simple and I implemented it as a resource ControlTemplate to get around the issue. Sorry I don't have a better answer, I am really not happy wih my findings...

Resources