Silverlight 在其他 XAML 中插入 XAML

发布于 2024-07-30 06:14:47 字数 4486 浏览 2 评论 0原文

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

有人知道为什么吗?

I am using WPF and Silverlight BookControls by Mitsu

http://www.codeplex.com/wpfbookcontrol

The WPF example alows that every page in the book to be a XAML file, but the Silverlight example dont.

Is there a way load a XAML in every book page in the Silverlight example ?


I have this 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>

And the correspondent 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);
        }
    }
}

And the XAML I wnat to include is this 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>

With the correspondent 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.
        }
    }
}

I am getting an error on this line

System.Windows.Application.LoadComponent(this, new System.Uri("/SLBookDemoApp;PagTeste2.xaml", System.UriKind.Relative));

Anyone knows why ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-08-06 06:14: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。 作为示例,请查看以下创建 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 基类。

Load the XAML dynamically by using the following code/process:

System.Windows.Application.LoadComponent(this,
new System.Uri("/SilverlightApplication1;component/MyPage.xaml",
System.UriKind.Relative));

where "SilverlightApplication1" is the project name & "MyPage.xaml" is the XAML page you want to load dynamically.
Then you have to do FindName() to get the inner controls:

Grid LayoutRoot = ((Grid)(FindName("LayoutRoot")));
TextBlock testTextBlock = ((TextBlock)(FindName("testTextBlock"))); 

also see
http://silverlight.net/learn/learnvideo.aspx?video=56933

EDIT: Expanded explaination

First of all, the string containing the XAML fragment must contain only one root element. And that node must include a namespace reference to http://schemas.microsoft.com/client/2007. If you forget to reference that namespace, you'll get a XamlParseException when you attempt to load the XAML. As an example, look at the following C# code which creates a TextBlock object:

string xamlText="<TextBlock xmlns=\"http://schemas.microsoft.com/client/2007\" " +
"Text=\"Hello World!" \"/>";
TextBlock txt = System.Windows.Markup.XamlReader.Load(xamlText) as TextBlock;

In this code, the XAML text fragment contains the necessary namespace reference and an attribute setting the Text property to "Hello world!" The XamlReader class in the System.Windows.Markup namespace is then used to load up the XAML text using the Load method which returns a System.Object. Since the root node of the XAML text was a TextBlock, the resultant object is actually an instance of the System.Windows.Controls.TextBlock type. So the cast to that type succeeds, yielding a disconnected TextBlock.

I say that the new TextBlock is "disconnected" because just after it is created, it is not associated with any other UIElement. Controls are not really usable until they are connected to a canvas and ultimately to an application. Most XAML controls have a property called Children which is a UIElementCollection. That collection's Add method can be use to attach the new TextBlock and render it. Of course, casting the result of the Load method to a TextBlock isn't necessary. But to be attached via the UIElementCollection.Add method, you'll have to cast the Load result to the UIElement base class at a minimum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文