我的应用程序MyCursor和MyOtherCursor有两个自定义游标,两者都是用xaml设计的,我在xaml.cs中为每个游戏添加了一些行为。这两种情况都是一样的,所以我让它们从基类继承来减少代码重复。
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);
}
}
基类没有xaml,它纯粹在cs中定义。
我的问题是,如果我在xaml中为MyCursor更改某些内容,则会重新生成MyCursor.g.cs文件,而不是从CursorBase继承,g.cs中的partial类继承自System.Windows.Controls。用户控件。由于xaml.cs文件中的partial类的另一端仍然继承CursorBase,因此会发生构建错误。我发现每次修复g.cs文件都很烦人。有谁知道如何防止这种情况发生?
答案 0 :(得分:2)
你的XAML错误应该是:
<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>
g.cs文件是从XAML生成的,根据XAML,您的基类是UserControl
答案 1 :(得分:1)
嘿嘿,@ Jonny对我来说,它的工作正常,这就是我所做的,我认为你搞砸了命名空间:
我的项目命名空间是:SilverlightApplication2 在这个项目中我创建了名为CursorBase的cs文件,继承它形成用户控件:
public class CursorBase : UserControl
{
public virtual void MoveTo(Point pt)
{
this.SetValue(Canvas.LeftProperty, pt.X);
this.SetValue(Canvas.TopProperty, pt.Y);
}
}
然后我创建了两个用户控件MyCursor.xaml和MyOtherCursor.xaml
MyOtherCursor的xaml:
<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>
MyOtherCursor的cs:
public partial class MyOtherCursor : CursorBase
{
public MyOtherCursor()
{
InitializeComponent();
}
}
MyCursor也一样:
MyCursor的xaml:
<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>
MyCursor的cs:
public partial class MyCursor : CursorBase
{
public MyCursor()
{
InitializeComponent();
}
}