你如何将XAML分离出来,这样它不仅仅是一个文件中所有东西的混搭?
有很多种方法,包括创建一个单独的UserControl,CustomControl,Page或者Window
例如,如果你想拉一些XAML出你的MainWindow.xaml,你可以创建一个UserControl(右击项目,添加,新项...,用户控件(WPF) )被称为MyUserControl.xaml是这样的:
<UserControl x:Class="WpfApplication1.MyUserControl"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid>
<TextBlock>This is from a different XAML file.</TextBlock>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
然后在MainWindow.xaml这样使用此控件:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myControls="clr-namespace:WpfApplication1">
<Grid>
<myControls:MyUserControl/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
请注意,您需要添加对您的命名空间的引用 UserControl
xmlns:myControls="clr-namespace:WpfApplication1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2225 次 |
| 最近记录: |