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.
Related
Hello I am using Prism in my demo project and I have a problem with user controls' inheritance. If I use my user control base class for my user control like below, user control's content is showing up empty. Then when I use
<igf:UserControlBase x:Class="DemoProject.Views.DemoView"
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:igf="http://igf.schema"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="200">
<StackPanel>
<TextBox Text="Hello Prism"/>
<TextBlock Text="Hello Prism"></TextBlock>
</StackPanel>
</igf:ViewControlBase>
and this is my user control and it is in another project. And ctors are commented now but still not working. Only way is changing
using System.Windows;
using System.Windows.Controls;
namespace DemoProject.Base.Controls
{
public class UserControlBase : UserControl
{
static UserControlBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(UserControlBase), new FrameworkPropertyMetadata(typeof(UserControlBase)));
}
public UserControlBase()
{
DefaultStyleKey = typeof(UserControlBase);
}
}
}
I found solution by adding code below into Assembly.cs file to user control's project
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
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 see a number of other people asking about this error message in other questions, but I don't seem to understand enough about what's going on to fix this for myself. I created this error by having a WPF UserControl
public partial class EnterNewRequest : UserControl
But then later on I wanted to add a method to UserControl, so I used inheritance to stick it in there (can't use an extension because I need to override this method). But now my usercontrol is upset, and I'm not sure what in the xaml I need to change. The UserControl change block is in the namespace RCO_Manager. This is my xaml:
<UserControl x:Class="RCO_Manager.EnterNewRequest"
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"
I had the same issue when I was working with Windows Phone. I can't remember the exact exception, but you can see the XAML here on GitHub, the page code here, and the base page code here (mine was a base page, not base control). I needed to add a new XAML namespace and change the <UserControl/> declaration:
Code Assumption
namespace RCO_Manager
{
// Inherits **Base**UserControl, not UserControl
public partial class EnterNewRequest : BaseUserControl
{
// Magic goes here
...
}
}
XAML
<local:BaseUserControl
xmlns:local="clr-namespace:RCO_Manager"
x:Class="RCO_Manager.EnterNewRequest"
Side Note
According to Baboon, you don't need to specify it in your code-behind once you specify the base class in the XAML, so you can then change the code-behind to show the following. I can't verify it right now, but you can give this a try after you get it working.
public partial class EnterNewRequest // Don't specify BaseUserControl here
{
...
I am trying to allow several classes to inherit a more general Silverlight user control to avoid redundancy in my code. The classes inherit the extended control, which then inherits the User Control class. The issue I have been running into is that the ExtendedControlExtension.g.cs file regenerates every time I compile, with the incorrect inheritance (it inherits User Control not my Extended Control).
Note that I have been inheriting the Extended Control in the .cs and g.cs files, but continuing to use the User Control tag in the .aspx file as this causes the error
Error 29 The tag 'ExtendedControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Is there a way to fix this?
Thanks!
You cannot change the .g.cs file, in fact is says so right in the file. Also, it's unfortunate to use the term "custom control" as this means something specific and not what you are trying to do. But, the good news is that what you are trying to do is possible.
Derive from UserControl:
public class FancyUserControl : UserControl
{
// Your added common functionality.
}
and then add a new UserControl to your project using the normal mechanism, let's say UserControl1. Then edit the UserControl.xaml files as follows:
<local:FancyUserControl x:Class="SilverlightApplication1.UserControl1"
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:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</local:FancyUserControl>
paying special attention to the three lines with local in them, adjusting to your application. Then edit the UserControl1.xaml.cs file as follows:
public partial class UserControl1 : FancyUserControl
{
public UserControl1()
{
InitializeComponent();
}
}
and Visual Studio won't be quite happy yet but finally rebuild your project and all will be well.
The class UserControl1 is now derived from FancyUserControl instead of UserControl and you can begin adding your common functionality. To add more controls you will need to manually edit the XAML and code-behind once after initially adding each new control to the project.
I have DataTemplate in a ressource dictionnary, and in some, I need button and i don't know how i can use code behind for manage events.
I tried to put a class in my resource dictionnary like that :
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SLProject.Templates"
x:Class="TVTemplate">
And I definied the class in the cs file like that :
namespace SLProject.Templates
{
partial class TVTemplate
{
}
}
The build is OK but when the application started, I obtains XAML error following :
AG_E_PARSER_BAD_TYPE
I tried all I know like change the class kind to a ClassModifier, make the class to an inherited class of RessourceDictionnary ... no way.
Someone have an idee ...
Thanks.
Using the x:Class attribute allows you to define a codebehind for a ResourceDictionary.
You must specify the complete namespace of the class (i.e. x:Class="WpfApplication.MyClass"), and such class has to be defined as partial (at least VS 2010 complains and does not compile without such modifier).
I mocked-up a simple example:
1. Create a new WPF application project (WpfApplication)
2. Add a new class file (TestClass.cs) and paste the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
namespace WpfApplication
{
public partial class TestClass
{
private void OnDoubleClick(object obj, MouseButtonEventArgs args)
{
MessageBox.Show("Double clicked!");
}
}
}
3. Add a new ResourceDictionary (Resources.xaml), open the file and paste the following code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication.TestClass">
<Style TargetType="{x:Type Label}">
<EventSetter Event="Label.MouseDoubleClick" Handler="OnDoubleClick"/>
</Style>
</ResourceDictionary>
4. Finally, open the MainWindow.xaml and past the following code
<Window x:Class="WpfApplication.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">
<Window.Resources>
<ResourceDictionary Source="Resources.xaml"/>
</Window.Resources>
<Grid>
<Label Content="Double click here..." HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="Red"/>
</Grid>
</Window>
In the example I wire-up a double-click event from a Style, since it is a scenario requiring you to call some code from a ResourceDictionary.
You have the x:Class attribute defined twice, which is why you're getting the parser error. Change your declaration to this and it should work:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SLProject.Templates.TVTemplate">
I Checked, and it's just an error of copy-past. I have well the class definied one time.
Best thing would be to make your own usercontrol and add your events in it . and later put this entire usercontrol in resource dictionary.