Getting an undeclared prefix when dynamically creating an xaml object

Gosh, I'm currently trying to create multiple objects dynamically in code. This was successful until the moment when I want to identify the element, for example, Here I create an object (ellipse with shadow)

public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var element = CreateEllipse();
        LayoutRoot.Children.Add(element);
    }

    public Ellipse CreateEllipse()
    {
        StringBuilder xaml = new StringBuilder();
        string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        xaml.Append("<Ellipse ");
        xaml.Append(string.Format("xmlns='{0}'", ns));
        xaml.Append(" x:Name='myellipse'"); //causes the exception
        xaml.Append(" Margin='50 10 50 10'");
        xaml.Append(" Grid.Row='0'");
        xaml.Append(" Fill='#FD2424FF'");
        xaml.Append(" Stroke='Black' >");
        xaml.Append("<Ellipse.Effect>");
        xaml.Append("<DropShadowEffect/>");
        xaml.Append(" </Ellipse.Effect>");
        xaml.Append(" </Ellipse>");
        var ellipse = (Ellipse)XamlReader.Load(xaml.ToString());
        return ellipse;
    }

What I want to do is after creating the object. I want to be able to find parent objects using VisualTreeHelper.

public void button1_Click(object sender, RoutedEventArgs e)
    {
        DependencyObject o = myellipse;
        while ((o = VisualTreeHelper.GetParent(o)) != null)
        {
            textBox1.Text = (o.GetType().ToString());
        }
    }

Can someone point me in the right direction to refer to a dynamically created object in such a scenario or how to correctly determine x: Name for the object programmatically?

Thank,

+3
source share
2 answers

- x: ?

"myellipse" , . x:Name - , .

"myellipse" :

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var element = CreateEllipse();
    LayoutRoot.Children.Add(element);
}

private Ellipse myEllipse; 
public Ellipse CreateEllipse()
{
    StringBuilder xaml = new StringBuilder();
    string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    xaml.Append("<Ellipse ");
    xaml.Append(string.Format("xmlns='{0}'", ns));
    xaml.Append(" Margin='50 10 50 10'");
    xaml.Append(" Grid.Row='0'");
    xaml.Append(" Fill='#FD2424FF'");
    xaml.Append(" Stroke='Black' >");
    xaml.Append("<Ellipse.Effect>");
    xaml.Append("<DropShadowEffect/>");
    xaml.Append(" </Ellipse.Effect>");
    xaml.Append(" </Ellipse>");
    this.myEllipse = (Ellipse)XamlReader.Load(xaml.ToString());

    return this.myEllipse;
}

, , . . XamlReader .

private Ellipse myEllipse; 
public Ellipse CreateEllipse()
{
     this.myEllipse = new Ellipse();
     this.myEllipse.Effect = new DropShadowEffect();
     Grid.SetRow(this.myEllipse, 0);
     // Set properties as needed

     return this.myEllipse;
}
+5

, , , , !

XML. .

XML API XML. XML-API , , , .


, :

XNamespace ns =
    "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xns =
    "http://schemas.microsoft.com/winfx/2006/xaml";
var ellipse = new XElement(
    ns + "Ellipse",
    new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
    new XAttribute(xns + "Name","myellipse"),
    new XAttribute("Margin","50 10 50 10"),
    new XAttribute("Grid.Row", "0"),
    new XElement(ns +"Ellipse.Effect",
        new XElement(ns +"DropShadowEffect")));
return
    (Ellipse)
    XamlReader.Load(ellipse.CreateReader());
+2

Source: https://habr.com/ru/post/1793667/


All Articles