通过C#实现动态XAML

3

我承认我对WP7应用程序开发领域还很新。以前我找到了一个视频,展示了如何做类似于这样的事情,但现在无法找到它。

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

2个回答

1

你不会真的“粘贴”到你的XAML文件中,你会在按钮处理程序中有C#代码,动态地向在你的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);
            }
        }
    }
}

是的,“粘贴”非常简化了这个过程。 - Dan
如果我想使用我自己编写的特定代码块,而不是动态生成的代码块,该怎么办呢?例如,如果我想在按钮单击事件中将<TextBlock Name="textBlock1" Text="Hello!">放入<StackPanel x:Name="TextBoxes">中,我应该如何在代码后台文件中编写代码? - Dan
更新。理解XAML本身只是一种声明性的创建和组合对象的方式。您可以在XAML中做的任何事情也可以在代码中完成(反之亦然,到一定程度上)。 - lesscode

0
正如Wayne所指出的,在许多情况下,使用C#在运行时生成控件是很简单的。
或者,如果您确实想要研究在运行时生成XAML,这里有一篇Pete Brown的文章可以帮助您入门。 在WPF和Silverlight中动态生成控件

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接