Form validation

I need to develop a windows phone 7 application. And for obvious reasons, I have to check my forms.

I usually program in WPF and use the ValidationRule principle. But I can not find the same principle in a Windows 7 phone.

Hence my question is how to create form validation.

+6
source share
2 answers

Windows Phone does not support formal checks out of the box.

Here 's a blog post that describes how to collapse a user control to implement validation rules.

The way I could handle this in one of my own applications would be to put the validation logic in my model class and create the IsValid property on the model. The model class also has the Error property with an error message describing the validation problem. My user interface layer will call myModel.IsValid and display an error message if something is wrong.

+4
source

I copied the same approach as with Silverlight on desktops: INotifyDataErrorInfo .

Here I described it more specifically, and here you can download the source code for a sample project.

The simplest example looks like this:

View.xaml

 <TextBox Text="{Binding SomeProperty, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" Style="{StaticResource ValidationTextBoxStyle}" /> 

View.xaml.cs

 public MainPage() { InitializeComponent(); this.BindingValidationError += MainPage_BindingValidationError; } private void MainPage_BindingValidationError(object sender, ValidationErrorEventArgs e) { var state = e.Action == ValidationErrorEventAction.Added ? "Invalid" : "Valid"; VisualStateManager.GoToState((Control)e.OriginalSource, state, false); } 

ViewModel.cs

 public class MainViewModel : ValidationViewModel { public MainViewModel() { this.Validator.AddValidationFor(() => this.SomeProperty).NotEmpty().Show("Enter a value"); } private string someProperty; public string SomeProperty { get { return someProperty; } set { someProperty = value; RaisePropertyChanged("SomeProperty"); } } } 

It relies on many additional classes, but at the same time there is little code that you write yourself.

0
source

Source: https://habr.com/ru/post/910245/


All Articles