加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 动态创建xaml对象时接收未声明的前缀

发布时间:2020-12-16 01:41:25 所属栏目:百科 来源:网络整理
导读:地狱般的,我正在尝试在代码中动态创建一些对象.这已经成功,直到我想要识别元素的地方为例 在这里我创建了一个对象(一个带阴影的椭圆) public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Load
地狱般的,我正在尝试在代码中动态创建一些对象.这已经成功,直到我想要识别元素的地方为例
在这里我创建了一个对象(一个带阴影的椭圆)

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

我想要做的是创建对象后我希望能够使用VisualTreeHelper找到父对象.

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

有人能指出我在这样的场景中引用动态创建的对象的正确方向,或者如何以编程方式正确定义x:对象的名称?

谢谢,

解决方法

Can anyone point me in the right direction for referencing a dynamically created object in a scenario like this or how to properly define x:Name for an object programatically?

您不能使用动态生成的类型直接引用“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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读