ProgressRing IsActive属性未更新

Kam*_*iak 0 c# xaml mvvm mvvm-light uwp

我正在编写一个小型UWP项目,也使用MVVM模式(MVVM Light框架)进行研究,并在许多场景和控件中遇到了绑定问题.

我已经努力将其中一个分离成这里提供的迷你示例项目.

ProgressRing控件的IsActive属性对ViewModel方面的更新没有做出反应,即使它具有:

  • INotifyPropertyChanged已实现
  • 绑定模式明确设置为TwoWay绑定
  • UpdateSourceTrigger显式设置为PropertyChanged

MainPage.xaml中

<Page
    x:Class="TestProgressRing.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:local="using:TestProgressRing"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="491.282"
    Height="492.308"
    DataContext="{Binding MainPageViewModel, Source={StaticResource ResourceKey=Locator}}"
    mc:Ignorable="d">
    <Grid
        Width="500"
        Height="800"
        Margin="0,0,-8.667,-307.667"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="3*" />
        </Grid.RowDefinitions>
        <Button
            Grid.Row="0"
            Width="250"
            Height="50"
            Margin="0"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Content="Boom">
            <i:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction Command="{Binding ClickCommand}" />
                </core:EventTriggerBehavior>
            </i:Interaction.Behaviors>
        </Button>
        <ProgressRing
            Grid.Row="1"
            Width="500"
            Height="500"
            Margin="0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            IsActive="{Binding IsLoading, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
            Visibility="Visible" />
    </Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

MainPageViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace TestProgressRing
{
    public class MainPageViewModel : ViewModelBase
    {
        private bool _isLoading;
        public bool IsLoading
        {
            get => _isLoading;
            set
            {
                _isLoading = value;
                Set(() => IsLoading, ref _isLoading, value);
            }
        }

        public ICommand ClickCommand { get; set; }

        public MainPageViewModel()
        {
            ClickCommand = new RelayCommand(() => 
            IsLoading = !IsLoading);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

UWP控件是否仅仅适用于常规Binding而不是x:Bind?还有更多问题,例如CalendarPickerView Date属性也不想合作.

Jus*_* XL 7

首先,您不需要UpdateSourceTrigger=PropertyChanged, Mode=TwoWay绑定.它总是将是目标(即OneWay).

真正的问题是你不应该设置,_isLoading = value;因为它是作为ref检查属性值是否已更改而传入的.

因此,删除此行将使您的代码正常工作.你甚至可以简化它

private bool _isLoading;
public bool IsLoading
{
    get => _isLoading;
    set => Set(ref _isLoading, value);
}
Run Code Online (Sandbox Code Playgroud)