【问题标题】:Binding app.xaml to view WPF in c#绑定app.xaml以在c#中查看WPF
【发布时间】:2015-04-22 05:26:35
【问题描述】:

我使用 MVVM 框架并在网上获得了这个教程:https://code.msdn.microsoft.com/windowsdesktop/How-to-use-MVVM-Pattern-0e2f4571 和 http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/

现在,我的问题是:

即使没有语义错误,我也无法显示 mainpage.xaml。这是我在 app.xaml.cs 上的代码:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    BasicWPF.View.MainPage window = new MainPage();
    UserViewModel VM = new UserViewModel();
    window.DataContext = VM;
    window.Show();
}

谁能帮帮我?谢谢你的帮助! :)

感谢所有帮助过的人。

[已解决]

将 app.xaml 中的 startupuri 更改为要加载的页面。就我而言

1:我改变它:

StartupUri="View/MainPage.xaml"

2:在 app.xaml.cs 中,我输入了以下代码:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    BasicWPF.View.MainPage window = new MainPage();
    UserViewModel VM = new UserViewModel();
    window.DataContext = VM;
}

从我之前的代码中,删除这段代码:window.show();因为它两次启动来自 app.xaml 和 app.xaml.cs 的页面。为了防止这种情况,删除:window.show();

再次感谢您! :)

【问题讨论】:

  • 为什么要在 OnStartup 中手动显示主页?我认为您只需要设置在主页加载时也可以设置的视图模型。
  • 什么意思?我不明白你的问题。
  • 你能展示你的 App.xaml 吗?
  • 默认 MainPage.xaml.cs 是否存在,是否包含默认 InitializeComponent() 调用?
  • MainPage.xaml.cs 存在,是的,它包含初始化组件调用

标签: c# .net wpf xaml mvvm


【解决方案1】:

在 app.xaml 中设置起始页,而不是在 app.xaml.cs 文件中 - 在 Application 标签中,如果没有属性 StartupUri - 添加一个并将其值设置为您的页面名称,这样页面将是应用程序启动后自动显示。它应该看起来像这样:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainPage.xaml">

我正在考虑您这样做是因为您希望设置页面的 DataContext,但是有更好的方法来设置页面的 DataContext,它是直接将其设置到您的 XAML 代码中。这是一个例子:

<Page x:Class="WpfApplication1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" >
    <Page.DataContext>
        <local:UserViewModel/>
    </Page.DataContext>

xmlns:local 是映射到您为其设置的命名空间的前缀。有了这个,您就可以使用前缀 - local:UserViewModel 访问命名空间中包含的类型。

【讨论】:

  • 您的第二个建议确实有效,但它仍然显示 mainwindow.xaml
  • 当我对我的代码执行第一个和第二个示例时,它会运行,但出现错误,指出发生 system.exception.io。
  • 看这里stackoverflow.com/questions/6518603/…,特别是将启动URI更改为完全限定的URI我不知道你的页面完全限定名称是什么,但我猜你发布它是 BasicWPF.View.MainPage,所以您可能应该输入 BasicWPF/View/MainPage.xaml
  • 但是,主窗口仍然弹出
  • app.xaml 文件中是否还有另一个属性 StartuUri = "MainWindow.xaml"?如果有删除它,如果没有,请查看您的 app.xaml.cs 文件,看看它是否从那里显示
【解决方案2】:

您可以做的不是在app.xaml.cs中设置数据上下文,而是挂钩主窗口的加载事件并添加以下代码。

   public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        UserViewModel VM = new UserViewModel();
        this.DataContext = VM;
    }  

这应该可行。不要忘记从 App.xaml.cs 中删除代码。 谢谢

【讨论】:

  • 它不起作用。它仍然加载主窗口而不是主页。主页在“查看”下
  • 添加了另一个使用不同方法的答案。
【解决方案3】:

所以在您的情况下,您在该主窗口中有一个主窗口,您需要加载页面。 您可以做的是向主窗口添加一个框架,例如

 <Frame x:Name="myFrame"/>

然后在主窗口加载事件中添加以下代码

  void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        UserViewModel VM = new UserViewModel();
        this.DataContext = VM;
        myFrame.Content = new MainPage();      
    }

这就像我们正在添加一个框架并将您的视图加载到该框架。

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多