Winphone开发之动态加载XAML

XAML是可以动态加载的,这里所说的动态加载就是说写好了一个静态XAML,然后在运行时用C#加载到相应的地方去。

动态加载XAML注意的几点:

1)待加载的XAML只能有一个根节点,也就是说不能有多个平行的节点

2)待加载的XAML要引入命名空间xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

3)注意一下加载的语法,看下下面的代码就知道了

4)动态加载XAML其实也分为两种,一种是通过String来加载,一种是加载静态的XAML,其实加载静态的XAML是在String的基础上 改进而已,是首先把XAML加载进内存,存为String变量,然后再用String加载的方法加载进去。


下面是主界面XAML:

<phone:PhoneApplicationPage
    x:Class="XmlStudy.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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--ContentPanel - 在此处放置其他内容-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button Content="Button" HorizontalAlignment="Left" Margin="163,88,0,0" VerticalAlignment="Top" Click="Button_Click"/>

        <Grid x:Name="MyRoot" HorizontalAlignment="Left" Height="417" VerticalAlignment="Top" Width="458" Margin="12,180,0,0" Grid.Row="1"/>
    </Grid>

</phone:PhoneApplicationPage>

下面是截图:

待会我要把Button下面动态加载一个Grid控件

下面是Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using XmlStudy.Resources;
using System.IO;
using System.Windows.Resources;
using System.Windows.Markup;

namespace XmlStudy
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string xaml = "";
            StreamResourceInfo info = Application.GetResourceStream(new Uri("/XmlStudy;component/Page1.xaml" ,UriKind.Relative));
            using (StreamReader sr = new StreamReader(info.Stream))
            {
                xaml = sr.ReadToEnd();
            }
            Grid g = (Grid)XamlReader.Load(xaml);
            MyRoot.Children.Add(g);
        }

        
    }
}

上面注意一下加载的语法就行了。

下面是待加载的XAML:

<Grid Background="Transparent"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel 包含应用程序的名称和页标题-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="新添加页面" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="新添加页面" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
</Grid>


另外要注意的是这个待加载的XAML的属性要设置为Resource,默认为Page,这样才能用GetResourceStream加载进去。

然后点击按钮,加载这个GRID:

加载成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值