从资源文件导入视图

时间:2015-06-25 04:16:02

标签: c# wpf xaml layout resources

我有一个资源文件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)和其他各种文件,这样主文件和主资源文件就不会变得杂乱无章随机代码

1 个答案:

答案 0 :(得分:1)

<ResourceDictionary />不是宣称控制的中心位置。其中一个词典中声明的内容是 by 控件(例如样式,画笔,主题,常用图像,转换器和模板)。

但是,控件声明(例如<Grid />总是将由WPF框架呈现的特定类的实例。控件一次只能存在于一个地方,因此,将一个控件置于ResourceDictionary内并不是一个好主意(无论如何它都不可重用)。控制声明始终属于UserControlWindow或您希望其显示的其他主机控件。