【发布时间】:2017-01-17 09:05:38
【问题描述】:
在我的应用程序中,我有一个文本框,用户可以在上面输入数字。我想在用户键入时将此拉丁数字转换为波斯数字。在调用RaisePropertychanged 之后,MobileNumber 的getter 没有被调用,所以App Ui 不会更新。我的代码有什么问题?
这是我的代码
View.xaml
<Page
x:Class="CustomName.RegistrationPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ShahrMobileBank.Views.Masters.Registration"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:behaviors="using:Template10.Behaviors"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:converter="using:ShahrMobileBank.Converter"
mc:Ignorable="d"
DataContext="{Binding Path=RegistrationPage, Source={StaticResource ViewModelLocator}}">
<StackPanel Background="{StaticResource RegistrationPageBackgroundColor}" x:Name="LayoutRoot">
<StackPanel.Resources>
<converter:RegistrationConverter x:Key="RegistrationConverter"/>
</StackPanel.Resources>
<TextBox Style="{StaticResource GenericTextBoxBeforeLogin}" x:Uid="PhoneNumber" InputScope="Number" Text="{Binding Path=MobileNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MaxLength="{StaticResource MobileNumberMaxLength}"/>
</StackPanel>
</Page>
ViewModel.cs
public class RegistrationPageViewModel : ViewModelBase
{
private string _mobileNumber;
public String MobileNumber
{
get
{
return _mobileNumber;
}
set
{
_mobileNumber = LangUtil.ConvertEnNumberToFaNumber(value); // this converts the number from Latin to Persian
RaisePropertyChanged(() => MobileNumber);
}
}
}
【问题讨论】:
-
为什么不使用:
Set( ref _mobileNumber , LangUtil.ConvertEnNumberToFaNumber(value))? -
在用户离开文本框之前,该属性不会更新。您是否希望属性值随着用户键入而改变?
标签: c# mvvm windows-phone-8.1 windows-phone mvvm-light