winphone动态生成textBlock、image等控件
2013-09-05 22:53
246 查看
若在cs代码中如XAML生成控件一样简单就好了。
当然!办法永远都比困难多。
引用命名空间:
using System.Windows.Markup;// XamlReader
在需要动态生成textBlock的地方写如下代码:
//可以先在form上拖一个自己想要的textBlock,之后复制下来它的相关XAML语言,放到一个string s里面
string s = @"<TextBlock
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
FontSize='{StaticResource PhoneFontSizeMediumLarge}'
VerticalAlignment='Top'
Height='40'
Margin='34,34,0,0'
Name='tb'
Text='地区:'
/>";
//通过XamlReader,我们可以将s读出来,并转换成相应的object
TextBlock tbArea = XamlReader.Load(s) as TextBlock;
后续:
我们可以将创建好的这个tb加入到Grid里面,代码如下:
Grid gd = new Grid();
gd.Children.Add(tbArea);
若想动态生成一个image,只需转换string s即可
string sImage =
@"<Image
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
Height='90'
HorizontalAlignment='Left'
Margin='40,232,0,0'
Name='image'
Stretch='Uniform'
VerticalAlignment='Top'
Width='90' />";
注意两点
1:
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
这两句话一定不能丢,否则会转换报错!
2:
原XAML语言中的“”号在string中要换成‘’;
当然!办法永远都比困难多。
引用命名空间:
using System.Windows.Markup;// XamlReader
在需要动态生成textBlock的地方写如下代码:
//可以先在form上拖一个自己想要的textBlock,之后复制下来它的相关XAML语言,放到一个string s里面
string s = @"<TextBlock
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
FontSize='{StaticResource PhoneFontSizeMediumLarge}'
VerticalAlignment='Top'
Height='40'
Margin='34,34,0,0'
Name='tb'
Text='地区:'
/>";
//通过XamlReader,我们可以将s读出来,并转换成相应的object
TextBlock tbArea = XamlReader.Load(s) as TextBlock;
后续:
我们可以将创建好的这个tb加入到Grid里面,代码如下:
Grid gd = new Grid();
gd.Children.Add(tbArea);
若想动态生成一个image,只需转换string s即可
string sImage =
@"<Image
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
Height='90'
HorizontalAlignment='Left'
Margin='40,232,0,0'
Name='image'
Stretch='Uniform'
VerticalAlignment='Top'
Width='90' />";
注意两点
1:
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
这两句话一定不能丢,否则会转换报错!
2:
原XAML语言中的“”号在string中要换成‘’;
相关文章推荐
- 使用Type.GetType 动态生成 TextBlock 等控件
- C#如何获得动态生成的控件
- Android动态生成控件如何取值
- Microsoft,WEB控件树(TreeView)的动态生成.
- ASP.NET 后台动态生成CheckBOx控件并获取选中的值
- C#代码生成控件动态效果-XAML篇
- VC中动态生成控件
- C#获取动态生成的控件值
- 在VC程序中实现控件的动态生成与响应
- 获取ARRAYLIST 的某一列以及动态生成控件的方法
- VC程序中实现控件的动态生成与响应
- Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用
- Asp.net中使用文本框的值动态生成控件的方法
- 如何动态生成页面控件,并取到值?(.net)
- CEGUI 动态生成 texture imageset image
- 为动态生成的控件添加响应事件
- VC程序中实现控件的动态生成与响应(一)
- SolpartMenu的使用:(二)、在ASP.NET页面中使用SolpartMenu控件之动态的加载数据库中的数据来生成菜单
- 在asp.net中动态生成web控件