Something very strange with WPF solution - wpf

This is happening in several old or new projects in my WPF based solution. I have just added a window called Dashboard, and in the generated 'Dashboard.xaml.vbfile, I entered the following code. Note it normally pastesInitializeComponentin for you when you createSub New`, but now it doesn't. So, I have:
Public Class DashBoard
Public Sub New()
InitializeComponent()
End Sub
End Class
Ans the compiler complains that:
Error 15 'InitializeComponent' is not declared. It may be inaccessible
due to its protection level.
It's as if the IDE doesn't know this is the code behind for a Window. Some kind of partial class or link is missing, yet in the vbproj file, we do still find the link:
<Compile Include="Bridge.xaml.vb">
<DependentUpon>Bridge.xaml</DependentUpon>
</Compile>
I am using PostSharp on all projects for automatic error logging, but it has never given me trouble before. It is remarkably well behaved.
ADDED:
`Dashboard.xaml' looks like:
<Window x:Class="DashBoard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DashBoard" Height="300" Width="300">
<Grid>
</Grid>
</Window>
and `Dashboard.xaml.vb' looks like this:
Public Class DashBoard
End Class

To relate you Dashboard.xaml.vb file with the existing Dashboard.xaml file, you need to:
declare the class partial, and
make it inherit from the WPF window base class, for example:
Public Partial Class Dashboard Inherits Window
As long as your class does not inherit from Window, it's clear that it doesn't find the InitializeComponent method.
However, this does not explain why the necessary code parts are not generated automatically. How does the Dashboard.xaml file look like?

Related

Inheriting from Window in WPF MVVM

I'm trying to implement the following solution: https://stackoverflow.com/a/1486481/154439. In the author's solution he writes,
I created an abstract ApplicationWindowBase class that inherits from Window.
I am not sure what he means here. I tried changing my XAML from <Window ... to <ApplicationWindowBase .... No luck.
The designer-generated file for the window inherits from System.Windows.Window, but as that file is designer-generated, making changes to it does me no good. I've tried various other approaches with no success.
I am very new to both WPF and MVVM. Am I missing something obvious?
Code to go along with Kevin's comment:
Code Behind
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : ApplicationWindowBase
{
public MainWindow()
{
InitializeComponent();
}
}
XAML
<myPrefix:ApplicationWindowBase x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myPrefix="clr-namespace:StackOverflow"
Title="TestWindow" Height="300" Width="300">
<Grid>
</Grid>
</myPrefix:ApplicationWindowBase>
Just make sure you use the correct namespace, mine happens to be StackOverflow.
First, xaml or not, these are all classes. Therefore, anything inheriting from another class (i.e., Window), must do so as any other class.
namespace SomeNamespace
{
public sealed class MyWindow : Window
{
public object SomeNewProperty {get;set;}
}
}
xaml is just a different type of serialization with its own set of rules. But it's still serialized XML, and as such everything must be traceable back to its original type.
How xaml does this is through a facility in XML called "namespaces". They fit well with our understanding of namespaces as C# developers, as we must map an XML namespace to a namespace in our application so that the xaml serializer can match an XML element with a CLR type.
Xaml uses a couple different ways of matching these namespaces. One looks like a URL and is defined by an assembly attribute (e.g., "xmlns="http://microsoft.com/whatevs"). This works when the assembly is external to the one you are writing in. In your case, you'll have to use the other method of identifying your namespace. This specialized XML namespace is parsed by the xaml serializer in order to identify the namespace and containing assembly of your classes. It goes by the form "clr-namespace:SomeNamespace;assembly=SomeAssembly". You can omit the assembly bit if the type is in the current assembly.
To put it all together with the above example, you would create an instance of your new window like this:
<t:MyWindow
xmlns:t="clr-namespace:SomeNamespace"
xmlns="the standard microsoft namespace here"
t:OmitOtherStuffBecauseThisIsAnExampleLol="true">

inheriting user control from a base class

I've done lots of searching and found lots of answers but for some reason it's not working for me. I have a VB app in WPF. I want some common code for user controls.
So I make a base class like this:
Public Class cU
Inherits UserControl
Public Value As Double
End Class
And a user control, the Xaml starts like this:
<UserControl x:Class="UserControl3"
So all I have to do, as far as I can see, is to change this to:
<local:cU x:Class="UserControl3"
But although no error is shown in the Xaml window, I get and error in the error list:
"local" is an undeclared prefix. Line 1. position 2.' XML is not valid
(by the way, when I did exactly this in winrt it worked fine)
I tried changing it to local to Controls and also putting cU in a namespace called local but it doesn't change.
Add your namespace in your XAML for your window tag or control tag (depends whichever you are using).
<Window x:Class="YourNamespace.YourClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace">
then you can use the <local:UserControl3 /> tag
the YourNamespace in your case would be WpfApplication1

Prism-WPF equivalent to Silverlight's: CompositionInitializer class and SatisfyImports()

i'm using Prism-MEF-WPF and Sometimes i need view model gets constructed from the XAML
of the view, so the container is not involved and can’t do the dependency injection
automatically (as there is no Export attribute used with VM).so there should be some
class in Prism-WPF like CompositionInitializer to enable me to ask the container to
do the injection.In case there is equivalent class how to use it, and in case there is
no equivalent how to construct view model from xaml of the view knowing that i use MEF.
Thanks in advance.
The problem is that you can't create an object in XAML if it doesn't have a parameterless constructor.
Using the ServiceLocator, you can achieve this. It will work as an IoC (and is set up by Prism/MEF, you just have to drop the .dll):
The xaml:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
The code-behind:
class ViewModel : NotificationObject
{
public ViewModel()
{
var firstDependency = ServiceLocator.Current.GetInstance<FirstDependencyType>();
//... more dependencies here instead of as constructor parameters
}
//class code omitted for brievity
}
Here is the right answer which i got from Agustin Adami "http://blogs.southworks.net/aadami":
Based on my understanding the view model can be instantiated in XAML as the view’s DataContext only if a view model does not have any constructor arguments. And as far as I know creating objects defined in XAML by partnering with an Inverse of Control Container is currently not supported.
Regarding the CompositionInitializer class, as far as I know there is no equivalent class for WPF, on the other hand regarding this topic, I believe you could find the following blog post interesting:
•http://reedcopsey.com/2010/03/26/mef-compositioninitializer-for-wpf/
Also, I believe an alternative for this could be registering the CompositionContainer class like mentioned in this thread:
http://compositewpf.codeplex.com/discussions/311933
As this could let you retrieve this class for example in your view model's constructor, in order to call the SatisfyImportsOnce method to satisfy the Imports defined in the passed class:
this.compositionContainer =ServiceLocator.Current.GetInstance();
this.compositionContainer.SatisfyImportsOnce(this);
Bootstrapper class is what you are looking for. It uses UnityContainer for injecting dependencies. This link here might be of your interest too..
EDIT
If i am getting right, you want to create a ViewModel from your xaml which can be achieved like this(Here local is namespace of your ViewModel class) -
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>

WPF error message: Partial declaration must not specify different base classes

Why do I have this error message for a UserControl:
Partial declaration of
MyNamespace.MyUserControl must not
specify different base classes
Just because I inherited from another UserControl class I created in another namespace whereas this other namespace is referenced in the XAML as
xmlns:my="clr-namespace:ReferedNamespace;assembly=ReferedNamespace"
Little to go on here, but this usually happens when the code behind and the xaml file do not inherit from the same base class.
Since we do not have all the details concerning your problem,
I'll create a situation that will cause the same exception to be thrown,
this might help you understand your problem.
As an example, just create new WPF application using Visual Studio,
The XAML might look something like this:
<Window x:Class="WpfApplication1.MainWindow" .....>
The code behind would then contain something like this:
public partial class MainWindow : Window
{
//Code here
}
Note the 'partial' modifier here. It means this class (MainWindow) might
not be defined in a single file but spread out across multiple files,
in this case the XAML (.xaml.cs) and the CS (.cs) files.
Now add a new UserControl to the solution. It will be named UserControl1.
Without making any changes to the XAML, change the code behind for the MainWindow:
public partial class MainWindow : UserControl1
{
//Code here
}
Now you'll get the exception you questioned about.
Look for something like this in your code, if you still can't find a solution,
please provide more code.
look to both your .cs and .xaml files at this parts
in .xaml file :
<Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ...... </Window>
in .cs file :
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : Window
{
//code here
}
}
Now if u wanna change Window to UserControl
change
<Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </Window>
<!--**becomes**-->
<UserControl x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </UserControl>
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : Window
{
//code here
}
}
//**becomes**
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : UserControl
{
//code here
}
}
- i hope it`s useful :) .
I wanted to add this piece of information. In Visual Studio 2017, There is a bug which I haven't had time to dig into with much depth, but it causes this error. (from the OP)
Overview: When declaring a partial class as an x:Class for a resource xaml file, it will cause this error, and after removing the declaration, the error will persist.
To reproduce this bug, (or if you are here because you are getting this error message)
Step 1: Create a new WPF Project.
Step 2: Create a new Resource File
Step 3: Change the resource files name-space to the projects main namespace (remove the bit at the end.)
Step 4: In the resource files declaration/header section, add an x:Class declaration specifying your MainWindow (default startup window object) as the Class for that resource file. (EG: x:Class=MainWindow)
Step 5: Clean/Rebuild Solution
Step 6: You should now be getting this error from the OP. So remove the x:Class Declaration from your header on the Resource File, and save/clean/rebuild the project.
If the bug was reproduced, the error should remain, even though you have removed the x:Class declaration from the resource file.
The only solution I could find for this bug, is to delete the projects Bin folder, and any other folder with builds in it.
After deleting them, Clean/Rebuild your project, and the error should be gone.

WPF - UserControl inheritance

I have problem with control inheritance in WPF. I created a UserControl named BaseUserControl. I want for this control to be a base control for other WPF userControls. So I wrote another UserControl called FirstComponent. In next step I changed this code
FirstComponent : UserControl
to this
FirstComponent : BaseControl
However during compilation I get this error
Partial declarations of 'controlinheritance.componenets.FirstComponent' must not specify different base classes
What should I do to enable FirstComponent to derive from BaseControl?
EDIT
Thanks to abhishek answer I managed to inherit controls . Howerver I have another question. In base class I specified a property public Grid _MainGrid { get; set; }. Now I want in my derived class create an instance of this grid. So I used this code
Howerver I get an error Property '_MainGrid' does not have a value. Line 8 Position 36.
Did you see my complete article on it?
http://www.dotnetfunda.com/articles/article832-define-base-class-for-window--usercontrol-.aspx
I hope that would help you in this.
If you try to execute the project, it would definitely throw error to
you. This is because, every WPF window is created from the baseWindow
layout rather than the current Window layout. In other words, if you
see the XAML, you will see the root tag is Window, which is a class
just parent of the current window.
Thus to ensure everything works perfectly, we need to change the Root
Element.
So it would look like :
<local:BaseWindow Class="BaseWindowSample.Window1"
Name="winImp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaseWindowSample"
Title="Window1">
...
</local:BaseWindow>
If you see this minutely, you can see I have added one namespace to my
project and named it as local. So BaseWindow should come from
BaseWindow and thus it goes like local:BaseWindow
Well the reason for the initial error was because the class was actually a partial class that was listing a particular base inheritance somewhere else in addition to the location where you changed your base class.
As for your property 'inheritance', I suggest trying
public Grid MainGrid
{
get
{
return base.MainGrid;
}
set
{
base.MainGrid = value;
}
}
However I should note that this will not give you a link to any existing instance(s) of your base class. If you want there to be a guaranteed link in your derived class to the lone instance of that Grid, then you will have to make the base class property a static.
In which case, your code will look like this...
public Grid MainGrid
{
get
{
return BaseControl.MainGrid;
}
set
{
BaseControl.MainGrid = value;
}
}
When you specify a Different base class for a Usercontrol in the XAML.cs file
FirstComponent : BaseControl
You should also change this in the XAML
<Base:BaseControl x:Class="FirstComponent"
xmlns:Base="clr-namespace:MyApplication.Base"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</Base:BaseControl>

Resources