【发布时间】:2014-08-19 10:51:05
【问题描述】:
我正在尝试建立一个包含许多不同控件的“简单”窗口。
我遇到的问题是我只完成了 10% 并且我的 XAML 编码已经占用了很多行。按照不重复代码的概念,我想知道是否有更有效的方法来保持我的代码干净、整洁和高效。
有没有类似于资源字典或类的解决方案?
提前致谢!
这是我的 XAML 代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="League_of_Legends_Alpha_1_0.MainWindow"
Title="MainWindow" Height="446" Width="1366">
<Grid Background="Black" ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="73*"></ColumnDefinition>
<ColumnDefinition Width="42*"/>
<ColumnDefinition Width="166*"/>
<ColumnDefinition Width="269*"></ColumnDefinition>
<ColumnDefinition Width="270*"></ColumnDefinition>
<ColumnDefinition Width="270*"></ColumnDefinition>
<ColumnDefinition Width="269*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="34*" MaxHeight="34"/>
<RowDefinition Height="32*" MaxHeight="32"/>
<RowDefinition Height="32*" MaxHeight="32"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="19"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="19*"/>
<RowDefinition Height="32*"/>
<RowDefinition Height="34*" MaxHeight="34"/>
<RowDefinition Height="142*"/>
<RowDefinition Height="31*"/>
</Grid.RowDefinitions>
<Viewbox Stretch="Uniform" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3">
<Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="3" Grid.Row="0">
<Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="4" Grid.Row="0">
<Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="5" Grid.Row="0">
<Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="6" Grid.Row="0">
<Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="3" Grid.Row="3">
<Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="4" Grid.Row="3">
<Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="5" Grid.Row="3">
<Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
</Viewbox>
<Viewbox Stretch="Uniform" Grid.Column="6" Grid.Row="3">
<Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
</Viewbox>
<Image HorizontalAlignment="Left" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Margin="5,0,5,0" Height="64" VerticalAlignment="Top" Width="64" Source="Blank.png" />
<Image HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" Height="32" VerticalAlignment="Top" Width="32" Source="Blank.png" Margin="5,0,0,0"/>
<Image HorizontalAlignment="Left" Grid.Row="2" Grid.Column="1" Height="32" VerticalAlignment="Top" Width="32" Source="Blank.png" Margin="5,0,0,0"/>
<Image Grid.Column="1" HorizontalAlignment="Left" Height="32" Margin="5,3,0,0" Grid.Row="3" Grid.RowSpan="2" VerticalAlignment="Top" Width="32" Source="Blank.png"/>
<Viewbox Grid.Column="2" Grid.Row="1" Stretch="UniformToFill">
<Label Content="Runes" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" Width="166" Height="31" FontWeight="Bold"/>
</Viewbox>
<Viewbox Grid.Column="2" Grid.Row="2" Stretch="Fill">
<Label Content="Masteries: 21/0/9" Grid.Column="2" HorizontalAlignment="Left" Height="32" Grid.Row="2" VerticalAlignment="Top" Width="166" Foreground="White" HorizontalContentAlignment="Center" FontWeight="Bold"/>
</Viewbox>
<Label Content="Challenger IV (100LP)" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="3" VerticalAlignment="Top" Width="161" Foreground="White" FontSize="9" FontWeight="Bold"/>
<Label Content="K/D/A" HorizontalAlignment="Left" Grid.Row="3" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="9" FontWeight="Bold"/>
<Label Content="Label" HorizontalAlignment="Left" Margin="10,5,0,0" Grid.Row="4" VerticalAlignment="Top"/>
<Label Content="Games: 10000" HorizontalAlignment="Left" Height="19" Grid.Row="4" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="8.5" FontWeight="Bold"/>
<Label Content="Win Ratio:" HorizontalAlignment="Left" Height="20" Grid.Row="5" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="9" FontWeight="Bold"/>
<Label Content="Last Season: Challenger IV (100LP)" Grid.Column="2" HorizontalAlignment="Left" Height="20" Grid.Row="5" VerticalAlignment="Top" Width="166" FontSize="8.5" Background="Black" Foreground="White" Margin="0,1,0,0" Grid.RowSpan="2" FontWeight="Bold"/>
<Label Content="Promotion Series: 3W/1L" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="4" VerticalAlignment="Top" Width="166" Foreground="White" FontSize="8.5" Height="19" FontWeight="Bold"/>
<Label Content="100.5%" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="5" VerticalAlignment="Top" Height="20" Width="42" Foreground="White" FontSize="9" FontWeight="Bold"/>
<Label Content="Normal Wins:" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="8" FontWeight="Bold"/>
<Label Content="10000" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Height="19" Width="42" Foreground="White" FontSize="8" FontWeight="Bold"/>
<Label Content="OP.GG" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Width="168" Foreground="White" FontSize="8" Grid.ColumnSpan="2" FontWeight="Bold"/>
<!--
<Border Grid.Column="0" Grid.RowSpan="2" BorderBrush="Aqua" BorderThickness="5" CornerRadius="8"/>
-->
</Grid>
</Window>
编辑/回答:
我首先做的是创建一个 ResoureDictionary 并添加一个带有网格的数据模板。
<DataTemplate x:Key="myDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="4*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
</grid>
然后在我的主窗口中,我使用 ContentControl 将数据模板绑定到我的网格。
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition/>
<RowDefinition Height="32"/>
<RowDefinition/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="0" Grid.Row="1"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="1" Grid.Row="1"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="2" Grid.Row="1"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="3" Grid.Row="1"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="4" Grid.Row="1"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="0" Grid.Row="3"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="1" Grid.Row="3"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="2" Grid.Row="3"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="3" Grid.Row="3"></ContentControl>
<ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="4" Grid.Row="3"></ContentControl>
</Grid>
同样对于像我这样不熟悉 WPF 的人,您还需要引用资源(因为我使用了资源字典)
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainDataTemplateDictionary.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
【问题讨论】:
-
请参阅 MSDN 上的Control Authoring Overview 页面。特别是,您可能对
UserControl感兴趣,以封装 UI 的可重用部分。 -
请发布您的 UI 图片。以便我们提出更好的方法
-
link 这是用户界面的截图;最初,由于声誉低,我无法发布图片。
-
顺便说一下,请注意区分“34”和“34*”(在您的实际代码中34* = 总宽度的8%)
-
为了简化你的
Grid(RowDefinitions和ColumnDefinition)的定义,我推荐使用SimpleGrid:thomaslevesque.fr/2010/07/20/…