簡體   English   中英

使用MVVM的BusyIndi​​cator

[英]BusyIndicator using MVVM

這可能是重復的問題,但我找不到我的問題的解決方案。

我正在使用MVVM模式處理WPF應用程序。

有四個視圖綁定到他們的ViewModel。 所有ViewModel都將BaseViewModel作為父級。

public abstract class ViewModelBase : INotifyPropertyChanged
{
    private bool isbusy;

    public bool IsBusy
    {
        get
        {
            return isbusy;
        }
        set
        {
            isbusy = value;
            RaisePropertyChanged("IsBusy");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainView包含BusyIndi​​cator:

<extWpfTk:BusyIndicator IsBusy="{Binding IsBusy}">
        <ContentControl />
    </extWpfTk:BusyIndicator>

如果我在MainViewModel中設置IsBusy = true,則顯示BusyIndi​​cator。

如果我嘗試從其他ViewModel設置IsBusy = true,則不顯示BusyIndi​​cator。

請注意,我不能在我的項目中使用第三方庫,如MVVMLight,以便使用他們的Messenger在ViewModels之間進行通信。

的MainView:

public class MainWindowViewModel : ViewModelBase
{
    public ViewModel1 ViewModel1 { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ViewModel3 Model3 { get; set; }

    public MainWindowViewModel()
    {
        ViewModel1 = new ViewModel1();
        ViewModel2 = new ViewModel2();
        ViewModel3 = new ViewModel3();
        //IsBusy = true; - its working
    }
}

ViewModel1:

public class ViewModel1 : ViewModelBase
{
    RelayCommand _testCommand;

    public ViewModel1()
    {
    }

    public ICommand TestCommand
    {
        get
        {
            if (_testCommand == null)
            {
                _testCommand = new RelayCommand(
                    param => this.Test(),
                    param => this.CanTest
                    );
            }
            return _testCommand;
        }
    }

    public void Test()
    {
        //IsBusy = true; - BusyIndicator is not shown

    }

    bool CanTest
    {
        get 
        { 
            return true; 
        }
    }
}
   public class MainWindowViewModel : ViewModelBase
   {
    public ViewModel1 ViewModel1 { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ViewModel3 Model3 { get; set; }

    public MainWindowViewModel()
    {
        ViewModel1 = new ViewModel1();
        ViewModel2 = new ViewModel2();
        ViewModel3 = new ViewModel3();
        ViewModel1.PropertyChanged += (s,e) => 
        {
           if(e.PropertyName == "IsBusy") 
           { 
              // set the MainWindowViewModel.IsBusy property here
              // for example:
              IsBusy = ViewModel1.IsBusy;
           }
         }
    //IsBusy = true; - its working
   }
}

將subcsription重復到所有viewModel。

當你不需要它時,不要忘記取消訂閱事件,以避免內存泄漏。

你的問題是:你綁定到MainWindowViewModel的propetry,而不是內部ViewModel的屬性。

暫無
暫無

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

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