WPF-一个对话框,它有一个除Window之外的基类,该基类在其中实现泛型



我对这篇文章有类似的问题。

简而言之,如果您在WPF中创建一个对话框,您可以从VisualStudio:获得

<Window x:Class="FrontEnd.View.Window1"
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:DiabetesFrontEnd.View"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
...

作为xaml的一部分创建,以及

public partial class Window1 : Window
{
...

作为背后的代码。提问者询问如果你有一个插入了一些基类场景的场景,即,xaml会发生什么

public partial class Window1 : BaseWindow
{
.....  

其中

public class BaseWindow : Window
{
...

我的问题是这个问题的延伸。如果你有上述情况,但使用

public class BaseWindow<T> : Window
{
...

我显然也会有

public partial class Window1 : BaseWindow<SomeConcreteClass>
{
...

是否可以在xaml中表示此层次结构?如果是,xaml是什么样子的?我特别想在这里使用泛型,而不是对象。我刚刚遇到一个场景,泛型看起来很有用,但我不确定如何在涉及xaml的WPF中实现它。非常感谢。

是的,您应该在XAML标记中使用x:TypeArguments指令。

namespace WpfApp1
{
public class BaseWindow<T> : Window { }
public class SomeConcreteClass { }
}

Window1.xaml.cs:

public partial class Window1 : BaseWindow<SomeConcreteClass>
{
public Window1()
{
InitializeComponent();
}
}

Window1.xaml:

<local:BaseWindow x:Class="WpfApp1.Window1"
x:TypeArguments="local:SomeConcreteClass"
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:WpfApp1"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</local:BaseWindow>

最新更新