我有一个资源文件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>
<Grid><!-- PROBLEM IS RIGHT HERE --></Grid>
与<Grid x:Key="HomeGrid">...</Grid>
?我这样做的主要原因是将代码重构为一个文件(MainWindow.xaml
)和其他各种文件,这样主文件和主资源文件就不会变得杂乱无章随机代码
答案 0 :(得分:1)
<ResourceDictionary />
不是宣称控制的中心位置。其中一个词典中声明的内容是 by 控件(例如样式,画笔,主题,常用图像,转换器和模板)。
但是,控件声明(例如<Grid />
)总是将由WPF框架呈现的特定类的实例。控件一次只能存在于一个地方,因此,将一个控件置于ResourceDictionary
内并不是一个好主意(无论如何它都不可重用)。控制声明始终属于UserControl
,Window
或您希望其显示的其他主机控件。