how to prevent myControl.g.cs from overwriting with wrong base class
I've got two custom cursors for my app, MyCursor and MyOtherCursor, both of which were designed in xaml, and I added some behaviour in the xaml.cs for each of them. This behaviour was the same for both so I had them inherit from a base class to reduce code dupli开发者_运维百科cation.
xaml:
<UserControl x:Class="MyProject.MyCursor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="24" Height="24">
<Ellipse Stroke="Black" StrokeThickness="5" Height="24" Width="24" Fill="White" />
</UserControl>
cs:
public partial class myCursor: CursorBase
{
public InterchangeCursor()
{
InitializeComponent();
}
}
public class CursorBase : UserControl
{
public virtual void MoveTo(Point pt)
{
this.SetValue(Canvas.LeftProperty, pt.X);
this.SetValue(Canvas.TopProperty, pt.Y);
}
}
There is no xaml for the base class, it's purely defined in cs.
my problem is that if I change something in the xaml for MyCursor, the MyCursor.g.cs file is regenerated, and instead of inheriting from CursorBase, the partial class in the g.cs inherits from System.Windows.Controls.UserControl. Since the other side of the partial class in the xaml.cs file still inherits CursorBase, a build error occurs. I'm finding it annoying fixing the g.cs file each time. Does anyone know how to prevent this happening?
Your XAML is wrong it should be:
<CursorBase x:Class="MyProject.MyCursor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="24" Height="24">
<Ellipse Stroke="Black" StrokeThickness="5" Height="24" Width="24" Fill="White" />
</CursorBase>
The g.cs file is generated from the XAML and according to the XAML your base class is UserControl
Hey hi, @Jonny for me its working fine here is what i did, i think you messed with the namespace :
my project namespace is: SilverlightApplication2 Inside this project i created on cs file named CursorBase inheriting it form user control :
public class CursorBase : UserControl
{
public virtual void MoveTo(Point pt)
{
this.SetValue(Canvas.LeftProperty, pt.X);
this.SetValue(Canvas.TopProperty, pt.Y);
}
}
And then i created two user controls MyCursor.xaml and MyOtherCursor.xaml
xaml of MyOtherCursor :
<SilverlightApplication2:CursorBase x:Class="SilverlightApplication2.MyOtherCursor"
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:SilverlightApplication2="clr-namespace:SilverlightApplication2" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</SilverlightApplication2:CursorBase>
cs of MyOtherCursor :
public partial class MyOtherCursor : CursorBase
{
public MyOtherCursor()
{
InitializeComponent();
}
}
And same for MyCursor :
xaml of MyCursor :
<SilverlightApplication2:CursorBase x:Class="SilverlightApplication2.MyCursor"
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:SilverlightApplication2="clr-namespace:SilverlightApplication2" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</SilverlightApplication2:CursorBase>
cs of MyCursor :
public partial class MyCursor : CursorBase
{
public MyCursor()
{
InitializeComponent();
}
}
精彩评论