질문하기질문하기
 

답변됨Load XAML File dynamically

  • 2006년 3월 17일 금요일 오전 10:28Xavier ALVARO 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Is this possible?   

    I'm Beginner in winFX programming and I want to load a xaml file dynamically.   

    For example I make an event handler which load the new Xaml Description from a uri (generated by another application) on event catched and load the new interface in a panel.

    Have you an example or a tutorial on it?

    Thanks for your help

     

답변

  • 2006년 3월 17일 금요일 오후 2:37viliescu중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Using XamlReader you can load any element and set it as the content of your window/page.

    this.Content = XamlReader.Load(stream); XamlReader.
  • 2006년 3월 17일 금요일 오후 8:18Ashish Shetty - MSFT중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    There is no support for dynamically loading (or compiling) code behind or inline code with loose XAML.

    There are a couple ways you can work around this:

    1. Hook the event handlers via code,
      e.g something like:

      Page page = (Page)System.Windows.Markup.XamlReader.Load(xmlReader);

      TextBox tb = page.FindName("firstName") as TextBox;

      if (tb != null)
      tb.Text = "Joe";

      Button btnOk = page.FindName("btnOk") as Button;

      if (btnOk != null)
      btnOk.Click += new RoutedEventHandler(btnOk_Click); // and add the btnOk_Click event handler below


      or
    2. use a custom page with component, and hook the event handlers via component code

모든 응답

  • 2006년 3월 17일 금요일 오후 1:15_Michael_ 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Have a look in the SDK WinFX samples for FlowDocument_LoadSave sample.  It does more or less what you need I think.

    Michael

  • 2006년 3월 17일 금요일 오후 2:23Xavier ALVARO 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Thanks,

    I tried this sample but it can only load FlowDocument, what I want to do is to load a panel with Button, Label etc.

    I'll try to modify the sample.

     

     

  • 2006년 3월 17일 금요일 오후 2:37viliescu중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Using XamlReader you can load any element and set it as the content of your window/page.

    this.Content = XamlReader.Load(stream); XamlReader.
  • 2006년 3월 17일 금요일 오후 3:41Xavier ALVARO 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Thanks :)

    I use XamlReader and it work but not completely, it "draw" my interface but don't rely with "functions".

    This a example of my problem :

    I have a cs file with a function which is described here :

    private void CompileXaml(String xamlDocument)

    {

    StringReader stringReader = null;

    XmlTextReader xmlReader = null;

    try

    {

    stringReader = new StringReader(xamlDocument);

    xmlReader = new XmlTextReader(stringReader);

    UIElement documentRoot = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);

    interactionWindow.child = documentRoot;

    }

    catch (Exception ex)

    {}

    finally

    {

    if (stringReader != null) stringReader.Close();

    if (xmlReader != null) xmlReader.Close();

    }

    }

    private void Close(object sender, RoutedEventArgs e)  { this.Close(); }

    and i want to load dynamically the xaml description with xamlReader ( for example  <Button Name="btnOk" Click="Close">  or more complicated interaction ) with xamlreader, I can just load <Button Name="btnOk">

    it says "you must compile", I'm really obligated?

     

  • 2006년 3월 17일 금요일 오후 8:18Ashish Shetty - MSFT중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    There is no support for dynamically loading (or compiling) code behind or inline code with loose XAML.

    There are a couple ways you can work around this:

    1. Hook the event handlers via code,
      e.g something like:

      Page page = (Page)System.Windows.Markup.XamlReader.Load(xmlReader);

      TextBox tb = page.FindName("firstName") as TextBox;

      if (tb != null)
      tb.Text = "Joe";

      Button btnOk = page.FindName("btnOk") as Button;

      if (btnOk != null)
      btnOk.Click += new RoutedEventHandler(btnOk_Click); // and add the btnOk_Click event handler below


      or
    2. use a custom page with component, and hook the event handlers via component code
  • 2006년 3월 19일 일요일 오후 2:21Xavier ALVARO 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Big thanks !!!!

    this is what i need !

     

  • 2006년 3월 29일 수요일 오후 9:49Jalen 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Hi Ashish,

    Can you give an example for the 2nd option - using custom page with component? Thanks!

  • 2006년 12월 19일 화요일 오전 11:00simsod 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    I Second that motion! :)
  • 2007년 6월 12일 화요일 오전 10:29Khalid Naseem 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi Guys!

    I wish to create XAML files and have a single code behind for all the XAML files. How can I achieve this? This will help me in creating an engine which will contain the code for a common code behind and I would be able to introduce multiple XAML files to the engine and they would run with all its event handlers being picked from the common code behind. Please help me out on this.

  • 2007년 6월 12일 화요일 오후 2:27Ryan Cromwell 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    For those interested I used the following code snippet to "skin" my app.

    Code Snippet

    ResourceDictionary rd = null;
    rd = Application.LoadComponent( new Uri( uri, UriKind.RelativeOrAbsolute ) ) as ResourceDictionary;

    if( rd != null )
        Application.Current.Resources = rd;



    I found this through Alan Le's blog post.
  • 2007년 12월 6일 목요일 오후 3:49niketu 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    I am trying to create a XBAP application and I am having problems defining proper permissions to read a XAML file.

     

    Also, I tried this code using just a XML string, the code goes through but the Page does not change, I do not see the new controls loaded?

     

    How do you load a custom loaded page on the current page? Or do you have to navigate to the custom loaded page, if so how?

     

    Thank you for your help.

  • 2008년 11월 5일 수요일 오전 10:43ShadowmanX 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
    Ashish Shetty - MSFT said:

    There is no support for dynamically loading (or compiling) code behind or inline code with loose XAML.

    There are a couple ways you can work around this:

    1. Hook the event handlers via code,
      e.g something like:

      Page page = (Page)System.Windows.Markup.XamlReader.Load(xmlReader);

      TextBox tb = page.FindName("firstName") as TextBox;

      if (tb != null)
      tb.Text = "Joe";

      Button btnOk = page.FindName("btnOk") as Button;

      if (btnOk != null)
      btnOk.Click += new RoutedEventHandler(btnOk_Click); // and add the btnOk_Click event handler below


      or
    2. use a custom page with component, and hook the event handlers via component code

    I tried this, but I always get an exception:

    System.Windows.Markup.XamlParseException: Ungültige Daten auf Stammebene. Zeile 1, Position 1. ---> System.Xml.XmlException: Ungültige Daten auf Stammebene. Zeile 1, Position 1.
       bei System.Xml.XmlTextReaderImpl.Throw(Exception e)
       bei System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
       bei System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
       bei System.Xml.XmlTextReaderImpl.ParseDocumentContent()
       bei System.Xml.XmlTextReaderImpl.Read()
       bei System.Xml.XmlTextReader.Read()
       bei System.Windows.Markup.XmlCompatibilityReader.Read()
       bei System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode)
       --- Ende der internen Ausnahmestapelüberwachung ---
       bei System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException)
       bei System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode)
       bei System.Windows.Markup.XamlParser.ReadXaml(Boolean singleRecordMode)
       bei System.Windows.Markup.TreeBuilderXamlTranslator._Parse()
       bei System.Windows.Markup.XamlParser.Parse()
       bei System.Windows.Markup.XamlTreeBuilder.ParseFragment()
       bei System.Windows.Markup.TreeBuilder.Parse()
       bei System.Windows.Markup.XamlReader.XmlTreeBuildDefault(ParserContext pc, XmlReader reader, Boolean wrapWithMarkupCompatReader, XamlParseMode parseMode, Boolean etwTracingEnabled)
       bei System.Windows.Markup.XamlReader.Load(XmlReader reader)
       bei Skin_Test.Window1.MI_Load_Click(Object sender, RoutedEventArgs e) in C:\Dokumente und Einstellungen\christian.hauer\Eigene Dateien\Visual Studio 2008\Projects\Skin_Test\Skin_Test\Window1.xaml.cs:Zeile 163.


    I only have it in german, sorry about that.

    My Code:

    try 
        StringReader stringReader = new StringReader("testload.xaml"); 
        XmlReader xmlReader = new XmlTextReader(stringReader); 
        Page page = (Page)System.Windows.Markup.XamlReader.Load(xmlReader); 
    catch (Exception ex) 
        MessageBox.Show(ex.ToString(), "Error"); 

    My XAML-File:

    <Grid 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:jas="clr-namespace:WPF.JoshSmith.Panels"
        <jas:dragCanvas x:Name="dragCanvas"
            <Button Name="testButton" Width="100" Height="30" Canvas.Left="10" Canvas.Top="20" VerticalAlignment="Top" HorizontalAlignment="Left">Button</Button> 
        </jas:dragCanvas> 
    </Grid> 
     

  • 2009년 1월 15일 목요일 오후 3:04Staratnight 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
    I tried this, but I always get an exception:

    I think you got a problem casting from <grid> (your root [Stammebene]) to page.

    Try this:
    Grid grid = (Grid)System.Windows.Markup.XamlReader.Load(xmlReader); 

    • 편집됨Staratnight 2009년 1월 15일 목요일 오후 3:37Format
    • 편집됨Staratnight 2009년 1월 15일 목요일 오후 3:38Format...again
    •  
  • 2009년 6월 4일 목요일 오전 12:30GA30 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    Ashish,

    Would you please be able to provide a little more detail on how to accomplish 2nd approach: "use a custom page with component, and hook the event handlers via component code"?????

    Thank you