简体   繁体   English

绑定app.xaml以在C#中查看WPF

[英]Binding app.xaml to view WPF in c#

I use MVVM framework and got this tutorial on net: https://code.msdn.microsoft.com/windowsdesktop/How-to-use-MVVM-Pattern-0e2f4571 and http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/ 我使用MVVM框架,并在网上获得了本教程: https : //code.msdn.microsoft.com/windowsdesktop/How-to-use-MVVM-Pattern-0e2f4571http://www.c-sharpcorner.com/UploadFile / raj1979 /简单MVVM图案在-WPF /

Now, my problem is: 现在,我的问题是:

I can't display the mainpage.xaml even there is no semantic error. 即使没有语义错误,我也无法显示mainpage.xaml。 Here's my code on app.xaml.cs: 这是我在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();
}

Can anyone help me? 谁能帮我? Thanks for any help! 谢谢你的帮助! :) :)

Thanks to everyone who helped. 感谢所有提供帮助的人。

[SOLVED] [解决了]

Change the startupuri in app.xaml to where the page you want to load. 将app.xaml中的startupuri更改为要加载的页面。 In my case 就我而言

1: I change it: 1:我更改它:

StartupUri="View/MainPage.xaml"

2: In app.xaml.cs, I typed in this code: 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;
}

from my previous code, delete this code: window.show(); 从我之前的代码中删除此代码:window.show(); because it startup the page twice coming from app.xaml and app.xaml.cs. 因为它两次启动了来自app.xaml和app.xaml.cs的页面。 To prevent that, delete: window.show(); 为了防止这种情况,请删除:window.show();

Thank you again! 再次感谢你! :) :)

Set the starting page in app.xaml, not the app.xaml.cs file - in the Application tag, if there is no property StartupUri - add one and set its value to your page name, this way the page will be automatically shown as soon as your application is started. 在app.xaml中而不是app.xaml.cs文件中设置起始页面-在Application标记中,如果没有属性StartupUri-添加一个并将其值设置为您的页面名称,这样该页面将自动显示为您的应用程序启动后。 It should look something like this: 它看起来应该像这样:

<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">

I am considering that you are doing it this way because you wish to set the DataContext of the page, but there is a better way to set the DataContext of your page and it is by setting it directly into your XAML code. 我正在考虑这样做,因为您希望设置页面的DataContext,但是有一种更好的方法来设置页面的DataContext,它是通过直接将其设置到XAML代码中来实现的。 Here is an example: 这是一个例子:

<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 is a prefix mapping to the namespace you have set for it. xmlns:local是映射到您为其设置的名称空间的前缀。 Having this you are able to access the types contained in the namespace using the prefix - local:UserViewModel . 有了这个,您就可以使用前缀-local:UserViewModel访问名称空间中包含的类型。

what you can do is instead of setting data context in app.xaml.cs, just hook up the loaded event of main window and add the following code. 您可以做的是代替在app.xaml.cs中设置数据上下文,只需挂接主窗口的已加载事件并添加以下代码即可。

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

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

This should work. 这应该工作。 Dont forget to remove the codes from App.xaml.cs. 不要忘记从App.xaml.cs中删除代码。 Thanks 谢谢

SO in your case you have a main window inside that main window you need to load the page. 因此,在您的情况下,您需要在该主窗口内加载一个主窗口。 What you can do is add a frame to your mainwindow like 您可以做的是在主窗口中添加一个框架,例如

 <Frame x:Name="myFrame"/>

then inside the mainwindow loaded event add the below code 然后在mainwindow加载的事件内添加以下代码

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

This is like we are adding a frame and loading your view to that frame. 就像我们要添加一个框架并将您的视图加载到该框架一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM