1

just got some Problems with Loading Xaml Files by Runtime. For your Information my Code-Snippet to Load the File as Content of a Usercontrol:

        public UserControl LoadXaml(FileInfo paramFile)
    {
        FileInfo _XamlFile = paramFile;
        UIElement rootElement;
        FileStream s = new FileStream(_XamlFile.FullName, FileMode.Open);
        rootElement = (UIElement)XamlReader.Load(s);
        s.Close();
        UserControl uc = new UserControl();

        if (rootElement.GetType() == typeof(Window))
        {
            uc.Content = (rootElement as Window).Content;
        }
        else
        {
            uc = rootElement as UserControl;
        }


        return uc;
    }

    private void lstPDFDokumente_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var _XamlFile = ((System.Windows.Controls.ListBox)sender).SelectedItem as FileInfo;



        if (_XamlFile != null)            
        {
            layoutGrid.Children.Clear();
            System.Windows.Controls.UserControl rootElement;
            rootElement = XamlController.LoadXaml(_XamlFile);
            layoutGrid.Children.Add(rootElement);
        }
    }

This works fine while Events and x:Class="..." are deleted by hand.

The Problems I try to solve are:

  1. If there is a x:Class="..." at the root element the XamlReader throws the first exception.
  2. When the XamlReader reaches a Control which contains an event, for Example Click or TextChanged, it throws another Exception.

What i try to figure out is how to load a XamlFile, show it inside of a Control in the main Window and to show some of the attributes like Name,Height,Width and so on. Just read dozens of Websites but never found a topic about to make a preview or things like that.

One of the solutions i tried is to read the Xaml File as XML and delete that code. The Problem was to get a list of all possible Events in C#.

If there are some Questions to that Code, feel free to ask :)

Greetings Daniel

Daniel
  • 95
  • 2
  • 9

0 Answers0