在 WPF 的 base xaml 中实现点击事件并在 base class 中处理它
implement click event in base xaml and handle it in base class in WPF
我是 WPF 新手。我正在创建一些示例 classes 以快速创建最终项目。我有 CustomControl1
作为基础 class,Generic
作为基础 xaml 和 wInherit
作为派生 xaml 和 class。我的基地 window 的 xaml 在这里:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah">
<!--x:Class="Palayeshgah.CustomControl1">-->
<Window.Resources>
<Style x:Key="btnClose" TargetType="{x:Type Button}" >
<Setter Property="Template">
...
</Setter>
</Style>
</Window.Resources>
...
<Grid>
<Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/>
</Grid>
这是我导出的 xaml:
<ns:CustomControl1 x:Class="Palayeshgah.PL.Base.wInherit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns="clr-namespace:Palayeshgah"
Title="Inheritance Sample" Height="300" Width="300" Background="Wheat" MouseEnter="CustomControl1_MouseEnter"/>
这是我的基地class:
public class CustomControl1 : Window
这是我导出的 class:
public partial class wInherit : CustomControl1
这些代码完美地展示了基础设计。但我应该覆盖我的按钮的点击事件。如果我在基础 xaml 中添加 Click="btnClose_Click"
到 btnClose
,我应该在 Generic.xaml
中引入一个 class。就像下面这样:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah"
x:Class="Palayeshgah.CustomControl1">
然后我收到一个错误:
Missing partial modifier on declaration of type 'Palayeshgah.CustomControl1'; another partial declaration of this type exists
.
我将 partial
添加到 CustomControl1
然后错误更改如下:
Partial declarations of 'Palayeshgah.CustomControl1' must not specify different base classes
我坚持住。
我没有忘记 btnClose_Click
。这是基础 class:
中的点击事件
protected virtual void btnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
您可能会问为什么我要将基数 xaml 与基数 class 分开。答案是我无法用这种方法展示基础设计。
谁能告诉我如何通过任何方法在 WPF 中为 window 和事件使用继承?
我建议你不要继承 CustomControl1 class。您为通用 xaml 创建一个用户控件并将其包含到派生的 xaml.
我是 WPF 新手。我正在创建一些示例 classes 以快速创建最终项目。我有 CustomControl1
作为基础 class,Generic
作为基础 xaml 和 wInherit
作为派生 xaml 和 class。我的基地 window 的 xaml 在这里:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah">
<!--x:Class="Palayeshgah.CustomControl1">-->
<Window.Resources>
<Style x:Key="btnClose" TargetType="{x:Type Button}" >
<Setter Property="Template">
...
</Setter>
</Style>
</Window.Resources>
...
<Grid>
<Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/>
</Grid>
这是我导出的 xaml:
<ns:CustomControl1 x:Class="Palayeshgah.PL.Base.wInherit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns="clr-namespace:Palayeshgah"
Title="Inheritance Sample" Height="300" Width="300" Background="Wheat" MouseEnter="CustomControl1_MouseEnter"/>
这是我的基地class:
public class CustomControl1 : Window
这是我导出的 class:
public partial class wInherit : CustomControl1
这些代码完美地展示了基础设计。但我应该覆盖我的按钮的点击事件。如果我在基础 xaml 中添加 Click="btnClose_Click"
到 btnClose
,我应该在 Generic.xaml
中引入一个 class。就像下面这样:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah"
x:Class="Palayeshgah.CustomControl1">
然后我收到一个错误:
Missing partial modifier on declaration of type 'Palayeshgah.CustomControl1'; another partial declaration of this type exists
.
我将 partial
添加到 CustomControl1
然后错误更改如下:
Partial declarations of 'Palayeshgah.CustomControl1' must not specify different base classes
我坚持住。
我没有忘记 btnClose_Click
。这是基础 class:
protected virtual void btnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
您可能会问为什么我要将基数 xaml 与基数 class 分开。答案是我无法用这种方法展示基础设计。
谁能告诉我如何通过任何方法在 WPF 中为 window 和事件使用继承?
我建议你不要继承 CustomControl1 class。您为通用 xaml 创建一个用户控件并将其包含到派生的 xaml.