简体   繁体   中英

how can I run a page in WPF c#?

I want to do something like this, but without XAML only C#

<Page x:Class="Mynamespace.MyPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Mynamespace"
        Title="MyPage"
        Height="350"
        Width="525">
       <DockPanel>
            <...>

            </...>
        </DockPanel>
</Page>

This is what I've got so far:

public class MyPage : Page
{
   public MyPage()
     {
        Width = 525;
        Height = 350;

        // init elements
        DockPanel myDockPanel = new DockPanel();
        this.Content = myDockPanel;
        //...
    }

    [STAThread]
    public static void Main()
    {
        Application app = new Application();
        app.Run(new MyPage());
    }
}

I want my application to run the page. But Application.Run only works for windows. How can I get my application to run a Page?

Thanks for your Time!

You could simply do this:

public static void Main()
{
    Application app = new Application();
    app.Run(new Window() { Content = new Frame() { Content = new MyPage() } });
}

Doing this you're loading a Window , with a Frame inside it. Inside this Frame there'll be your page.

According to Microsoft's Page documentation :

(A Page ) can be navigated to and hosted by Windows Internet Explorer, NavigationWindow, and Frame

(emphasis mine)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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