2009-08-04 19 views
0

の内側に私は Silverlightの挿入XAMLは他のXAML

http://www.codeplex.com/wpfbookcontrol

WPFの例は、ブック内のすべてのページは、XAMLファイルであることをことをalowsみつ

でWPFとSilverlightのBookControlsを使用していますが、Silverlightの例ではいけません。

Silverlightの例の各ブックページに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); 
     } 
    } 
} 

そして、私は含めることwnat XAMLは、このPagTeste2あるこのPage.xamlを

を持っています.xaml

特派 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. 
     } 
    } 
} 

で0

<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> 

私は誰もが知っている、なぜ

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

この行にエラーを取得していますか?

答えて

1

次のコード/プロセスを使用して動的にXAMLをロードします。

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

「SilverlightApplication1は」&「MyPage.xaml」プロジェクト名ですが、あなたが動的にロードするXAMLページです。 そして、あなたは、内側のコントロールを取得するために)(FindName指定をしなければならない:拡張、について説明

まず、XAMLフラグメントを含む文字列が含まれている必要があります

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

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

EDITを参照してくださいルート要素は1つだけです。そのノードには、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プロパティの設定属性が含まれ、「こんにちは、世界を!」次に、System.Windows.Markup名前空間のXamlReaderクラスを使用して、System.Objectを返すLoadメソッドを使用してXAMLテキストを読み込みます。 XAMLテキストのルートノードはTextBlockであるため、結果として得られるオブジェクトは実際はSystem.Windows.Controls.TextBlock型のインスタンスです。その型へのキャストは成功し、切断されたTextBlockが生成されます。

新しいTextBlockは、作成直後に他のUIElementと関連付けられていないため、「切断されました」と言います。コントロールは、キャンバスに接続され、最終的にアプリケーションに接続されるまで、実際には使用できません。ほとんどのXAMLコントロールには、UIElementCollectionであるChildrenというプロパティがあります。そのコレクションのAddメソッドを使用して、新しいTextBlockをアタッチしてレンダリングすることができます。もちろん、Loadメソッドの結果をTextBlockにキャストする必要はありません。しかし、UIElementCollection.Addメソッドを介して接続するには、ロード結果をUIElement基本クラスにキャストする必要があります。

+0

私はこのラインでは、このエラー "XAML解析の例外AG_E_PARSER_BAD_TYPE" を取得しています: System.Windows.Application.LoadComponent(この、新しい可能System.Uri( "/ SLBookDemoApp; PagTeste2.xaml"、System.UriKind.Relative) ); – Bonfocchi

+0

jsutは、ロードしようとしているXamlの構文に何か問題があることを意味します。 – mattmanser

+0

私が使用するXAMLファイルの種類は何ですか? – Bonfocchi

関連する問題