Winphone开发之动态加载XAML
来源:互联网 发布:wordpress yoast seo 编辑:程序博客网 时间:2024/05/16 19:22
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:
加载成功
0 0
- Winphone开发之动态加载XAML
- Winphone开发之关于XAML Style的若干备注
- Winphone开发之BackgroupWorker
- Winphone开发之IsolatedStorageFile
- WinPhone开发之GeoCoordinateWatcher
- 动态加载XAML文件
- 动态加载XAML
- WPF动态加载XAML
- Winphone开发之JSON解析
- Winphone开发之保存设置
- Winphone开发之页面导航
- Winphone开发之资源字典
- 学习WinPhone开发之路
- 实现动态加载XAML文件
- WPF:动态加载xaml对象
- wp8动态加载xaml控件
- Winphone开发之下载+显示图片
- Winphone开发之数据绑定(1)
- 程序员的自我修养(1)——操作系统篇
- timestamp[] postgresql
- U-Boot 的编译
- 科锐C++视频教程
- shell中的cat和文件分界符(<<EOF)
- Winphone开发之动态加载XAML
- 当向后台插入或读取JSON数据遇见回车时
- iOS runloop
- DOS命令大全
- 爱是什么?
- 【Unity3D自学记录】代码获取隐藏游戏对象
- ehcache集群缓存配置
- Android中处理崩溃异常
- 转载 sudo apt-get 和 dpkg 包管理命令总结