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.
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.
This is my first foray into custom controls, and it's not going well. I have a custom graph control derived from Canvas.
namespace Grapher2 {
public class SeriesManager : Canvas {
public SeriesManager() {
...
}
}
}
It's defined in the same project and namespace as my app. I tried adding a reference to the control in XAML as follows:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:SeriesManager Name="seriesManager" Width="700" Height="500" />
</StackPanel>
But when I try to reference the control name "seriesManager" in the code-behind for the Window, I get "The name 'seriesManager' does not exist in the current context."
Furthermore, the XAML editor will not render the Window, giving a huge stack trace with the error: "Type 'MS.Internal.Permissions.UserInitiatedNavigationPermission' in Assembly 'PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
I imagine the solution is something stupidly simple for anyone who's done custom controls. But I'm stumped.
did you try x:Name="seriesManager" in your xaml?
Edit: This may not be the issue seeing how you said your xaml isn't rendering. I'm guessing once you get the xaml to render in the designer... the code behind will work better.
Edit 2: Whenever I've had a problem with the designer rendering, it's because I'm doing something in the constructor of my custom control. Check your SeriesManager to see if you are doing something in its constructor that is causing a problem. Maybe you are referencing something that doesn't exist yet. If you do have extra code in your constructor, consider moving it to the UserControl_Loaded event.
Backing up Scott's answer here, since he helped me solve it:
What I did wrong was trying to access the control BEFORE InitializeComponent(), but was confused by 2 other error messages somewhere else in the code.
Just in case someone else has this error.
i have some UserControls that are shown fine in designer, but i can't make any changes to the design-time example content from the constructor. It seems like it is not executed at all.
XAML:
<UserControl x:Class="Example.Test"
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>
<TextBlock Name="testx" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
Code:
using System.ComponentModel;
using System.Windows.Controls;
namespace Example
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class Test : UserControl
{
public Test()
{
InitializeComponent();
if (DesignerProperties.GetIsInDesignMode(this))
testx.Text = " IN DESIGN!";
}
}
}
I've tried many options, but still can't get it how to display design-time data in WPF designer :( Different context binding also shows nothing...
PS: Tried clean VS2012 and VS2013 projects on Win8. NOTHING WORKS! :( I don't know what to do, haven't found anything similar on the net... Is it sufficient to just add design check in constructor and set existent control text? It should work, right?
K, the short answer is: You're on the right path.
The long one is: It's a bit more complicated than that.
Your example will "kinda" work, as in, if you'll put an else testx.Text = RUNTIME; after your if, like that:
if (DesignerProperties.GetIsInDesignMode(this))
testx.Text= " IN DESIGN!";
else
testx.Text= " Runtime";
you'll see what you want on runtime, but you're design time will stay empty.
For the Design time, you also need to set the context if I'm not mistaken.
If you're using any of the MVVM framework out there, you kinda get this functionality for "free". As in, you'll have a "in design time" property and you can set whatever data you want for the design. The catch is that you need to have an empty constructor if my memory serves me right.
You'll also use bindings, and not set the text property directly.
I remember that the default WPF and binding for design time was lacking a bit last time I tried to do something like that in "vanilla" wpf (as in, no MVVM, no bindings), but I believe that with a bit of a hack it's achievable. Again, can't remember it from the top of my head.
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
{
...
HOPEFULLY, someone strong in WPF knows what's going on... The scenario I've sampled below is also applicable to others too, like textbox, command buttons, etc...
I'm playing with creating custom user controls... Ex: working with a simple Combobox. In one project class library LibA I've created some samples derived from... ex: TextBox, ComboBox, Window, etc. A second library LibB I'm creating another class derived from ex: Combobox in LibA... Otherwise, no problem.... done similar thing in C# WinForms with no problems.
Now, the problem, I drag the control (from LibB) onto the first "Window" (native so no derivation issues) of the app, save and run. The derived library doesn't even hit its constructor which I just put a simple command just test it was getting created properly worked or not, but its not... In the XAML of the form, it is properly referencing both namespace projects, so I know that appears correct.
So, I then created a derived combobox in the same original LibA, put that on the form, and IT properly went into the constructor.
Here's a snippet of what I have going on.
namespace LibA
{
public class MyCombo1 : ComboBox
{
public MyCombo1()
{ ToolTip = "this is my base declaration"; }
}
public class MyCombo1b : MyCombo1
{
public MyCombo1b() : base()
{ ToolTip = "this constructor IS reached"; }
}
}
In a separate project (library),
using FirstLibraryThatHas_MyCombo1
namespace LibB
{
public class OtherLibCombobox : MyCombo1
{
public OtherLibCombobox() : base()
{ ToolTip = "this version is NOT being recognized in the window"; }
}
}
So, neither of these are visually designed, they are all in code only... In addition, I've done it with the TextBox control too, same results... It doesn't stop in the debugger... Any ideas?
Although I've changed actual names from sample, here's a brand new window, one with original class declaration AND one with the DERIVED version.. Here's a full XAML test window
<Window x:Class="MyProject.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="300"
xmlns:my="clr-namespace:WPFGUI;assembly=WPFGUI"
xmlns:my1="clr-namespace:DerivedControls;assembly=DerivedControls">
<Grid>
<my:MyComboBoxClass
Name="MyComboBoxInWindow"
HorizontalAlignment="Left"
Height="23"
Width="120" />
<my1:cboDerivedComboClass
Name="cboDerivedComboInWindow"
Height="23"
HorizontalAlignment="Left"
Width="120" />
</Grid>
</Window>
Isn't this making a circular reference?
You call MyDerivedControl that is in another assembly, and DerivedControl needs the primary assembly
because it inherits a type you defined there.
And then, you try to display it in a window from the primary assembly?
Try to clean and rebuild your project.
I've tried and failed to reproduce the problem. I think you have a different problem, though. If you use the xaml above - the number two combobox will completely cover the first - thus you will not be able to get the tooltip...
Also, check that all assemblies target the same framework version.