【问题标题】:ProgressRing IsActive property is not updatingProgressRing IsActive 属性未更新
【发布时间】:2018-02-22 16:41:25
【问题描述】:

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

我已努力将其中一个隔离到可用的迷你示例项目here

ProgressRing 控件的 IsActive 属性不会对来自 ViewModel 端的更新做出反应,即使它有:

  • INotifyPropertyChanged 已实现
  • 绑定模式显式设置为双向绑定
  • 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>

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);
        }
    }
}

UWP 控件是否不能很好地与常规绑定而不是 x:Bind 一起使用?还有更多类似的问题,例如 CalendarPickerView 日期属性也不想配合。

【问题讨论】:

    标签: c# xaml mvvm uwp mvvm-light


    【解决方案1】:

    首先,您的绑定中不需要UpdateSourceTrigger=PropertyChanged, Mode=TwoWay。它总是 sourcetarget(即OneWay)。

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

    因此,删除此行将使您的代码正常工作。您甚至可以将其简化为

    private bool _isLoading;
    public bool IsLoading
    {
        get => _isLoading;
        set => Set(ref _isLoading, value);
    }
    

    【讨论】:

    • 这就是问题所在。谢谢你:)
    【解决方案2】:

    尝试将您的绑定更改为:

    IsActive="{Binding IsLoading}"
    

    以及您的房产代码:

        private bool _isLoading;
        public bool IsLoading
        {
            get {return _isLoading;}
            set
            {
                _isLoading = value;
                NotifyOfPropertyChange(nameof(IsLoading));
            }
        }
    

    我使用了来自Caliburn.Micro 框架的INotifyPropertyChanged 的实现,但它可以是任何其他有效的实现;)

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2019-04-18
      • 2012-07-18
      相关资源
      最近更新 更多