简体   繁体   English

使最基本的MetaDataType起作用

[英]Getting most basic MetaDataType to work

In addition to my original post I guess I need to mention that I am using Prism 6.3. 除了我的原始帖子,我想我还需要提到我正在使用Prism 6.3。 Apparently, the compiler doesn't like stuff added to the metadata class that's not in the original partial. 显然,编译器不喜欢添加到元数据类中不在原始部分中的内容。 Not sure how to resolve this. 不确定如何解决此问题。 Thanks again ... Ed 再次感谢... Ed

Ok, I give, UNCLE! 好吧,我给,UNCLE!

I am trying to add data annotations to my wpf entity framework app. 我正在尝试将数据注释添加到我的wpf实体框架应用程序。 I've tried 6 ways to Sunday with no luck. 我已经尝试了6种方法到周日没有运气。 I put together what is what I consider the most simple example possible and followed all the instructions ... nothing works. 我整理了我认为最简单的示例,然后按照所有说明进行操作……没有任何效果。 Here goes. 开始。 I have a class that is generated by EF (db first). 我有一个由EF生成的类(首先是db)。

namespace junk.DataModels
{
    public partial class MyClass
    {
        public string SomeText { get; set; }
    }
}

I have another file with the following partial class: 我有另一个具有以下部分类的文件:

namespace junk.DataModels
{
    [MetadataType(typeof(MyClassMetaData))]
    public partial class MyClass
    {
    }

    public partial class MyClassMetaData
    {
      private string _someText;
      [Required(ErrorMessage = "Required")]
      public string SomeText 
      {
          get { return _someText; }
          set { SetProperty(ref _someText, value); }
      }
    }
}

In my ViewModel I define: 在我的ViewModel中,我定义:

private MyClass _mc;
public MyClass MC
{
    get { return _mc; }
    set
    {
        SetProperty(ref _mc, value);
    }
}

And in the constructor: 并在构造函数中:

MC = new MC();
MC.SomeText = "Hello World.";

Lastly, in my xaml: 最后,在我的xaml中:

I have a single bound control: 我有一个绑定控件:

<TextBox x:Name="txt" Text="{Binding MC.SomeText, 
        ValidatesOnDataErrors=True, 
        ValidatesOnExceptions=True,
        ValidatesOnNotifyDataErrors=True,
        UpdateSourceTrigger=PropertyChanged }"
 />

According to everything I've read, if I run this and clear the textbox, I should get a validation error. 根据我阅读的所有内容,如果运行此程序并清除文本框,则应该收到验证错误。 I have tried all combinations of "ValidatesOn" it doesn't seem to make a difference. 我尝试了“ ValidatesOn”的所有组合,但似乎没有什么不同。 Can someone take pity on me and share the secret sauce? 有人可以同情我并分享秘密酱料吗? I must be missing something simple. 我一定想念一些简单的东西。 If I bind to the metadataclass it works but that is kinda defeating the purpose. 如果我绑定到元数据类,它将起作用,但这有点违反了目的。

Any help would be great! 任何帮助将是巨大的!

Try adding the following static constructor to your buddy class "MyClass". 尝试将以下静态构造函数添加到好友类“ MyClass”。 It will register the metadata against your EF class so the Validator can find the Data Annotations: 它将针对您的EF类注册元数据,以便验证器可以找到数据注释:

static MyClass()
{
    // Register the metadata against our EF data object. 
    // This will ensure the Validator find the annotations
    TypeDescriptor.AddProviderTransparent(
        new AssociatedMetadataTypeTypeDescriptionProvider(
            typeof(MyClass), 
            typeof(MyClassMetaData)), 
            typeof(MyClass)
    );
}

You could also try running a unit test to confirm whether the Validator has used your annotation, before adding the complexity of the GUI: 您还可以尝试运行单元测试,以确认Validator是否使用了注释,然后再添加GUI的复杂性:

[TestMethod]
public void TestAnnotations()
{
    MyClass c = new MyClass();

    // Manually validate the MyClass object
    List<ValidationResult> validationResults = new List<ValidationResult>();
    ValidationContext context = new ValidationContext(c, serviceProvider: null, items: null);
    bool isValid = Validator.TryValidateObject(c, context, validationResults, validateAllProperties: true);

    Assert.IsFalse(isValid, "Validation should fail because we didn't set SomeText");

}

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

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