Validation for UWP textbox

微笑、不失礼 提交于 2019-12-24 18:23:52

问题


I am currently developing a UWP application, I am very new to this platform and need some help.

Thanks for the help!


回答1:


So I was able to write up a demo project.

You can find it on Github Here!

I'll quickly tell you about the sample, There are two solutions:

  1. Quick and Dirty: This took the most time for me and I had to write up a lot of code for something very trivial. I don't recommend this way but this way is non complicated and straight forward.
  2. Dynamic Content way (Recommended): Here, I've kept things extensible might be a little bit complex but it's more easy to manage and works much better than the first way.

Since you're using MVVM, I've followed the same.

Please Note: I haven't exactly replicated your model as I didn't know what to put in the UI and what all are the validations, but from the sample you'll be able to figure it out.

I hope this helps, in-case of any queries, feel free to use the comments section.

In the recommended solution your key area to control is just a single collection:

internal ObservableCollection<ComponentModel.IFormControl> FormFields => new ObservableCollection<ComponentModel.IFormControl>(new List<ComponentModel.IFormControl>()
    {
        new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Name",PlaceholderText="e.g. John Doe",IsMandatory = true },
        new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Admin No" , PlaceholderText = "e.g. ABC123"},
        new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Phone" , PlaceholderText = "e.g. +32538349182" ,IsMandatory = true,MatchingPattern = @"^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$"},
        new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Item Description", PlaceholderText = "e.g. My Fav Item",IsMandatory = true },
        new ViewModel.TextFieldInputControlViewModel(){HeaderName = "Location Lost", PlaceholderText = "e.g. Alaska",IsMandatory = true },
        new ViewModel.DateTimeFieldInputViewModel(){ HeaderName = "Date Lost",IsMandatory = true}
    });

and you can add more types from inheriting from the interface IFormControl and just add in fields here. exactly as simple as it sounds when it comes to adding more fields.



来源:https://stackoverflow.com/questions/46538144/validation-for-uwp-textbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!