感谢造访。
我试图在UWP应用程序中使用模板10验证来验证绑定到整数类型属性的文本框,但是当数据是字符时,set访问器不会触发,只有当它是整数时才触发。当我使用字符串类型属性时,一切正常。我正在使用MVVM灯。
这是我的模型:
public class User : ValidatableModelBase
{
public User()
{
}
public string FirstName
{
get
{
return Read<string>();
}
set
{
Write(value);
}
}
public string LastName
{
get
{
return Read<string>();
}
set
{
Write(value);
}
}
public int ID
{
get
{
return Read<int>();
}
set
{
Write(value);
}
}
public override string ToString() => $"{FirstName} {LastName}";
}
这是我的类,它返回一些示例数据并指定验证逻辑:
public static class UserService
{
public static User GetUser()
{
var user = new User
{
Validator = i =>
{
var u = i as User;
if (string.IsNullOrEmpty(u.FirstName))
{
u.Properties[nameof(u.FirstName)].Errors.Add("First name is required");
}
if (string.IsNullOrEmpty(u.LastName))
{
u.Properties[nameof(u.LastName)].Errors.Add("Last name is required");
}
int x;
if (!int.TryParse(u.ID.ToString(), out x))
{
u.Properties[nameof(u.ID)].Errors.Add("Integers only allowed");
}
}
};
return user;
}
}
这是我的ViewModel:
public class BlankViewModel : ViewModelBase
{
public RelayCommand ValidateCommand { get; private set; }
public BlankViewModel()
{
ValidateCommand = new RelayCommand(ValidateCommandAction);
}
private void ValidateCommandAction()
{
//Do something
}
private async Task<User> GetUser()
{
return await Task.Run(() => UserService.GetUser()).ConfigureAwait(false);
}
User _selected;
public Models.User Selected
{
get
{
_selected = GetUser().Result;
return _selected;
}
set
{
try
{
Set(ref _selected, value);
}
catch (Exception)
{
}
}
}
}
这是观点:
<Grid
Grid.Row="1"
Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
<!--The SystemControlPageBackgroundChromeLowBrush background represents where you should place your content.
Place your content here.-->
<StackPanel DataContext="{Binding Selected}">
<validate:ControlWrapper PropertyName="FirstName">
<TextBox Text="{Binding FirstName, Mode=TwoWay}" Header="First Name"/>
</validate:ControlWrapper>
<validate:ControlWrapper PropertyName="LastName">
<TextBox Text="{Binding LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Last Name"/>
</validate:ControlWrapper>
<validate:ControlWrapper PropertyName="ID">
<TextBox Text="{Binding ID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="ID"/>
</validate:ControlWrapper>
<Button Content="Validate" IsEnabled="{Binding IsValid}"/>
</StackPanel>
</Grid>
对于firstname和lastname属性,一切正常,但是当属性设置为字符时,ID属性验证逻辑和set访问器不会触发,只有当它是整数时才会触发,即使它是双精度也不会触发。
我真的很感激一些帮助。非常感谢提前。
它应该是ValidatableModelBase
中的Template10.Validation
类的问题。
作为Introducing Template10 Validation,
如果您有反馈或要报告的错误,请在此处执行:https://github.com/Windows-XAML/Template10/issues。
您可以在上面的链接中报告此问题。
目前,您可以将int ID属性更改为字符串类型,然后将GetUser
静态方法更改为以下代码,它将很好地满足您的要求。
public static User GetUser()
{
var user = new User
{
Validator = i =>
{
var u = i as User;
if (string.IsNullOrEmpty(u.FirstName))
{
u.Properties[nameof(u.FirstName)].Errors.Add("First name is required");
}
if (string.IsNullOrEmpty(u.LastName))
{
u.Properties[nameof(u.LastName)].Errors.Add("Last name is required");
}
if (string.IsNullOrEmpty(u.ID))
{
u.Properties[nameof(u.ID)].Errors.Add("Integers is required");
}
else
{
int x;
bool success = int.TryParse(u.ID.ToString(), out x);
if (!success)
{
u.Properties[nameof(u.ID)].Errors.Add("Integers only allowed");
}
}
}
};
return user;
}