代码之家  ›  专栏  ›  技术社区  ›  Jonny Cundall

如何防止mycontrol.g.cs用错误的基类重写

  •  1
  • Jonny Cundall  · 技术社区  · 15 年前

    我的应用程序有两个自定义光标,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>
    

    反恐精英:

    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中的分部类从system.windows.controls.usercontrol继承。由于xaml.cs文件中分部类的另一端仍继承CursorBase,因此会发生生成错误。我发现每次修改G.CS文件都很烦人。有人知道如何防止这种情况发生吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Graeme Bradbury    15 年前

    您的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

        2
  •  1
  •   Malcolm    15 年前

    嘿,嗨,@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>
    

    肌热光标的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();
        }
    }