2014-01-09 78 views
0

大家好, 我需要使用busyindicator屏蔽屏幕。當我點擊視圖(Sample.xaml)時,事件被寫入(Sample.xaml.cs),導致執行寫入名爲(Sample.cs)的單獨類中的代碼。所以在單獨的類文件中我有busyindicator的實現。但它不起作用。BusyIndi​​cator不工作在silverlight MVVM

Sample.xaml:

<UserControl x:Class="Pool.View.CadViewer" 
    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:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignHeight="400" d:DesignWidth="400"> 

    <Grid> 
     <toolkit:BusyIndicator IsBusy="{Binding IsBusy}"> 
     <Grid x:Name="CadLayoutRoot" MouseLeftButtonUp="CadLayoutRoot_MouseLeftButtonUp" MouseRightButtonDown="CadLayoutRoot_MouseRightButtonDown" MouseRightButtonUp="CadLayoutRoot_MouseRightButtonUp" MouseLeftButtonDown="CadLayoutRoot_MouseLeftButtonDown" MouseMove="CadLayoutRoot_MouseMove" MouseWheel="CadLayoutRoot_MouseWheel" LostMouseCapture="CadLayoutRoot_LostMouseCapture" > 
     </Grid> 
     </toolkit:BusyIndicator> 
    </Grid> 

</UserControl> 

Sample.xaml.cs:

private void CadLayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    UpdateViewAndViewModel(); 
} 

private void UpdateViewAndViewModel() 
{ 
    Sample bfmobj = new BFM(View.CadViewer.radius2, View.CadViewer.model.Blocks, 
        View.CadViewer.Step, View.CadViewer.StepType, View.CadViewer.StepPosition); 
} 

Sample.cs:

public class Sample: ViewModelBase 
{ 
    public BFM(bool radius, DxfBlockCollection block, string step, string steptype, string stepposition) 
    { 
     this.Radius = radius; 
     this.Block = block; 
     this.Step = step; 
     this.StepType = steptype; 
     this.StepPosition = stepposition; 

     GetBFMPart(); 

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

      } 
     } 
    } 
    public void GetBFMPart() 
    { 
     IsBusy = true; 
    //code sample 
     IsBusy = false; 
    } 
} 

在函數GetBFMPart我有代碼來檢索數據。所以我需要屏蔽屏幕直到要檢索的數據。怎麼做?請幫助我解決這個問題。

+0

你在哪裏執行'GetBFMPart'方法? – Jehof

+0

我已編輯代碼@Jehof。 –

+0

是否將DataContext設置爲視圖Pool.View.CadViewer的正確值? – Jehof

回答

2

那麼,首先,您正在創建您的Sample對象,但不會將其設置爲您的視圖的DataContext。請修改現有的ViewModel/DataContext或將其替換爲您創建的新對象Sample

private void UpdateViewAndViewModel() 
{ 
    Sample bfmobj = new BFM(View.CadViewer.radius2, View.CadViewer.model.Blocks, 
        View.CadViewer.Step, View.CadViewer.StepType, View.CadViewer.StepPosition); 

    this.DataContext = bfmobj; 
} 

但是,我認爲你也可能遇到與線程有關的問題,但我對Silverlight並不是很熟悉。

在標準的WPF中,框架正在等待您的函數完成才能更新UI,因爲您的函數正在UI線程上運行。由於你的函數並沒有改變這個bool的值(它的確如此,但是因爲沒有別的東西可以在結束之前執行,所以值保持不變),UI保持不變。

你需要在你的代碼,「做工作」上的一個單獨的線程,要做到這一點,最簡單的方法是使用一個任務:

public void GetBFMPart() 
{ 
    IsBusy = true; 

    var task = Task.Factory.StartNew(delegate 
    { 
     // do your work here then set IsBusy = false 
     for (int i = 0; i < 5; i++) 
     { 
      System.Threading.Thread.Sleep(1000); 
     } 

     IsBusy = false; 
    }); 
} 
0

我發現雙向和UpdateSourceTrigger工作

 <toolkit:BusyIndicator HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
      BusyContent="{Binding BusyMessage,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
      IsBusy="{Binding UserControlIsBusy,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
         d:IsHidden="True" /> 
相關問題