2014-06-20 46 views
-1

我想开发一个繁忙的窗口,如herehere。我希望网格在我需要可见时随时可见,例如,当我做了一个长时间的任务时。为什么我的繁忙网页无法正常工作?

我做现在这个TIL:

的XAML:

<Window x:Class="LoadingWindow2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:loadingWindow2="clr-namespace:LoadingWindow2" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <loadingWindow2:BoolToVisibilityConverter x:Key="boolConverter"/> 
</Window.Resources> 
<Grid> 
    <Label HorizontalAlignment="Right"> 
     <Hyperlink Command="{Binding Path=DoSomething}">Do Something</Hyperlink> 
    </Label> 
    <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Visibility="{Binding IsBusy, Converter={StaticResource boolConverter}}" Grid.RowSpan="3"> 
     <Grid> 
      <TextBlock Margin="0" TextWrapping="Wrap" Text="Please Wait..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#7EFFFFFF"/> 
     </Grid> 
    </Border> 
</Grid> 

我的.cs:

的BusyViewModel.cs:

public class BusyViewModel : INotifyPropertyChanged 
{ 
    private ICommand doSomethingCommand; 

    public event PropertyChangedEventHandler PropertyChanged; 

    public bool IsBusy { get; set; } 

    public ICommand DoSomething 
    { 
     get { return doSomethingCommand ?? (doSomethingCommand = new DelegateCommand(LongRunningTask)); } 
    } 

    private void LongRunningTask() 
    { 
     var task = new Task(ComputeResults); 
     task.Start(); 
    } 

    private void ComputeResults() 
    { 
     this.IsBusy = true; 
     Thread.Sleep(5000); 
     this.IsBusy = false; 
    } 
} 

的DelegateCommand.cs:

public class DelegateCommand : ICommand 
{ 
    private readonly Action executeMethod; 
    private readonly Func<bool> canExecuteMethod; 

    public DelegateCommand(Action executeMethod) 
     : this(executeMethod,() => true) 
    { 
    } 

    public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod) 
    { 
     if (executeMethod == null) 
      throw new ArgumentNullException("executeMethod"); 
     if (canExecuteMethod == null) 
      throw new ArgumentNullException("canExecuteMethod"); 

     this.executeMethod = executeMethod; 
     this.canExecuteMethod = canExecuteMethod; 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public bool CanExecute(object stupid) 
    { 
     return CanExecute(); 
    } 

    public bool CanExecute() 
    { 
     return canExecuteMethod(); 
    } 

    public void Execute(object parameter) 
    { 
     Execute(); 
    } 

    public void Execute() 
    { 
     executeMethod(); 
    } 

    public void OnCanExecuteChanged() 
    { 
     CommandManager.InvalidateRequerySuggested(); 
    } 
} 

MainWindow.xaml.cs:

public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new BusyViewModel(); 
    } 

我下载源代码从我复制的第一链路,而busy Grid正在显示。但在我的情况下...不是!我在这里做错了什么?

编辑:我删除了Converter建议。但它没有工作......我加我`MainWindow.xaml.cs”

整来源:here

+0

为什么不只是使用您下载的代码并正在工作? – Paparazzi

+0

我必须在一个大项目中插入这段代码。如果它不适用于一个小项目...为什么要在大工作?还有......一个大问题:如果它一样,为什么不工作? – Sonhja

+0

为什么不使用正在工作的代码? – Paparazzi

回答

2

有可从WPF已经是一个转换器,将‘没有工作booleanToVisibilityConverter’

http://msdn.microsoft.com/de-de/library/system.windows.controls.booleantovisibilityconverter%28v=vs.110%29.aspx

编辑您的XAML这样的:

<Window x:Class="LoadingWindow2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:loadingWindow2="clr-namespace:LoadingWindow2" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources>  
     <BooleanToVisibilityConverter x:Key="boolConverter"/> 
    </Window.Resources> 
    <Grid x:Name="ROOT"> 
     <Label HorizontalAlignment="Right"> 
      <Hyperlink Command="{Binding Path=DoSomething}">Do Something</Hyperlink> 
    </Label> 
    <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Visibility="{Binding Path=IsBusy, Converter={StaticResource boolConverter}}" Grid.RowSpan="3"> 
     <Grid> 
      <TextBlock Margin="0" TextWrapping="Wrap" Text="Please Wait..." 
      horizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#7EFFFFFF"/> 
    </Border> 
</Grid> 

编辑: BusyViewMOdel

public class BusyViewModel : INotifyPropertyChanged 
{ 
private ICommand doSomethingCommand; 

public event PropertyChangedEventHandler PropertyChanged; 

private bool _isBusy = false; 
public bool IsBusy 
{ 
    get { return _isBusy; } 
    set 
    { 
     _isBusy = value; 
     OnPropertyChanged("IsBusy"); 
    } 
} 

// Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 


public ICommand DoSomething 
{ 
    get { return doSomethingCommand ?? (doSomethingCommand = new DelegateCommand(LongRunningTask)); } 
} 

private void LongRunningTask() 
{ 
    var task = new Task(ComputeResults); 
    task.Start(); 
} 

private void ComputeResults() 
{ 
    this.IsBusy = true; 
    Thread.Sleep(5000); 
    this.IsBusy = false; 
} 
} 

的实施见http://msdn.microsoft.com/de-de/library/ms743695%28v=vs.110%29.aspx


编辑:尝试通过给电网的名称设置根网格数据上下文和代替this.DataContext = ...public MainWindow() ...ROOT.DataContext = ...。查看更新的xaml!


编辑:得到它的工作。请参阅BusyViewModel类的此代码。

private void LongRunningTask() 
    { 
     var task = new Task(ComputeResults); 
     task.Start(); 
    } 

    private void ComputeResults() 
    { 
     this.IsBusy = true; // you did _isBusy = true. but to invoke OnPropertyChanged you need to use the setter, thus IsBusy! Works now even if set in the worker thread. Put it back to ComputeResults! 
     Thread.Sleep(5000); 
     this.IsBusy = false; 
    } 
+0

仍然没有用...你认为是因为'Converter'? – Sonhja

+0

啊,我认为问题存在于别的地方。你能不能在主窗口显示代码? – MABVT

+0

只需编辑并添加。 – Sonhja

相关问题