簡體   English   中英

BusyIndi​​cator不支持Silverlight MVVM

[英]BusyIndicator is not working in silverlight MVVM

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

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中,我有代碼來檢索數據。 所以我需要阻止屏幕直到要檢索的數據。 怎么做? 請幫我解決這個問題..

好吧,首先,您要創建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;
    });
}

我找到了Two Way和UpdateSourceTrigger

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM