2010-12-10 43 views
3

我承认WP7应用程序开发现场的新手。我发现一段视频演示了如何做一些类似于此的事情,但无法找到它。动态XAML通过C#

基本上我想根据用户的选择更改XAML页面的内容。举一个简单的例子,可能有五个按钮编号为1-5。点击任何一个会在页面上创建许多文本框。那么有没有办法指示事件处理程序中的按钮将一些XAML代码粘贴到页面上?这是否与HTML中的CSS工作表类似?非常感谢您的时间,并帮助我解决这个问题!甚至指着我的教程或方法的名称,我可以谷歌将是有益的。

回答

1

你不会真的是“粘贴”到你的XAML文件,你必须C#代码在你的按钮处理动态地添加TextBox控件在XAML中定义的面板。

MainWindow.xaml

<phone:PhoneApplicationPage x:Class="WindowsPhoneApplication2.MainPage" 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
          xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          mc:Ignorable="d" 
          d:DesignWidth="480" 
          d:DesignHeight="768" 
          FontFamily="{StaticResource PhoneFontFamilyNormal}" 
          FontSize="{StaticResource PhoneFontSizeNormal}" 
          Foreground="{StaticResource PhoneForegroundBrush}" 
          SupportedOrientations="Portrait" 
          Orientation="Portrait" 
          shell:SystemTray.IsVisible="True"> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" 
      Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel x:Name="TitlePanel" 
        Grid.Row="0" 
        Margin="12,17,0,28"> 
      <TextBlock x:Name="ApplicationTitle" 
         Text="MY APPLICATION" 
         Style="{StaticResource PhoneTextNormalStyle}" /> 
      <TextBlock x:Name="PageTitle" 
         Text="page name" 
         Margin="9,-7,0,0" 
         Style="{StaticResource PhoneTextTitle1Style}" /> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" 
       Grid.Row="1" 
       Margin="12,0,12,0"> 
      <StackPanel> 
       <Button Click="Button_Click" 
         Content="1" /> 
       <Button Click="Button_Click" 
         Content="2" /> 
       <Button Click="Button_Click" 
         Content="3" /> 
       <Button Click="Button_Click" 
         Content="4" /> 
       <Button Click="Button_Click" 
         Content="5" /> 
       <StackPanel x:Name="DynamicPanel"> 
       </StackPanel> 
      </StackPanel> 
     </Grid> 
    </Grid> 
</phone:PhoneApplicationPage> 

MainWindow.cs:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using Microsoft.Phone.Controls; 

namespace WindowsPhoneApplication2 { 
    public partial class MainPage : PhoneApplicationPage { 
     // Constructor 
     public MainPage() { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) { 
      int numBoxes = Int32.Parse(((Button) sender).Content.ToString()); 
      DynamicPanel.Children.Clear(); 
      for (int i = 0; i < numBoxes; i++) { 
       var newTextBlock = new TextBlock { Name = "textBlock" + i.ToString(), Text = "Hello!" }; 
       DynamicPanel.Children.Add(newTextBlock); 
      } 
     } 
    } 
} 
+0

是的,“粘贴”过程非常简单。 – Dan 2010-12-10 02:31:15