XAML页面翻转扭曲

发布于 2024-07-30 00:02:13 字数 4195 浏览 6 评论 0原文

我有这个 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 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技术交流群

发布评论

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

评论(3

贵在坚持 2024-08-06 00:02:13

请使用此选项:

this.Content = new PagTeste2();

如果您要从不同的程序集加载内容,则只需执行任何类型的程序集加载,即使如此,您也不会使用它来设置内容。

如果您实际上是在问如何动态加载程序集,MS 有一个示例

Use this instead:

this.Content = new PagTeste2();

You only have to do any sort of assembly loading if you're loading content from a different assembly and even then you wouldn't use it to set content.

If you're actually asking how do you dynamically load an assembly, MS have an example of how.

暮年慕年 2024-08-06 00:02:13

您可能想尝试/SLBookDemoApp;component/PageTeste2.xaml。

You may want to try /SLBookDemoApp;component/PageTeste2.xaml.

感悟人生的甜 2024-08-06 00:02:13

如果 PagTeste2.xaml 位于项目的顶级文件夹中,您可以使用以下代码加载它:

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

如果您已将 PagTeste2.xaml 放在项目内的子文件夹中 (例如文件夹 Tests),您需要在 uri 中包含文件的路径:

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

另外,请密切注意拼写。 PagTest2.xamlPageTeste2.xamlPageTest2.xaml 不同。 显然Test被插入在Page中的e之前。

您可以在 MSDN 上阅读有关 Pack URI 的更多信息

If PagTeste2.xaml is at the top-level folder of your project you can load it using this code:

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

If you have placed PagTeste2.xaml in a subfolder inside your project (say folder Tests) you need to include the path to the file in the uri:

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

Also, pay close attention to spelling. PagTest2.xaml is different from PageTeste2.xaml and PageTest2.xaml. Apparently Test is inserted before the e in Page.

You can read more about pack URI's on MSDN.

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