UtilitiesView是打开Package Extension窗口时的一个用户控件。因此,这是UtilitiesView中的xaml代码:
<UserControl.Resources>
<DataTemplate DataType="{x:Type engines:CalcEngineViewModel}">
<engines:CalcEngineView/>
</DataTemplate>
<DataTemplate DataType="{x:Type engines:TAEngineViewModel}">
<engines:TAEngineView/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="NavContent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width ="*"/>
<ColumnDefinition Width ="*"/>
<ColumnDefinition Width ="*"/>
</Grid.ColumnDefinitions>
<Button Content="Calc"
Command ="{Binding ChangeViewModelCommand}"
CommandParameter="CalculationEngine"
Grid.Column="0"/>
<Button Content="TA"
Command ="{Binding ChangeViewModelCommand}"
CommandParameter="TAEngine"
Grid.Column="1"/>
</Grid>
<Grid x:Name="MainContent"
Grid.Row="1">
<ContentControl Content="{Binding CurrentEngineViewModel}"/>
</Grid>
</Grid>
</UserControl>`
可以看到,有两个按钮通过绑定ChangeViewModelCommand并传递一个字符串值("CalculationEngine"或"TAEngine")来切换视图。
这是UtilitiesViewModel.cs类:
public class UtilitiesViewModel : BindableBase
{
#region Fields
public RelayCommand<string> ChangeViewModelCommand { get; private set; }
private CalcEngineViewModel calcViewModel = new CalcEngineViewModel();
private TAEngineViewModel taViewModel = new TAEngineViewModel();
private BindableBase currentEngineViewModel;
public BindableBase CurrentEngineViewModel
{
get { return currentEngineViewModel; }
set
{
SetProperty(ref currentEngineViewModel, value);
}
}
#endregion
public UtilitiesViewModel()
{
ChangeViewModelCommand = new RelayCommand<string>(ChangeViewModel);
}
#region Methods
public void ChangeViewModel(string viewToShow) //(IEngineViewModel viewModel)
{
switch (viewToShow)
{
case "CalculationEngine":
CurrentEngineViewModel = calcViewModel;
break;
case "TAEngine":
CurrentEngineViewModel = taViewModel;
break;
default:
CurrentEngineViewModel = calcViewModel;
break;
}
}
#endregion
}
这里是BindableBase.cs:
public class BindableBase : INotifyPropertyChanged
{
protected virtual void SetProperty<T>(ref T member, T val, [CallerMemberName] string propertyName = null)
{
if (object.Equals(member, val)) return;
member = val;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我使用一个简单的ViewModelLocator类来将视图与它们的视图模型链接起来:
public static class ViewModelLocator
{
public static bool GetAutoWireViewModel(DependencyObject obj)
{
return (bool)obj.GetValue(AutoWireViewModelProperty);
}
public static void SetAutoWireViewModel(DependencyObject obj, bool value)
{
obj.SetValue(AutoWireViewModelProperty, value);
}
// Using a DependencyProperty as the backing store for AutoWireViewModel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AutoWireViewModelProperty =
DependencyProperty.RegisterAttached("AutoWireViewModel", typeof(bool), typeof(ViewModelLocator), new PropertyMetadata(false, AutoWireViewModelChanged));
private static void AutoWireViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(d)) return;
var viewType = d.GetType();
var viewTypeName = viewType.FullName;
var viewModelTypeName = viewTypeName + "Model";
var viewModelType = Type.GetType(viewModelTypeName);
var viewModel = Activator.CreateInstance(viewModelType);
((FrameworkElement)d).DataContext = viewModel;
}
}
如前所述,使用UtilitiesView.xaml上定义的按钮切换视图是有效的。
工具栏按钮从Package.cs类中调用UtilitiesViewModel.cs中上述提到的ChangeViewModel方法,但即使CurrentEngineViewModel属性被设置为不同的值,它也不会反映在UtilitiesView.xaml上。
当我进行调试时,无论哪种情况,都会正确地进入BindableBase的SetProperty方法,但是在ToolBar按钮的情况下,ViewModelLocator中的AutoWireViewModelChanged方法从未被调用。
我不知道为什么。我原以为UtilitiesView中与UtilitiesViewModel的CurrentEngineViewModel属性的绑定足够了?
我试图将其视为对模型组件进行更改,而视图应该对此做出响应,即使实际上我将ToolBar按钮作为人们认为的视图组件的一部分。
这是在Package.cs类中调用ChangeViewModel方法的方式:
if (Config.Engine.AssemblyPath.Contains("Engines.TimeAndAttendance.dll"))
{
uvm.ChangeViewModel("TAEngine");
}
else //Assume Calculation Engine
{
uvm.ChangeViewModel("CalculationEngine");
}
我希望我已经提供了足够的细节。
更新 1
关于gRex的评论,我认为可能有两个UtilitiesViewModel对象。
这是在打开包扩展的自定义窗口时发生的情况:
public class SymCalculationUtilitiesWindow : ToolWindowPane
{
/// <summary>
/// Initializes a new instance of the <see cref="SymCalculationUtilitiesWindow"/> class.
/// </summary>
public SymCalculationUtilitiesWindow() : base(null)
{
this.Caption = "Sym Calculation Utilities";
this.ToolBar = new CommandID(new Guid(Guids.guidConnectCommandPackageCmdSet), Guids.SymToolbar);
// This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
// we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
// the object returned by the Content property.
this.Content = new UtilitiesView();
}
}
AutoWireViewModelChanged方法用于将UtilitiesViewModel链接到UtilitiesView的Content上。
在Package.cs类中,我有这个字段:
private UtilitiesViewModel uvm;
在Initialize方法中,我有以下内容:
uvm = new UtilitiesViewModel();
在原始帖子上方(UPDATE之前),uvm对象用于调用ChangeViewModel方法并传递适当的字符串参数。
这会给我两个不同的对象,对吗? 如果是这样,并且假设这可能是问题的根本原因,我该如何改进这个问题?必须使UtilitiesViewModel成为单例吗?
更新2
我已经在Github上添加了一个解决方案。功能略有更改,因此我不需要与原始解决方案的其他部分进行任何交互。 因此,连接按钮(工具栏上)使用“TAEngine”参数调用ChangeViewModel方法,保存按钮(工具栏上)也是如此,但是使用“CalculationEngine”作为参数。目前,DataTemplates仍然被注释,因此只能看到文本中的类名。 这里是链接。在Visual Studio的实验实例中,可以在View -> Other Windows -> SymplexityCalculationUtilitiesWindow找到该窗口。 如果您没有Visual Studio SDK,则可能需要下载它。
更新3
我使用Unity IoC容器和ContainerControlledLifetimeManager来确保我没有两个不同的UtilitiesViewModels。在实现这一点后,工具栏按钮可以导航到正确的视图。