简体   繁体   English

动态XAML通过C#

[英]Dynamic XAML Through C#

I'm admittedly new to the WP7 App development scene. 我无疑是WP7 App开发领域的新手。 I had found a video showing how to do something similar to this a while back, but can't locate it anymore. 我发现了一段视频,展示了如何做一些类似于此的事情,但不能再找到它了。

Essentially I want to change the contents of a XAML page based on a selection by the user. 基本上我想根据用户的选择更改XAML页面的内容。 For a simple example, there could be five buttons numbered 1-5. 举一个简单的例子,可能有五个按钮编号为1-5。 Clicking on whichever one would create that many textboxes on a page. 单击任何一个将在页面上创建许多文本框。 So is there a way to instruct the button within the event handler to paste some XAML code to the page? 那么有没有办法指示事件处理程序中的按钮将一些XAML代码粘贴到页面? Does this work similarly to CSS sheets in HTML? 这是否与HTML中的CSS表格类似? Thanks so much for your time, and helping me with this question! 非常感谢您的时间,并帮助我解决这个问题! Even pointing me to a tutorial or the name of the method I could Google would be helpful. 甚至指向我的教程或方法的名称,我可以谷歌将有所帮助。

You wouldn't really be "pasting" into your XAML file, you'd have C# code in your button handler to dynamically add TextBox controls to a Panel defined in your XAML. 你不会真正“粘贴”到你的XAML文件中,你的按钮处理程序中有C#代码,可以动态地将TextBox控件添加到XAML中定义的Panel中。

MainWindow.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: 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);
            }
        }
    }
}

As Wayne points out it is straight forward in many cases to generate controls at runtime in C#. 正如Wayne所指出的,在许多情况下,在C#中在运行时生成控件是很简单的。

Alternatively, if you did want to look into generating XAML at runtime, here is a post by Pete Brown that will get you started. 或者,如果您确实想要在运行时查看生成XAML,请参阅Pete Brown的一篇文章,它将帮助您入门。

Dynamically Generating Controls in WPF and Silverlight 在WPF和Silverlight中动态生成控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM