【问题标题】:MVVM Light - Changing the startup URIMVVM Light - 更改启动 URI
【发布时间】:2013-10-10 08:29:46
【问题描述】:

创建了一个新的 WPF 4.5 MVVM 轻量级应用程序后,我想更改启动 URI,以便在应用程序启动之前进行一些检查。我对 App.xaml 进行了以下更改:

<Application x:Class="MvvmLight1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         StartupUri="MainWindow.xaml"
         mc:Ignorable="d">

收件人:

<Application x:Class="MvvmLight1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d">

并在App.xaml.cs 中添加了OnStartup 方法:

public partial class App : Application
{
    static App()
    {
        DispatcherHelper.Initialize();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        //base.OnStartup(e);

        MainWindow win = new MainWindow();
        win.Show();
    }
}

这样做似乎改变了窗口运行的上下文。我确实尝试将数据上下文设置为 MainViewModel,但这似乎没有帮助。

【问题讨论】:

  • 您对Application 的定义做了哪些更改?我看不出有什么区别……你还有StartupUri="MainWindow.xaml"。你在哪里设置DataContext?我也没有看到。
  • 很好看,虽然这完全是因为我的格式不好!
  • 就 DataContext 而言,更改它没有任何区别,这就是我没有包含它的原因。
  • 把它从什么改成什么?请向我们展示,以便我们了解您的问题的背景。

标签: c# .net wpf mvvm mvvm-light


【解决方案1】:

您似乎没有将 Startup 处理程序添加到您的 Application 定义中...试试这个:

<Application x:Class="MvvmLight1.App"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d" Startup="OnStartup">   <!--   <<< Here   -->

更新>>>

您不能为此使用任何方法...您似乎没有为您的启动处理程序使用正确的方法定义...我的看起来像这样:

public void App_Startup(object sender, StartupEventArgs e)
{

}

尝试将object sender 参数添加到您的处理程序。

【讨论】:

  • 好吧,这行不通。但是,如果我将启动设置为新方法,它确实有效。但是,新启动方法上的断点表明它实际上从未触发过它!为什么这行得通? (顺便说一句,override OnStartup 上的断点表明无论是否设置了启动属性,它都会执行。
  • 我正在覆盖 OnStartup 方法 - 这不仅仅是一种随机方法。就像我说的,我尝试添加处理程序,但它永远不会触发(即使它似乎确实解决了问题)
  • 我的错误。不要覆盖OnStartup 方法,而是尝试使用所需的System.Windows.StartupEventHandler 处理Application.Startup Event,就像我展示的那样。然后处理程序将被调用。
【解决方案2】:

您应该在 App.xaml.cs 中有一些内容,例如:

base.OnStartup(e);
var window = new MainWindowView();
var viewModel = new MainWindowViewModel();
window.DataContext = viewModel;
window.Show();

或在 MainView.xaml 中:

<Window.DataContext>
   <vms:MainWindowViewModel />
</Window.DataContext>

更新>>>

Okej 我下载了 MVVM Light Project 4.5 WPF 添加到我的 VS2012,我做了什么: 1. 删除 App.xaml 中的 StartUp 2. 删除 MainWindow.xaml 中的数据上下文声明 3. 创建如下代码:

  protected override void OnStartup(StartupEventArgs e)
  {
     base.OnStartup(e);
     var window = new MainWindow {DataContext = new MainViewModel(new DataService())};
     window.Show();
  }

一切正常。

【讨论】:

  • 这不起作用,因为 MVVM light 中的主视图模型有一个接受数据服务的构造函数。此外,它没有解释为什么它在将 startuuri 设置为主视图时起作用
  • Okej 编辑了我的帖子。我下载了 WPF 4.5 MVVM Light Application 并在我的帖子中做了类似的事情。
  • 你是对的......这是 DataContext 声明......一旦它被删除,它就可以在没有明确处理启动事件的情况下工作(我仍然不明白)。但是,现在这意味着无法看到设计时数据。
猜你喜欢
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
相关资源
最近更新 更多