使用WPF MVVM, 我有一个主窗口和几个usercontrol。基于按钮单击我显示视图和视图模型。当我点击主窗口中的按钮时,我可以导航到不同的视图。因为导航命令仅在主窗口视图模型中。但当我在不同的视图(用户控制)。我如何从viewmodel导航。
MainwindowViewmodel
private void setFragment(int grpPos, int childPos, String fragmentName, boolean isDrawerToBeClosed) {
try {
imgHome.setVisibility(View.GONE);
switch (grpPos) {
case 0:
callHomeFragment();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void callHomeFragment() {
try {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragmentExisting = fragmentManager.findFragmentByTag(
getResources().getString(R.string.menu_home));
if (fragmentExisting != null) {
Fragment Home = fragmentManager.findFragmentByTag(getResources().getString(R.string.menu_home));
if (fragmentExisting.equals(Home)) {
Fragment fragment2 = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.fragment_content_frame, fragment2,
getResources().getString(R.string.menu_home)).commit();
}
showFragment(fragmentExisting, fragmentManager);
drawer.closeDrawer(GravityCompat.START);
} else {
Fragment fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.fragment_content_frame, fragment,
getResources().getString(R.string.menu_home)).commit();
drawer.closeDrawer(GravityCompat.START);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void showFragment(Fragment fragmentExisting, FragmentManager fragmentManager) {
FragmentManager fragmentManagerInternal = getSupportFragmentManager();
for (int i = 0; i < fragmentManagerInternal.getBackStackEntryCount(); ++i) {
fragmentManagerInternal.popBackStack();
}
fragmentManagerInternal.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
try {
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments != null && fragments.size() > 0) {
for (int i = 0; i < fragments.size(); i++) {
if (fragments.get(i) != null) {
fragmentManager.beginTransaction().hide(fragments.get(i)).commit();
}
}
}
fragmentManager.beginTransaction().show(fragmentExisting).commit();
} catch (Exception exception) {
// ((MyApplication) getApplicationContext()).printStackTrace(exception);
}
}
RegisterViewModel
public class MainWindowViewModel : BindableBase, INotifyPropertyChanged
{
private StudyViewModel _studyViewModel = new StudyViewModel();
private ImageCaptureViewModel _imageCaptureViewModel = new ImageCaptureViewModel();
private RegisterViewModel _registerViewModel = new RegisterViewModel();
private BindableBase _CurrentViewModel;
public BindableBase CurrentViewModel
{
get { return _CurrentViewModel; }
set { SetProperty(ref _CurrentViewModel, value); OnPropertyChanged("_CurrentViewModel"); }
}
public MainWindowViewModel()
{
NavCommand = new RelayCommand<string>(onNav);
onNav("study");
}
//This is the command am using in xaml to redirect view.
public RelayCommand<string> NavCommand { get; private set; }
private void onNav(string destination)
{
switch (destination)
{
case "study":
CurrentViewModel = _studyViewModel;
break;
case "capture":
CurrentViewModel = _imageCaptureViewModel;
break;
case "register":
CurrentViewModel = _registerViewModel;
break;
default:
CurrentViewModel = _studyViewModel;
break;
}
}
}
当在鼠标事件中添加命令时,它正在重定向。但我不知道如何从viewmodel重定向
RegisterView.xaml
public void RegisterPatient(string action)
{
if (action == "addnew")
{
}
else
{
if (StartImaging)
{
//Have to redirect to other screen.
}
}
var a = PatientToAdd;
MessageBox.Show("triggered");
}
答案 0 :(得分:0)
你的问题不够明确,所以这是基于我认为你需要的。
主窗口:
<Window x:Class="MyApp.MainWindow" ...>
...
<ContentControl Content={Binding MyCurrentViewModel}>
<ContentControl.Resources>
<DataTemplate DataType={x:Type vm:UserListViewModel}>
<view:UserListView />
</DataTemplate>
<DataTemplate DataType={x:Type vm:AnotherViewModel}>
<view:AnotherView />
</DataTemplate>
...
</ContentControl.Resources>
</ContentControl>
</Window>
MainViewModel:
private ViewModelBase _myCurrentViewModel;
public ViewModelBase MyCurrentViewModel
{
get { return _myCurrentViewModel; }
set
{
if (value != _myCurrentViewModel)
{
_myCurrentViewModel = value;
RaisePropertyChanged("MyCurrentViewModel");
}
}
}
UserListViewModel:
public class UserListViewModel : ViewModelBase
{
private MainViewModel MainVM;
public UserListViewModel(MainViewModel mainVM)
{
this.MainVM = mainVM;
}
private ICommand _myCommand;
public ICommand MyCommand
{
get
{
if (_myCommand = null)
_myCommand = new RelayCommand(MyExecuteDelegate, true);
return _myCommand;
}
}
private void MyExecuteDelegate(object parameter)
{
// My other logic
if (this.MainVM != null)
this.MainVM.MyCurrentViewModel = new AnotherViewModel();
}
}