如何在 WinUI 3 中使用 Xaml 继承?
How to use Xaml inheritance in WinUI 3?
从 answers like this 来看,xaml 继承似乎适用于 WPF 和 UWP。
它适用于 WinUI 3 吗?
使用这样的代码:
<local:YourBaseClass x:Class="MyApp.ChildClass"
...
xmlns:local="clr-namespace:MyApp">
</local:YourBaseClass>
我收到错误:
Error WMC0001 Unknown type 'YourBaseClass' in XML namespace 'clr-namespace:MyApp'
Does it work for WinUI 3?
是的。几乎一模一样
创建继承自 Microsoft.UI.Xaml.Window
的基础 class:
public class YourBaseClass : Microsoft.UI.Xaml.Window
{
public YourBaseClass() : base()
{
Title = "Title...";
}
}
修改MainWindow.xaml.cs继承新基class:
public sealed partial class MainWindow : YourBaseClass
{
public MainWindow()
{
this.InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
}
}
修改MainWindow.xaml中的根元素:
<local:YourBaseClass
x:Class="WinUI3App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</local:YourBaseClass>
从 answers like this 来看,xaml 继承似乎适用于 WPF 和 UWP。
它适用于 WinUI 3 吗?
使用这样的代码:
<local:YourBaseClass x:Class="MyApp.ChildClass"
...
xmlns:local="clr-namespace:MyApp">
</local:YourBaseClass>
我收到错误:
Error WMC0001 Unknown type 'YourBaseClass' in XML namespace 'clr-namespace:MyApp'
Does it work for WinUI 3?
是的。几乎一模一样
创建继承自
Microsoft.UI.Xaml.Window
的基础 class:public class YourBaseClass : Microsoft.UI.Xaml.Window { public YourBaseClass() : base() { Title = "Title..."; } }
修改MainWindow.xaml.cs继承新基class:
public sealed partial class MainWindow : YourBaseClass { public MainWindow() { this.InitializeComponent(); } private void myButton_Click(object sender, RoutedEventArgs e) { myButton.Content = "Clicked"; } }
修改MainWindow.xaml中的根元素:
<local:YourBaseClass x:Class="WinUI3App.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinUI3App" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button x:Name="myButton" Click="myButton_Click">Click Me</Button> </StackPanel> </local:YourBaseClass>