【问题标题】:MVVMLight: Xaml don't refresh when the viewmodel is updatedMVVMLight:更新视图模型时 Xaml 不刷新
【发布时间】:2015-04-01 10:22:58
【问题描述】:

我正在使用 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 并且工作正常。

【问题讨论】:

  • 您的属性没有实现INotifyPropertyChanged
  • 谢谢迈克!它工作正常!拜托,你能解释一下为什么在其他视图模型中,我没有实现 INotifyPropertyChanged 并且它工作正常吗?
  • 绑定会起作用,但只有一次。如果您随后更改该属性,则除非您通知该视图,否则该视图将不会更新。因此,INotifyPropertyChanged.

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 2021-06-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2013-08-26
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多