我正在尝试重新使用在ResourceDictionary下定义的Grid(guiLayout.xaml)
guiLayout.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Key="guiLayout">
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
我还有 MainWindow.xaml ,
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!-- I want to use ResourceDictionary definitions here-->
</Grid>
是否可以在guiLayout.xml
中使用Mainwindow.xaml
中的网格定义?
修改:
项目看起来像这样,
答案 0 :(得分:2)
您可以像这样在XAML中设置Content
的{{1}}:
Window
或使用元素语法:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Content="{StaticResource guiLayout}">
</Window>
请注意,如果<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Content>
<StaticResource ResourceKey="guiLayout"/>
</Window.Content>
</Window>
未保存为资源x:Shared
,则Grid
仅可用于1个窗口。要将其用于多个窗口,您必须为Grid
添加x:Shared="false"
,如下所示:
Grid
更新:尝试使用此代码导入资源:
在 App.xaml :
中<Grid x:Key="guiLayout" x:Shared="false">
<!-- ... -->
</Grid>