MVVMLight:更新viewmodel时不会刷新Xaml

时间:2015-04-01 10:22:58

标签: windows-phone-8.1 winrt-xaml mvvm-light

我正在使用MVVMlight实现WP8.1项目。 在我的项目中,我有多个ViewModel(一个按功能)。

加载My IHM时,与ViewModel的绑定工作正常。

但是当我点击IHM按钮以刷新我的Viewmodel(我使用RelayCommand)时,我的IHM不会根据我的ViewModel刷新我的字段。

Xaml代码:

 <Page
x:Class="ApplicationMobileWinPhone.IHM.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ApplicationMobileWinPhone.IHM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
 DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
    <TextBox Foreground="Black" x:Name="TextAdress" Text="{Binding mytest.A}"  Margin="0,40,0,0" ></TextBox>
    <Button Command="{Binding mytest.SearchCommand,  Mode=OnWay}" Content="OK"
            CommandParameter="{Binding Text, ElementName=TextAdress}"
            BorderThickness="0" MinHeight="39" MinWidth="39" x:Name="ButtonSearch" VerticalAlignment="Bottom" Height="50" Width="32" Margin="358,-9,0,593">

    </Button>
</Grid>

ViewModels实施:

public class Mytest : ViewModelBase
{
    private RelayCommand<string> _searchCommand;
    private readonly INavigationService _navigationService;
    public string A
    {
        get;
        set;
    }
    public Mytest(INavigationService nav) 
    {
        _navigationService = nav;
        A = "init";
    }
    public RelayCommand<string> SearchCommand
    {
        get
        {
            return _searchCommand
                   ?? (_searchCommand = new RelayCommand<string>(
                        a =>
                       {
                           A = "modify";
                       }
             ));
        }
    }
}

我不明白,因为还有其他ViewModel,它运行正常。

1 个答案:

答案 0 :(得分:1)

您必须致电RaisePropertyChanged,您不必实施它。只需在为字段指定新值后添加以下行:

...
A = "modify";
RaisePropertyChanged(() => A);
...