Wpf 在Mainwindow XAML中使用基类控件

Wpf 在Mainwindow XAML中使用基类控件,wpf,Wpf,我希望创建一个基类,该基类包含用户控件和窗口的属性以及Mainwindow.xaml中使用的基类控件。 下面是我正在使用的代码,但出现以下错误“MainWindow的部分声明不能指定不同的基类” Usercontrol1.xaml <UserControl x:Class="WpfApplication1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我希望创建一个基类,该基类包含用户控件和窗口的属性以及Mainwindow.xaml中使用的基类控件。 下面是我正在使用的代码,但出现以下错误“MainWindow的部分声明不能指定不同的基类”

Usercontrol1.xaml

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

    </Grid>
</UserControl>
<Window x:Class="WpfApplication1.MainWindow"
        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:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>
现在的问题是如何在我的主窗口中使用usecontrol。下面是主窗口的代码 Mainwindow.xaml.cs

namespace WpfApplication1
{
    public class baseclass : UserControl
    {
        public Button textBox { get; set; }

        public baseclass()
        {
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox.ToString()))
            {

            }
        }
    }

    public partial class UserControl1
    {
        public UserControl1()
        {
            InitializeComponent();
        }


    }
}
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
}
main window.xaml

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

    </Grid>
</UserControl>
<Window x:Class="WpfApplication1.MainWindow"
        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:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

我不确定是我遗漏了一些明显的东西,还是其他人遗漏了一些明显的东西!我可以将
基类
添加到
主窗口
中,只需使用以下功能即可:

<Window x:Class="WpfApp2.MainWindow"
        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:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <local:baseclass />
</Window>

关键是定义为
xmlns:local=“clr namespace:WpfApp2”

如果您不仅仅是对代码感兴趣,那么您可以看到上面的内容将如何为您工作


现在你没有问的问题的真正答案是:你做错了!
baseclass
中的文本框永远不会工作,因为您还没有将其初始化为UserControl(这不仅仅是从UserControl继承),因此需要学习MvvM并停止依赖代码隐藏。你的生活会变得轻松得多。

我不确定我是否错过了一些明显的东西,或者其他人错过了一些明显的东西!我可以将
基类
添加到
主窗口
中,只需使用以下功能即可:

<Window x:Class="WpfApp2.MainWindow"
        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:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <local:baseclass />
</Window>

关键是定义为
xmlns:local=“clr namespace:WpfApp2”

如果您不仅仅是对代码感兴趣,那么您可以看到上面的内容将如何为您工作

现在你没有问的问题的真正答案是:你做错了!
baseclass
中的文本框永远不会工作,因为您还没有将其初始化为UserControl(这不仅仅是从UserControl继承),因此需要学习MvvM并停止依赖代码隐藏。你的生活会变得轻松得多

我想在主窗口中使用基本用户控件这就是我想做的

你不能。顶级窗口类不能从System.Windows.Controls.UserControl继承。它必须从System.Windows.Window继承。因此,不可能在WPF中为用户控件和窗口定义公共基类

您可以更改UserControl1的基类,但必须在XAML标记和代码隐藏文件中指定相同的基类:

UserControl1.xaml.cs:

public partial class UserControl1 : baseclass
{
    public UserControl1()
    {
        InitializeComponent();
    }
}
<local:baseclass x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

</Grid>
</local:baseclass>
UserControl1.xaml:

public partial class UserControl1 : baseclass
{
    public UserControl1()
    {
        InitializeComponent();
    }
}
<local:baseclass x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

</Grid>
</local:baseclass>

我想在主窗口中使用基本用户控件这就是我想做的

你不能。顶级窗口类不能从System.Windows.Controls.UserControl继承。它必须从System.Windows.Window继承。因此,不可能在WPF中为用户控件和窗口定义公共基类

您可以更改UserControl1的基类,但必须在XAML标记和代码隐藏文件中指定相同的基类:

UserControl1.xaml.cs:

public partial class UserControl1 : baseclass
{
    public UserControl1()
    {
        InitializeComponent();
    }
}
<local:baseclass x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

</Grid>
</local:baseclass>
UserControl1.xaml:

public partial class UserControl1 : baseclass
{
    public UserControl1()
    {
        InitializeComponent();
    }
}
<local:baseclass x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="80,46,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="80,82,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

</Grid>
</local:baseclass>


公共部分类用户控制1:UserControl
替换此行
公共部分类用户控制1:UserControl
@AnjumSKhan不工作…我想在主窗口中使用基本用户控制这就是我试图用
公共部分类用户控制1
替换此行的原因UserControl1:UserControl@AnjumSKhan不工作…我想在主窗口中使用基本用户控件这就是我想做的