2014-06-20 82 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

相關問題