从资源文件导入视图
Import view from resource file
我有一个资源文件Grids.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Label x:Key="SomeTestLabel">Hello There</Label>
<Grid x:Key="HomeGrid">
<Label Content="{StaticResource SomeTestLabel}"></Label>
</Grid>
</ResourceDictionary>
在我的 Menus.xaml
文件中有
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TabItem x:Key="HomeTab" Header="Home">
<Grid><!-- PROBLEM IS RIGHT HERE --></Grid>
</TabItem>
</ResourceDictionary>
- 是否可以 link
<Grid><!-- PROBLEM IS RIGHT HERE --></Grid>
和 <Grid x:Key="HomeGrid">...</Grid>
?
- 我可以使用更好的视图吗?
- 有更好的方法吗?
我这样做的主要原因是将代码从一个文件 (MainWindow.xaml
) 重构到各种其他文件中,这样主文件和主资源文件就不会随机混乱代码段
A <ResourceDictionary />
不是声明控件的中心位置。在这些词典之一中声明的内容将由 控件(例如样式、画笔、主题、常用图像、转换器和模板)使用。
但是,控件声明(例如 <Grid />
)始终 将由 WPF 框架呈现的特定 class 的实例。一个控件一次只能存在于一个地方,因此,将一个控件放在 ResourceDictionary
中不是一个好主意(无论如何它都不可重用)。控件声明 always 属于 UserControl
、Window
或您希望显示的其他托管控件。
我有一个资源文件Grids.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Label x:Key="SomeTestLabel">Hello There</Label>
<Grid x:Key="HomeGrid">
<Label Content="{StaticResource SomeTestLabel}"></Label>
</Grid>
</ResourceDictionary>
在我的 Menus.xaml
文件中有
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TabItem x:Key="HomeTab" Header="Home">
<Grid><!-- PROBLEM IS RIGHT HERE --></Grid>
</TabItem>
</ResourceDictionary>
- 是否可以 link
<Grid><!-- PROBLEM IS RIGHT HERE --></Grid>
和<Grid x:Key="HomeGrid">...</Grid>
? - 我可以使用更好的视图吗?
- 有更好的方法吗?
我这样做的主要原因是将代码从一个文件 (MainWindow.xaml
) 重构到各种其他文件中,这样主文件和主资源文件就不会随机混乱代码段
A <ResourceDictionary />
不是声明控件的中心位置。在这些词典之一中声明的内容将由 控件(例如样式、画笔、主题、常用图像、转换器和模板)使用。
但是,控件声明(例如 <Grid />
)始终 将由 WPF 框架呈现的特定 class 的实例。一个控件一次只能存在于一个地方,因此,将一个控件放在 ResourceDictionary
中不是一个好主意(无论如何它都不可重用)。控件声明 always 属于 UserControl
、Window
或您希望显示的其他托管控件。