2016-06-11 75 views
0

我一直在用Delphi一年已经和现在的我会开发一些C#UWP。
我已经做了一些工作,我可以看到,在我的XAML也有像1000 lines..BECAUSE我要做10“小组”正好同文,只有改变他们的名字,例如:
如何在xaml中不重复代码?

<StackPanel Name="Stack1"> 
    <TextBlock Name="Text1"/> 
    <TextBox Name="Box1" 
</StackPanel> 
<StackPanel Name="Stack2"> 
    <TextBlock Name="Text2"/> 
    <TextBox Name="Box2" 
</StackPanel> 

这只是一个例子,实际上我的代码每个'StackPanel'有100行。

所以..用几行
不然我就必须把它写10次,并使用1000线那里有一种方法可以做到只有1“的StackPanel”,并用它作为XAML代码的类或某事10倍

+0

SO不想N合1的问题,其中n> 1。而你的第一个问题,真的是很有限的用途。您不会根据使用频率来选择控件。你有要求并选择最佳匹配。鉴于所提供的信息,第二个问题是无法回答的。 – IInspectable

+0

@IInspectable你好,因为第一个是“真/假”的问题,我认为这是毫无意义的创造另一个问题,对不起 – GuiPetenuci

+0

的第一个问题是没有意义的要求,无论它是另一个问题的一部分,或立场 - 单独的问题。询问任何特定控制的受欢迎程度是毫无意义的。选择符合您要求的那个。如果你不知道你的要求,你应该回到绘图板。 – IInspectable

回答

1

您可以随时使用旧的用户控件。在这里,我为你提供了一个例子:

<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> 

你可以用它在你的看法是这样的:

<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> 

如果你的用户控件位于你必须将其导出这样的文件夹内。

xmlns:usercontrol="using:App1.UserControlsFolder" 

你是哪个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" 
    xmlns:usercontrol="using:App1.UserControlsFolder" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <usercontrol:ReusableCode x:Name="ucReusableCode" /> 
    </Grid> 
</Page> 

希望这回答了你的问题。

+0

谢谢,后来回答抱歉,但这也会起作用,现在即时使用C#每天工作(我正在使用delphi)和家庭,即时了解很多事情,谢谢 – GuiPetenuci

2

我认为你需要的是ItemsControl,或者从ItemsControl得到的控制像ListView。例如这里:

<ItemsControl ItemsSource="{x:Bind itemscontrolCollection}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding MyText}" /> 
       <TextBox /> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

后面的代码:

private ObservableCollection<ItemsControlList> itemscontrolCollection; 

public MainPage() 
{ 
    this.InitializeComponent(); 
    itemscontrolCollection = new ObservableCollection<ItemsControlList>(); 
} 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    itemscontrolCollection.Clear(); 
    for (int i = 1; i <= 100; i++) 
    { 
     itemscontrolCollection.Add(new ItemsControlList { MyText = "Text" + i }); 
    } 
} 

ItemsControlList类:

public class ItemsControlList 
{ 
    public string MyText { get; set; } 
} 

我不知道为什么你设置的名字给每个StackPanelTextBlockTextBox这里,您可以使用data binding将值设置为每个控件的依赖项属性。

您可以检查官方ListView and GridView sample以检查如何在UWP应用程序中使用ListView。你也可以参考UI basics (XAML) sample来找出哪种控件更适​​合你的场景。