views:

583

answers:

3

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 ?

A: 

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.

mattmanser
A: 

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

Tim Heuer
I Have already tried that, and it didn't work. How can I put this to work ?
Code Burn
A: 

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.

Martin Liversage