我正在使用Mitsu的WPF和Silverlight BookControls。
http://www.codeplex.com/wpfbookcontrol
WPF示例说明书中的每一页都是XAML文件,但Silverlight示例不这样做。
在Silverlight示例中的每一本书中都有加载XAML的方法吗?
我有这个Page.xaml
<UserControl x:Class="SLBookDemoApp.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SLMitsuControls;assembly=SLMitsuControls"
Width="800" Height="600" Loaded="UserControl_Loaded">
<Grid>
<local:UCBook x:Name="book" Margin="50" />
</Grid>
</UserControl>记者Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLMitsuControls;
namespace SLBookDemoApp
{
public partial class Page : UserControl, IDataProvider
{
public Page()
{
InitializeComponent();
}
private List<Grid> pages;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
/*
pages = new List<Button>
{
new Button { Content = "Page 0"},
new Button { Content = "Page 1", Background = new SolidColorBrush(Colors.Green) },
new Button { Content = "Page 2", Background = new SolidColorBrush(Colors.Yellow) },
new Button { Content = "Page 3", Background = new SolidColorBrush(Colors.Brown) },
new Button { Content = "Page 4", Background = new SolidColorBrush(Colors.Magenta) },
new Button { Content = "Page 5", Background = new SolidColorBrush(Colors.Red) }
};
*/
System.Windows.Application.LoadComponent(this, new System.Uri("/SLBookDemoApp;PagTeste2.xaml", System.UriKind.Relative));
Grid LayoutRoot = ((Grid)(FindName("LayoutRoot")));
//TextBlock testTextBlock = ((TextBlock)(FindName("testTextBlock")));
pages = new List<Grid>
{
};
pages.Add(LayoutRoot);
/*
int i = 0;
foreach (var b in pages)
{
if (i % 2 == 0)
b.Click += Button_Click;
else
b.Click += Button_Click_1;
i++;
}
*/
book.SetData(this);
}
#region IDataProvider Members
public object GetItem(int index)
{
return pages[index];
}
public int GetCount()
{
return pages.Count;
}
#endregion
private void Button_Click(object sender, RoutedEventArgs e)
{
book.AnimateToNextPage(500);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
book.AnimateToPreviousPage(500);
}
}
}我要包含的XAML是这个PagTeste2.xaml
<Grid
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SLBookDemoApp.PagTeste2"
x:Name="LayoutRoot">
<Rectangle Width="192" Height="80" Fill="#FF8F0A0A" Stroke="#FF000000" Canvas.Left="224" Canvas.Top="104"/>
</Grid>与通讯员PagTeste2.xaml.cs
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
//using System.Windows.Navigation;
using SLMitsuControls;
namespace SLBookDemoApp
{
public partial class PagTeste2
{
public PagTeste2()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
}
}我在这条线上出错了
System.Windows.Application.LoadComponent(this, new System.Uri("/SLBookDemoApp;PagTeste2.xaml", System.UriKind.Relative));,有人知道为什么吗?
发布于 2009-08-04 09:16:47
使用以下代码/进程动态加载XAML:
System.Windows.Application.LoadComponent(this,
new System.Uri("/SilverlightApplication1;component/MyPage.xaml",
System.UriKind.Relative));其中"SilverlightApplication1“是项目名称& "MyPage.xaml”是要动态加载的XAML页面。然后,您必须执行FindName()来获取内部控件:
Grid LayoutRoot = ((Grid)(FindName("LayoutRoot")));
TextBlock testTextBlock = ((TextBlock)(FindName("testTextBlock"))); 还请参见http://silverlight.net/learn/learnvideo.aspx?video=56933
编辑:扩展解释
首先,包含XAML片段的字符串必须只包含一个根元素。该节点必须包含对http://schemas.microsoft.com/client/2007的命名空间引用。如果忘记引用该命名空间,则在尝试加载XAML时将得到一个XamlParseException。例如,查看下面的C#代码,它创建了一个TextBlock对象:
string xamlText="<TextBlock xmlns=\"http://schemas.microsoft.com/client/2007\" " +
"Text=\"Hello World!" \"/>";
TextBlock txt = System.Windows.Markup.XamlReader.Load(xamlText) as TextBlock;在这段代码中,XAML文本片段包含必要的命名空间引用和一个属性,将text属性设置为"Hello!“然后使用XamlReader命名空间中的System.Windows.Markup类来使用load方法加载XAML文本,该方法返回一个System.Object。由于XAML文本的根节点是TextBlock,因此生成的对象实际上是System.Windows.Controls.TextBlock类型的实例。因此,该类型的强制转换成功,生成一个断开连接的TextBlock。
我说新的TextBlock是“断开连接”的,因为在创建它之后,它与任何其他UIElement都没有关联。控件在连接到画布并最终连接到应用程序之前是无法真正使用的。大多数XAML控件都有一个名为UIElementCollection的属性。该集合的Add方法可用于附加新的TextBlock并呈现它。当然,没有必要将Load方法的结果转换为TextBlock。但是要通过UIElementCollection.Add方法附加,您必须至少将加载结果转换为UIElement基类。
https://stackoverflow.com/questions/1226427
复制相似问题