【问题标题】:Silverlight insert XAML inside other XAMLSilverlight 在其他 XAML 中插入 XAML
【发布时间】:2009-08-04 09:08:57
【问题描述】:

我正在使用 Mitsubishi 的 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));

有人知道为什么吗?

【问题讨论】:

    标签: wpf silverlight xaml silverlight-2.0


    【解决方案1】:

    使用以下代码/过程动态加载 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。例如,查看以下创建 TextBlock 对象的 C# 代码:

    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 world!”的属性。 System.Windows.Markup 命名空间中的 XamlReader 类随后用于使用返回 System.Object 的 Load 方法加载 XAML 文本。由于 XAML 文本的根节点是 TextBlock,因此生成的对象实际上是 System.Windows.Controls.TextBlock 类型的实例。因此,对该类型的强制转换成功,产生了一个断开连接的 TextBlock。

    我说新的 TextBlock 是“断开连接的”,因为它刚被创建后,就没有与任何其他 UIElement 关联。控件只有在连接到画布并最终连接到应用程序后才能真正使用。大多数 XAML 控件都有一个名为 Children 的属性,它是一个 UIElementCollection。该集合的 Add 方法可用于附加新的 TextBlock 并呈现它。当然,不需要将 Load 方法的结果强制转换为 TextBlock。但是要通过 UIElementCollection.Add 方法附加,您必须至少将 Load 结果转换为 UIElement 基类。

    【讨论】:

    • 我在这一行收到此错误“Xaml Parse Exception AG_E_PARSER_BAD_TYPE”: System.Windows.Application.LoadComponent(this, new System.Uri("/SLBookDemoApp;PagTeste2.xaml", System.UriKind .相对));
    • 这 jsut 意味着您尝试加载的 Xaml 的语法有问题。
    • 我应该使用什么样的 XAML,它看起来像什么?有什么例子吗?
    • 小心,您会遇到 XAML 兼容性问题:默认情况下,Silverlight 中没有很多控件(想想 DockPanel),即使安装了 Silverlight Toolkit,它们也会有不同的 Xmlns 前缀和程序集名称。要真正创建兼容的图书阅读格式,您可能需要对一些元素进行即时(想想 Linq)转换,以从 WPF 到 Silverlight 友好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多