MainWindow.xaml:
<Window x:Class="View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Nothing" Height="600" Width="800" MinHeight="450" MinWidth="400"/>
Mainwindow.xaml.cs:
using System.Windows;
namespace View
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
It throws an error. The namespaces are correct, i tried rebuilding the solution, cleaning the solution, the xaml build action is set to Page. The same code worked before, then i changed the View namespace to View2 in both files, it started throwing errors, so i changed it back, but the same error appears. Is there anybody who solved this bug somehow (without creating a new solution and copying the files, which would obviously solve the problem)?
Related
I made a project where it was one page only, then after I made everything decided to go back in and make a second page, which was a huge mistake because now I have 50 compiler errors. In my new page's code behind, I cannot access any control by name and the page cannot access any event handlers in the code-behind. Here is an example:
Autoclicker.xaml
<Page x:Class="_1337clicker.Pages.Autoclicker"
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"
xmlns:local="clr-namespace:_1337clicker.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Autoclicker">
<Grid>
<Button Name="hi"/>
</Grid>
</Page>
Autoclicker.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace _1337clicker
{
public partial class Autoclicker : Page
{
public Autoclicker()
{
hi.Content = "hello world"; //The name "hi" does not
//exist in the current context
}
}
}
I think you just messed up classname/namespace.
Check in XAML:
x:Class="_1337clicker.Pages.Autoclicker"
This will generate all the variables related to controls in a class called Autoclicker in a namespace of _1337clicker.Pages
but then, your code-behind class is:
namespace _1337clicker // <---- DIFFERENT NAMESPACE
{
public partial class Autoclicker : Page
You ended up with two 'Autoclicker' classes, one generated from XAML, other writen by you. Correct the namespaces and class names so they are identical, and try again. Generated code will end up in the same class as codebehind, and probably all such errors will go away.
EDIT: also what Joe noticed, it should be x:Name. Just 'Name' will let you find the control with tree walking and/or find-by-name tools, but probably won't generate a variable for code-behind.
I am trying to set my datacontext in the XAML file via
<Window x:Class="LocationScout.SettingsDeleteWindow"
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:ViewModel="clr-namespace:LocationScout.ViewModel"
mc:Ignorable="d"
Title="Delete" Height="315" Width="350"
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<ViewModel:SettingsDeleteDisplayItem/>
</Window.DataContext>
The XAML editor, however, shows the error "The name "SettingsDeleteDisplayItem" does not exist in the namespace "clr-namespace:LocationScout.ViewModel".
The view model class looks fine for me:
namespace LocationScout.ViewModel
{
public class SettingsDeleteDisplayItem : BaseObservableObject
{
private long _countryAreaCountToDelete;
public long CountryAreaCountToDelete
{
get => _countryAreaCountToDelete;
set
{
_countryAreaCountToDelete = value;
OnPropertyChanged();
}
}
}
}
Building the solution works fine without error. Any idea? Many thanks.
As advised by #RobertHarvey in a comment, I changed to lowercase and restarted Visual Studio:
try changing ViewModel to lower case, as in xmlns:viewModel.
The reason this might have been an issue is because ViewModel (in
upper case) appears in your namespace declarations.
Now it works.
Just rebuilding the solution did not help (I tried that earlier).
Tried to move all my Windows to a folder called Views within Visual Studio for better organisation. But now I'm getting errors on Click events:
Error 2 'JamSnapsWPF.Views.Main' does not contain a definition for 'addlogin_Click' and no extension method 'addlogin_Click' accepting a first argument of type 'JamSnapsWPF.Views.Main' could be found (are you missing a using directive or an assembly reference?) C:\Users\Martyn Ball\documents\visual studio 2013\Projects\JamSnapsWPF\JamSnapsWPF\Views\Main.xaml 226 79 JamSnapsWPF
Here is the line which the error originates from:
<Button Name="addlogin" Click="addlogin_Click">
I have updated the Windows Class so that it references the code behind for that window:
<Window x:Class="JamSnapsWPF.Views.Main"
What have I done wrong?
When you organize your files into folders inside Visual Studio, that doesn't automatically change the namespaces in any of your files.
Check your code-behind files to see what the namespace is. It should be the same namespace referenced at the top of your XAML files.
For example, I created an empty solution with one Window. I created a "Views" folder and moved the file into it. The code-behind moved too. But neither file references the "Views" folder in them anywhere. They still look like this:
namespace WpfApplication2
{
public partial class MainWindow
{
public MainWindow()
...
}
}
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
... >
...
</Window>
Either change the namespace in your XAML back, or change the code-behind file to match it:
namespace WpfApplication2.Views
{
public partial class MainWindow
{
public MainWindow()
...
}
}
<Window x:Class="WpfApplication2.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
... >
...
</Window>
In my tests, I've created a simple class like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace Test
{
public class MyCustomWindow: Window
{
}
}
This class is compiled into a dll.
In another project, I tried to use this custom window, like this:
<Custom:MyCustomWindow x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="clr-namespace:Test;assembly=Test"
Title="MainWindow" Height="600" Width="1210" WindowState="Maximized" >
<Grid Background="Blue">
<Button Content="Button" HorizontalAlignment="Left" Margin="457,212,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
This thing compiles with no errors, and works great when the custom window is opened by the "StartupUri" in the App.xaml file (that defines the first window loaded).
However, if I set other window to load in the StartupUri, and:
MainWindow m = new MainWindow();
m.Activate();
m.Show();
this.Close();
The CustomWindow will open, but without any content, without button and without the blue grid - and even without the title.
Any workaround? And what I need to do to open a Window with the same behavior of the StartupUri?
Edit:
I've noticed that the MainWindow (or any window derived from MyCustomWindow) simply cannot have the method InitializeComponent() in the constructor, because it does not exist in the context. Strangely, when using StartupUri, the contents are loaded normally without this.
Edit 2:
I think that the problem is occurring because I can't put the InitializeComponent() method in the MyCustomWindow. This explains why the MainWindow can be loaded normally into the StartupUri: it's loading directly from the xaml file, so it's parsing the content without the need of the InitializeComponent.
I starting to think about implement the IComponentConnector interface, but I have no idea how to do this.
Edit 3:
The code-behind of the file MainWindow.xaml.cs is:
using Test;
namespace TestingCustomWindow
{
public partial class MainWindow : MyCustomWindow
{
public MainWindow()
{
// Cannot use InitializeComponent here
}
}
}
pls add the new window using Visual Studio and
Replace :Window with :MyCustomWindow. You will get initializecomponent. You will hav to update window tag with your CustumWindow tag in xaml also
Adding it as answer so other can use it.
Thanks
I think the constructor has to look like this
public class MyCustomWindow: Window
{
InitializeComponent();
}
I have WPF project and some resources defined in App.xaml that are used in other files. When I try to move App.xaml to subdirectory designer is no longer able to find those resources. My project still compiles since I use 'Startup' event instead of 'StartupUri'. How to tell the designer where to search for resources? How it knows where they are when App.xaml is in root of project?
Update:
Project files:
App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="startup">
<Application.Resources>
<SolidColorBrush Color="LightGray" x:Key="brush" />
</Application.Resources>
</Application>
App.xaml.cs
namespace WpfApplication1
{
public partial class App : System.Windows.Application
{
private void startup(object sender, System.Windows.StartupEventArgs e)
{
new MainWindow().Show();
}
}
}
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Background="{StaticResource brush}" />
MainWindow.xaml.cs
namespace WpfApplication1
{
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Update 2:
Uploaded zipped solution to http://zalil.ru/30771604 (download will start automatically)
Mirror: http://www.speedyshare.com/files/27697250/WpfApplication1.zip
1. Move you App.xaml to your desired location
2. Refactor you App.xaml.cs namespace to accommodate the new change:
3. Rebuild your solution.
[4]. Go to your project properies and set the Startup object to your App.xaml file at the new location.
[5]. Run your application and it should work successfully :)
I can't duplicate the problem on my end. Here's what I tried:
I created a application with a style in the resources of App.xaml. I moved App.xaml to a subdirectory. I have another window that uses the style defined in the resources of App.xaml (he resides in a different directory) and it was able to resolve it just fine. I added ..\ to the beginning of my original StartupUri.
I did some searching, what version of Visual Studio are you using? Apparently there may be an bug related to your problem in VS2008:
http://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored
He says a workaround for this bug is to set the x:Name attribute on Application. Hope that helps!
EDIT: I also tried handling the Startup event instead of using the StartupUri and it still worked fine.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
new MainWindow().Show();
}
}
EDIT PART 2:
Okay, I enclosed the SolidColorBrush inside a ResourceDictionary as such:
<ResourceDictionary>
<SolidColorBrush Color="LightGray" x:Key="brush" />
</ResourceDictionary>
And the window picks up the brush. The designer doesn't like it, but when I change from StaticResource to DynamicResource it stops complaining.
EDIT 3:
I just thought of something. Do you have VS2010 SP1 installed? It fixed some bugs with the designer.
And sorry, my edit number 2 didn't work as expected. I noticed my blue squiggles were gone in the xaml, but I didn't check the designer. x__x