Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止myControl.g.cs被错误的基类覆盖_C#_Silverlight - Fatal编程技术网

C# 如何防止myControl.g.cs被错误的基类覆盖

C# 如何防止myControl.g.cs被错误的基类覆盖,c#,silverlight,C#,Silverlight,我的应用程序有两个自定义游标MyCursor和MyOtherCursor,它们都是在xaml中设计的,我在xaml.cs中为它们添加了一些行为。这两者的行为是相同的,所以我让它们从基类继承,以减少代码重复 xaml: 基类没有xaml,它完全是在cs中定义的 我的问题是,如果我在xaml中为MyCursor更改了某些内容,则会重新生成MyCursor.g.cs文件,而不是从CursorBase继承,g.cs中的分部类将从System.Windows.Controls.UserControl继承。

我的应用程序有两个自定义游标MyCursor和MyOtherCursor,它们都是在xaml中设计的,我在xaml.cs中为它们添加了一些行为。这两者的行为是相同的,所以我让它们从基类继承,以减少代码重复

xaml:

基类没有xaml,它完全是在cs中定义的


我的问题是,如果我在xaml中为MyCursor更改了某些内容,则会重新生成MyCursor.g.cs文件,而不是从CursorBase继承,g.cs中的分部类将从System.Windows.Controls.UserControl继承。由于xaml.cs文件中分部类的另一端仍然继承CursorBase,因此会发生生成错误。我发现每次修改g.cs文件都很烦人。有人知道如何防止这种情况发生吗?

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

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

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

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

这不应该发生……请尝试删除现有的生成文件夹,其中包含所有生成的文件,然后重新生成,然后再查看。@Malcolm我尝试过了,结果又发生了。我怀疑我遗漏了一些关于如何在SL中继承控件行为的信息。让我试试看……这不应该发生……请尝试删除现有的生成文件夹,其中包含所有生成的文件,然后重新生成并查看。@Malcolm我试过了,结果又发生了。我怀疑我遗漏了一些关于如何在SL中继承控件行为的信息。让我试试看..您需要为CursorBaseThank指定一个XAML命名空间谢谢,我怀疑我遗漏了一些基本信息,比如您需要为CursorBaseThank指定一个XAML命名空间谢谢,我怀疑我错过了一些基本的东西谢谢你花时间调查Malcolm,我现在明白我的错误了谢谢你花时间调查Malcolm,我现在明白我的错误了