[英]receiving undeclared prefix when dynamically creating a xaml object
地獄般的,我正在嘗試在代碼中動態創建一些對象。 這已經成功到了我想要識別元素的地方,例如在這里我創建了一個對象(一個帶陰影的橢圓)
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:對象的名稱?
謝謝,
有人能指出我在這樣的場景中引用動態創建的對象的正確方向,或者如何以編程方式正確定義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;
}
不,不,不,不,不!
切勿使用字符串操作來創建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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.