0
主窗口:行動未運行
<Window x:Class="Generator.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:ignore="http://www.galasoft.ch/ignore"
mc:Ignorable="d ignore"
Height="309.851"
Width="673.433"
Title="Generator"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid x:Name="LayoutRoot">
<Frame x:Name="MainFrame" Content="" NavigationUIVisibility="Hidden" HorizontalAlignment="Left" Height="235" Margin="0,36,0,0" VerticalAlignment="Top" Width="294" Source="/Generator;component/Pages/TCPage.xaml"/>
</Grid>
</Window>
這是內框架的頁面
<Page x:Class="Generator.Pages.TCPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Generator.Pages"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding TCPageVM, Source={StaticResource Locator}}"
Title="TCPage">
<Grid>
<Button x:Name="button" Content="Jobby" HorizontalAlignment="Left" Margin="160,185,0,0" VerticalAlignment="Top" Width="75"
Command="{Binding ButtonCommandNew}" />
</Grid>
</Page>
這是在頁面的模型視圖的按鈕命令(TCPageVM):
private bool _inProgress = false;
private RelayCommand _buttonCommandNew;
public RelayCommand ButtonCommandNew
{
get
{
var worker = new TCService();
return _buttonCommandNew
?? (_buttonCommandNew = new RelayCommand(async() =>
{
var progress = new Progress<string>(status =>
{
ProgressText = status;
});
_inProgress = true;
_buttonCommandNew.RaiseCanExecuteChanged();
await worker.GenerateConfiguration(1, "", "", progress);
_inProgress = false;
_buttonCommandNew.RaiseCanExecuteChanged();
},() => !_inProgress
));
}
}
這是服務執行:
public Task<bool> GenerateConfiguration(int cellCount, string templateFilePath, string outputFilePath, IProgress<string> progress)
{
return Task.Run(() => RunConfigurator(progress));
}
private bool RunConfigurator(IProgress<string> progress)
{
Thread.Sleep(6000);
progress.Report("Yeah!");
return true;
}
問題是該按鈕已啓用,但操作未運行。
完美答案。只是最後一個問題 - 你認爲我啓用/禁用按鈕的方式是唯一的方法嗎?我的意思是那些'RaiseCanExecuteChanged'對我來說看起來很醜,我試圖將它移到服務邏輯。沒有調用'RaiseCanExecuteChanged'就可以離開嗎? – Pablo
每當條件改變時你都需要調用'RaiseCanExecuteChanged'。沒有得到解決。我會做的是讓'inProgress'屬性,並從setter調用'ButtonCommandNew.RaiseCanExecuteChanged();'。然後,你可以從任何地方改變'inProgress',UI將被更新。 – FUR10N