问题描述
我举了一个简单的例子来说明我的问题。有一个窗口,其DataContext对应于MainWindowviewmodel类。该窗口包含带有按钮和ProgressBar的网格。 Button的命令受viewmodel的RelayCommand的支持,该命令首先执行从0到100的整数属性“ Percent”的增量。progressBar显示进度很好!该窗口包含TaskBarItemInfo属性,后者具有两个属性:MainValue在MainWindowviewmodel中,而ProgressValue则为double属性“ Count”,而Progressstate则为TaskbarItemProgressstate属性“ State”。 TaskbarItemInfo也显示从0到1的增量。
但是在初始方法的第二部分中,进度不确定5秒钟。虽然在progressBar中显示得很好,但在TaskBar中却没有。我既使用了从布尔值到TaskbarItemProgressstate的Converter到Progressstate属性的路径(IsIndeterminate),又使用了绑定到单独的属性State的方法,如下面的代码所示。
MainWindow.xaml
<Window x:Class="Progressstate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Progressstate"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Window.DataContext>
<local:MainWindowviewmodel/>
</Window.DataContext>
<Window.Resources>
<local:BoolToProgressstateConverter x:Key="boolToProgressstateConverter"/>
</Window.Resources>
<Window.TaskbarItemInfo>
<TaskbarItemInfo ProgressValue="{Binding Count,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
Progressstate="{Binding State,Mode=OneWay}"/>
<!--Progressstate="{Binding IsIndeterminate,Mode=OneWay,Converter={StaticResource boolToProgressstateConverter}}"-->
</Window.TaskbarItemInfo>
<Grid>
<Grid.RowDeFinitions>
<RowDeFinition/>
<RowDeFinition/>
</Grid.RowDeFinitions>
<Button Content="Start progress" Grid.Row="0" Height="30" Width="100"
Command="{Binding Start}" IsEnabled="{Binding IsEnabled,UpdateSourceTrigger=PropertyChanged}"/>
<ProgressBar Grid.Row="1" Height="20" Margin="10 0"
Value="{Binding Percent,Mode=OneWay}"
IsIndeterminate="{Binding IsIndeterminate,Mode=OneWay}"/>
</Grid>
</Window>
MainWindowviewmodel.cs
using galaSoft.MvvmLight;
using galaSoft.MvvmLight.CommandWpf;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Shell;
namespace Progressstate
{
public class MainWindowviewmodel : viewmodelBase
{
private double count;
private bool isIndeterminate;
private int percent;
private bool isEnabled = true;
private TaskbarItemProgressstate state;
public RelayCommand Start { get; set; }
public MainWindowviewmodel()
{
Start = new RelayCommand(OnStart,CanStart);
}
public TaskbarItemProgressstate State
{
get
{
if (IsIndeterminate)
{
state = TaskbarItemProgressstate.Indeterminate;
}
else
{
state = TaskbarItemProgressstate.normal;
}
return state;
}
}
public bool IsEnabled
{
get
{
return isEnabled;
}
set
{
if (value != isEnabled)
{
isEnabled = value;
RaisePropertyChanged(nameof(IsEnabled));
}
}
}
public bool IsIndeterminate
{
get
{
return isIndeterminate;
}
set
{
if (value != isIndeterminate)
{
isIndeterminate = value;
RaisePropertyChanged(nameof(IsIndeterminate));
RaisePropertyChanged(nameof(State));
}
}
}
public int Percent
{
get
{
return percent;
}
set
{
if (value != percent)
{
percent = value;
RaisePropertyChanged(nameof(Percent));
RaisePropertyChanged(nameof(Count));
}
}
}
public double Count
{
get
{
count = Percent / 100d;
return count;
}
}
private bool CanStart()
{
return true;
}
private async void OnStart()
{
IsEnabled = false;
Percent = 0;
await Task.Run(() =>
{
while (Percent != 100)
{
Thread.Sleep(100);
Percent += 2;
}
IsIndeterminate = true;
Thread.Sleep(5000);
IsIndeterminate = false;
});
IsEnabled = true;
}
}
}
BoolToProgressstateConverter.cs
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Shell;
namespace Progressstate
{
class BoolToProgressstateConverter : IValueConverter
{
public object Convert(object value,Type targettype,object parameter,CultureInfo culture)
{
if (value is bool boolean)
{
if (boolean == true)
{
return TaskbarItemProgressstate.Indeterminate;
}
else if (boolean == false)
{
return TaskbarItemProgressstate.normal;
}
else
{
throw new NotImplementedException();
}
}
return TaskbarItemProgressstate.normal;
}
public object ConvertBack(object value,CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)