简体   繁体   English

WPF xamlreader使用自定义元素加载xaml

[英]Wpf xamlreader load xaml with custom elements

I got a canvas with custom elements, similar to texboxes and shapes. 我得到了一个包含自定义元素的画布,类似于texbox和形状。

<Canvas Name="SomeCanvas" >
    <TextBox_Element   Canvas.Left="400" Canvas.Top="200" Height="50" Name="s3" Background="#57FF3ACB"  />
</Canvas>

Which im saving to a file with: 我即时保存到一个文件:

SerializeToXML(filename, SomeCanvas);

Later I´m trying to load that file and convert ist back to the original canvas with: 稍后,我尝试加载该文件,并使用以下命令将ist转换回原始画布:

FileStream fs = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
Canvas savedCanvas = XamlReader.Load(fs) as Canvas;

The last method throws a XamlParseException: 最后一个方法抛出XamlParseException:

No matching constructor found on type '...TextBox_Element'. 在类型'... TextBox_Element'上找不到匹配的构造函数。 You can use the Arguments or FactoryMethod directives to construct this type.' 您可以使用Arguments或FactoryMethod指令来构造此类型。

Does anybody know how to restore the canvas the way it was from the file? 有人知道如何从文件中还原画布吗? Thanks 谢谢

You have to define a parameterless constructor in your TextBox_Element . 您必须在TextBox_Element定义一个无参数的构造TextBox_Element

XamlReader.Load by default will always look for an empty constructor while if it has a required parameters you'll have to add more work. 默认情况下, XamlReader.Load始终查找空的构造函数,而如果它具有必需的参数,则必须添加更多工作。

According to Max Galkin 根据Max Galkin

It's a "feature" of the XAML language, it is declarative and doesn't know anything about constructors. 它是XAML语言的“功能”,是声明性的,对构造函数一无所知。 People use ObjectDataProvider in XAML to "translate" and wrap instances of classes that do not have a parameterless constructor (it's [also useful for data binding][2]). 人们使用XAML中的ObjectDataProvider来“翻译”并包装没有无参数构造函数的类的实例([对数据绑定也有用[2])。

In your case it'll be approximately like this: 在您的情况下,大致是这样的:

<ObjectDataProvider ObjectType="TextBox_Element">
    <ObjectDataProvider.ConstructorParameters>
        <x:Double>400</x:Double>
        <x:Double>200</x:Double>
    </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>


var textBoxElem = (TextBox_Element) ((ObjectDataProvider)XamlReader.Parse(xamlStr)).Data;

Assuming your TextBox_Element constructor looks like this 假设您的TextBox_Element构造函数如下所示

public class TextBox_Element
{
  public TextBox_Element(double left, double top)
  {
    // do whatever
  }
}

Do you have a parameterless constructor in your custom class? 您的自定义类中是否有无参数的构造函数? If not, try this: 如果没有,请尝试以下操作:

public class TextBox_Element{

    public TextBox_Element(double x, double y){
        // code
    }
    public TextBox_Element(){
        // emptyness
    }
}

That worked for me, i hope itsolves your problem too! 这对我有用,我希望它也能解决您的问题!

Perhaps the file it saved to did not contain the information required on your custom element? 也许保存到的文件不包含自定义元素上所需的信息? Just as if you save a textbox, the XamlReader needs to have the xmlns references to be able to read it. 就像保存文本框一样,XamlReader需要具有xmlns引用才能读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM