【问题标题】:mvvm light calling RaisePropertyChanged doesn't fire Property getter(windows phone 8.1)mvvm light 调用 RaisePropertyChanged 不会触发 Property getter(windows phone 8.1)
【发布时间】:2017-01-17 09:05:38
【问题描述】:

在我的应用程序中,我有一个文本框,用户可以在上面输入数字。我想在用户键入时将此拉丁数字转换为波斯数字。在调用RaisePropertychanged 之后,MobileNumbergetter 没有被调用,所以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


【解决方案1】:

尝试将RaisePropertyChanged 行更改为RaisePropertyChanged("MobileNumber");

如果你使用MVVM-Light Code sn-ps(如果安装了,可以开始输入mvvm,会弹出intellisense),可以看mvvmpinpc这是PropertyChanged代码sn-ps之一.您可以通过不同的模板字段Tab 将其设置为您想要的,并可能阐明一些小提示和技巧,以使您的编码生活更轻松。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2016-05-10
    • 1970-01-01
    相关资源
    最近更新 更多