2016-06-11 66 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來找出哪種控件更適​​合你的場景。