Win universal app 我可以做些什么来避免在xaml中重复代码?
我已经与delphi合作一年了,现在我将开发一些C#UWP。Win universal app 我可以做些什么来避免在xaml中重复代码?,win-universal-app,uwp,windows-10-universal,uwp-xaml,Win Universal App,Uwp,Windows 10 Universal,Uwp Xaml,我已经与delphi合作一年了,现在我将开发一些C#UWP。 我已经做了一些工作,我可以看到在我的XAML中有大约1000行。因为我必须做10个完全相同的“面板”,只更改它们的名称,例如: <StackPanel Name="Stack1"> <TextBlock Name="Text1"/> <TextBox Name="Box1" </StackPanel> <StackPanel Name="Stack2">
我已经做了一些工作,我可以看到在我的XAML中有大约1000行。因为我必须做10个完全相同的“面板”,只更改它们的名称,例如:
<StackPanel Name="Stack1">
<TextBlock Name="Text1"/>
<TextBox Name="Box1"
</StackPanel>
<StackPanel Name="Stack2">
<TextBlock Name="Text2"/>
<TextBox Name="Box2"
</StackPanel>
您始终可以使用良好的旧用户控件。在这里,我为您提供了一个示例:
<UserControl
x:Class="App1.ReusableCode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Text="SomeText" />
<TextBlock Text="Some Text 2" />
</StackPanel>
<StackPanel Name="stackPanel2" Grid.Row="1">
<TextBlock Text="SomeText 3" />
<TextBlock Text="Some Text 4" />
</StackPanel>
<StackPanel Name="stackPanel3" Grid.Row="2">
<TextBlock Text="SomeText 5" />
<TextBlock Text="Some Text 6" />
</StackPanel>
<StackPanel Name="stackPanel4" Grid.Row="3">
<TextBlock Text="SomeText 7" />
<TextBlock Text="Some Text 8" />
</StackPanel>
</Grid>
</UserControl>
它位于页面xaml中,可以这样调用:
<Page
x:Class="App1.MainPage"
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:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:ReusableCode x:Name="ucReusableCode" />
</Grid>
</Page>
<Page
x:Class="App1.MainPage"
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:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:usercontrol="using:App1.UserControlsFolder"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<usercontrol:ReusableCode x:Name="ucReusableCode" />
</Grid>
</Page>
希望这能回答您的问题。您可以始终使用良好的旧用户控件。在这里,我为您提供了一个示例:
<UserControl
x:Class="App1.ReusableCode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Text="SomeText" />
<TextBlock Text="Some Text 2" />
</StackPanel>
<StackPanel Name="stackPanel2" Grid.Row="1">
<TextBlock Text="SomeText 3" />
<TextBlock Text="Some Text 4" />
</StackPanel>
<StackPanel Name="stackPanel3" Grid.Row="2">
<TextBlock Text="SomeText 5" />
<TextBlock Text="Some Text 6" />
</StackPanel>
<StackPanel Name="stackPanel4" Grid.Row="3">
<TextBlock Text="SomeText 7" />
<TextBlock Text="Some Text 8" />
</StackPanel>
</Grid>
</UserControl>
它位于页面xaml中,可以这样调用:
<Page
x:Class="App1.MainPage"
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:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:ReusableCode x:Name="ucReusableCode" />
</Grid>
</Page>
<Page
x:Class="App1.MainPage"
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:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:usercontrol="using:App1.UserControlsFolder"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<usercontrol:ReusableCode x:Name="ucReusableCode" />
</Grid>
</Page>
希望这能回答您的问题。我想您需要的是一个,或者是从ItemsControl
派生的控件。例如:
代码隐藏:
private observeCollection items控制集合;
公共主页()
{
this.InitializeComponent();
itemscontrolCollection=新的ObservableCollection();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
itemscontrolCollection.Clear();
对于(int i=1;i我认为您需要的是a,或者从ItemsControl
派生的控件。例如:
代码隐藏:
private observeCollection items控制集合;
公共主页()
{
this.InitializeComponent();
itemscontrolCollection=新的ObservableCollection();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
itemscontrolCollection.Clear();
对于(int i=1;我不想要n-in-1的问题,其中n>1。您的第一个问题的用处非常有限。您不会根据控件的使用频率来选择控件。您有需求并选择最佳匹配项。鉴于提供的信息,第二个问题无法回答。@i不可预知的您好,因为第一个是“对/错"问题,我认为创建另一个问题是没有意义的,对不起,第一个问题问起来是没有意义的,不管它是另一个问题的一部分,还是一个独立的问题。问任何特定控件有多受欢迎都没有意义。选择一个符合您要求的。如果您不知道您的要求,您应该uld回到绘图板上。@I可检测的好,我删除了那个,尽管我很好奇MoneyApp或任何其他Win10应用程序中使用最多的是哪一个,或者他们在MoneyApp或任何其他Win10应用程序中使用过哪一个。如果他们混合面板或继续使用一个。“他们在[…]任何其他Win10应用程序中使用过哪一个?”-每个控件都曾在一个或多个应用程序中使用过。现在,这已经过时了,你可以专注于真正的问题。因此,你不希望出现n-in-1问题,其中n>1。你的第一个问题的用处非常有限。你不会根据控件的使用频率来选择控件。你有要求,并选择最佳匹配项。sec鉴于提供的信息,第二个问题无法回答。@i不可预知的您好,因为第一个是“真/假”问题,我认为创建另一个问题是没有意义的,对不起,第一个问题问起来是没有意义的,不管它是另一个问题的一部分,还是一个独立的问题。问任何特定控件有多受欢迎都没有意义。选择一个符合您要求的。如果您不知道您的要求,您应该uld回到绘图板上。@I可检测的好,我删除了那个,尽管我很好奇MoneyApp或任何其他Win10应用程序中使用最多的是哪一个,或者他们在MoneyApp或任何其他Win10应用程序中使用过哪一个。如果他们混合面板或继续使用一个。“他们在[…]任何其他Win10应用程序中使用过哪一个?”-每个控件都已在一个或多个应用程序中使用。现在,这已经不存在了,您可以专注于实际问题。谢谢,很抱歉稍后的回答,但这也会起作用,现在我每天都在工作中使用C(我在使用delphi)在家里,我现在知道很多事情,谢谢谢谢,很抱歉稍后的回答,但这也会起作用,现在我每天在工作中使用C#(我使用的是delphi)和在家里,我现在知道很多事情,谢谢