이것은 중복 된 질문 일 수 있지만 문제에 대한 해결책을 찾을 수 없습니다.MVVM을 사용하는 BusyIndicator
MVVM 패턴을 사용하는 WPF 응용 프로그램에서 작업하고 있습니다.
ViewModel에 바인드 된 4 개의 뷰가 있습니다. 모든 ViewModel은 BaseViewModel을 부모로가집니다. 내가 MainViewModel에서 IsBusy = true를 설정하면
<extWpfTk:BusyIndicator IsBusy="{Binding IsBusy}">
<ContentControl />
</extWpfTk:BusyIndicator>
, BusyIndicator가 표시됩니다 :
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는 BusyIndicator가 포함되어 있습니다.
다른 ViewModels에서 IsBusy = true로 설정하려고하면 BusyIndicator가 표시되지 않습니다.
MVVMLight와 같은 타사 라이브러리를 사용하여 메신저를 사용하여 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 : 모든 viewModels에
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;
}
}
}
FYI MVVMLight는 오픈 소스이므로 Messenger 구현 만 추출하고 나머지는 무시할 수 있습니다. 솔루션에 직접 소스가 포함되어 있으므로 타사 라이브러리가 필요하지 않습니다. –
@ AdamHouldsworth 글쎄, 어떤 이유로 MVVMLight에서 WeakReference 클래스를 사용하려고 할 때 SecurtyException이 발생했습니다. 또한 Mediator 패턴을 시도했지만 WeakReference SecurityException에 문제가 있습니다. –
@GazTheDestroyer 다른 4 개의보기에는 MainView에 대한 참조가 포함되어 있지 않습니다. –