2017-09-13 63 views
0

我正在編寫一個小型的UWP項目,也使用MVVM模式(MVVM輕型框架)進行我的研究,並遇到了許多場景和控件中的綁定問題。ProgressRing IsActive屬性未更新

我已經努力將其中的一個隔離到可用的迷你示例項目here

ProgressRing控制的IsActive屬性未反應在甚至通過它從視圖模型側更新具有:

  • 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行之有效:綁定?還有更多的問題,例如CalendarPickerView Date屬性也不想合作。

回答

6

首先,您不需要綁定中的UpdateSourceTrigger=PropertyChanged, Mode=TwoWay。它始終是來源目標(即OneWay)。

真正的問題是,您不應該設置_isLoading = value;,因爲它是作爲ref傳入的,用於檢查屬性值是否已更改。

因此,刪除此行將使您的代碼正常工作。你甚至可以把它簡化爲

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

這是iss UE。謝謝 :) –

-1

試着改變你的綁定:

IsActive="{Binding IsLoading}" 

而且你的財產代碼:

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

我已經使用實施INotifyPropertyChangedCaliburn.Micro框架,但它可以是任何其他的實現它的工作;)