WPF应用程序级别ViewModel

时间:2012-11-09 20:01:33

标签: wpf mvvm

我是WPF和MVVM的新手,但我正在尝试为我的应用程序中的多个窗口创建应用程序级视图模型。这是我的代码:

ApplicationViewModel.cs:

public class ApplicationViewModel : ViewModelBase
{
   // for MainWindow
   private MainWindowViewModel mainWindowViewModel;
   public MainWindowViewModel MainWindowViewModel
   {
      get { return this.mainWindowViewModel; }
      private set
      {
         this.mainWindowViewModel = value;
         OnPropertyChanged("MainWindowViewModel");
      }
   }

   // other window code...

   public ApplicationViewModel()
   {
      this.mainWindowViewModel = new MainWindowViewModel();

      // Now show the MainWindow
      MainWindow mainWindow = new MainWindow();
      mainWindow.Show();
   }
}

App.xaml中:

<Application x:Class="SomeApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:viewModel="clr-namespace:SomeApp.viewModel">
   <Application.Resources>
      <viewModel:ApplicationViewModel x:Key="applicationViewModel" />
...

MainWindow.xaml:

<Window x:Class="SomeApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:SomeApp.viewModel">
<Grid DataContext="{StaticResource applicationViewModel}">
   <TextBox Name="courseNameTxtBox" Width="200" MaxLength="251"
            Text="{Binding Path=MainWindowViewModel.SomeMainWindowObject,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" />
...

出于某种原因,似乎applicationViewModel从未实例化,因为MainWindow永远不会显示。我做错了什么?

我也试过这个:

App.xaml中:

<Application x:Class="CourseAttendanceTracking.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
...

App.xaml.cs:

public partial class App : Application
{
   private void Application_Startup(object sender, StartupEventArgs e)
   {
      this.Resources.Add("applicationViewModel", new ApplicationViewModel());
   }
}

我收到了这个错误:

  

System.Windows.Markup.XamlParseException发生Message ='Provide   'System.Windows.StaticResourceExtension'上的值引发异常。   行号“15”和行位置“19”   Source = PresentationFramework LineNumber = 15 LinePosition = 19
  StackTrace:at   System.Windows.Markup.XamlReader.RewrapException(Exception e,   IXamlLineInfo lineInfo,Uri baseUri)at   System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,   IXamlObjectWriterFactory writerFactory,Boolean   skipJournaledProperties,Object rootObject,XamlObjectWriterSettings   设置,Uri baseUri)at   System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,   Boolean skipJournaledProperties,Object rootObject,XamlAccessLevel   accessLevel,Uri baseUri)at   System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext   parserContext,Object parent,Boolean closeStream)at   System.Windows.Application.LoadComponent(Object component,Uri   resourceLocator)在SomeApp.MainWindow.InitializeComponent()中   \ MainWindow.xaml:SomeApp.MainWindow..ctor()中的第1行   \ MainWindow.xaml.cs:第25行InnerException:Message =找不到   名为'applicationViewModel'的资源。资源名称是大小写的   敏感。 Source = PresentationFramework StackTrace:           在System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider)   serviceProvider,Boolean allowDeferredReference)           在System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider   服务提供者)           在MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension)   我,IServiceProvider serviceProvider)InnerException:

如果我这样做,我的XAML工作正常:

MainWindow.xaml
<Window.Resources>
   <viewModel:ApplicationViewModel x:Key="applicationViewModel" />

但我真的不想在这个窗口中实例化ApplicationViewModel,因为我无法在另一个窗口中看到它。提前谢谢。

1 个答案:

答案 0 :(得分:2)

尝试更改{StaticResource XXX}

{DynamicResource XXX}