WPF应用程序级别ViewModel

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

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

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

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


尝试更改{DynamicResource XXX}{StaticResource XXX} {DynamicResource XXX}

链接地址: http://www.djcxy.com/p/30521.html

上一篇: WPF Application level ViewModel

下一篇: CanExecute on RelayCommand<T> not working