以编程方式在应用程序中插入XAML [英] Inserting XAML programmatically in application

查看:67
本文介绍了以编程方式在应用程序中插入XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可爱的小天文学程序,本来应该是为了自己的快乐而编码的一个星期;现在变成了噩梦!

我只是有一个XAML:

I have this cute little astronomy program that was supposed to be a week of coding for my own pleasure; which now have become a nightmare!

I simply have a piece of XAML:

<Ellipse Canvas.Left="15.38676905655"
                Canvas.Top="11.784944134632"
                Height="5px" Width="5px" Fill="Red"
                Panel.ZIndex="-800"
                Cursor="Hand">
        <Ellipse.RenderTransform>
            <TranslateTransform X="-2" Y="-2" />
        </Ellipse.RenderTransform>
        <Ellipse.ToolTip>
            <ToolTip>
                <StackPanel>
                    <TextBlock>Proxima Centauri</TextBlock>
                    <TextBlock>-1,538676906;-1,178494413;-3,752088504</TextBlock>
                </StackPanel>
            </ToolTip>
        </Ellipse.ToolTip>
    </Ellipse>


我想在运行时添加到画布"starfield"中.我正在从数据库中获取数据,(我无意输入45''000星来手动创建星图),但是遇到了一些问题.

添加:


That I want to add during runtime to the canvas "starfield". I''m getting the data from a database, (I have no intention of typing in 45''000 stars to create a starmap manually), I however have met some issues.

Adding:

<Ellipse Canvas.Left="15.38676905655" Canvas.Top="11.784944134632" Height="5px" Width="5px" Fill="Red" Panel.ZIndex="-800" Cursor="Hand"/>


没问题,我有3种不同的方式来做.但是问题在于嵌套在椭圆"内部的问题.

有人为此解决了吗?

谢谢!

-Frank


Is no problem, I got 3 different ways of doing that. But its the things nested inside the "Ellipse" that''s the problem.

anyone got a fix for this??

thank you!

-Frank

推荐答案

如何以编程方式而不是在xaml文件中添加所有Ellipse?然后,您只需从数据库中填写详细信息即可.

What about adding all of the Ellipse programatically instead of in a xaml file? Then you can just fill in the details from the database.

var tool = new StackPanel();
tool.Children.Add(new TextBlock() { Text = "Proxima Centauri" });
tool.Children.Add(new TextBlock() { Text = "-1,538676906;-1,178494413;-3,752088504" });

var star = new Ellipse
{
    Height = 5,
    Width = 5,
    Fill = Brushes.Red,
    RenderTransform = new TranslateTransform(-2, -2),
    ToolTip = tool,
    Cursor = Cursors.Hand
};
star.SetValue(Canvas.LeftProperty, 15.38676905655);
star.SetValue(Canvas.TopProperty, 11.784944134632);
star.SetValue(Canvas.ZIndexProperty, -800);

parentCanvas.Children.Add(star);


您也可以在XAML中执行操作,在某些情况下这样做会更好.也许在这种情况下可能不会,但是有些事情在编程上无法正常工作:

You can also do things in XAML, and there are cases where this is better. Maybe not in this case, but some things just do not work well programatically:

string Xaml= @"<SolidColorBrush xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Color="AliceBlue"/>;
          StringReader stringReader = new StringReader(Xaml);
          System.Xml.XmlReader xmlReader = new System.Xml.XmlTextReader(stringReader);
          Brush rslt = XamlReader.Load(xmlReader) as Brush;


这篇关于以编程方式在应用程序中插入XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆