I am trying to create my hello world windows app in WPF.
What should I do to run this window?
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid> </Grid>
</Window>
<Application x:Class="App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Class1.xaml" >
<Application.Resources>
</Application.Resources>
</Application>
class Program
{
[STAThread]
public static void Main()
{
new App().Run();
}
}
I have created a blank sln and added these three files. I have also added WindowsBase, PresentationBase, PresentationFramework refs.
But the App is not running.
What is the problem?
Creating a new WPF Application would be the way to go, but I may know one way of fixing it.
Your project file probably currently has a section that looks like this:
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
You'll need to change it to use ApplicationDefinition
instead of Page
, similar to this:
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
This way your project should know what to actually start when you run your application.
The solution I pulled this from was a VS2010 solution, but I think it's the same in VS2008.
使用WPF项目?
If you're going to have a Main
method, your App.xaml file will be ignored.
App.xaml is only used if you set its Build Action to be ApplicationDefinition
. When you do this, you'll notice you get a compiler error, as you'll have two entry points into your program -- so you'll have to lose the Main
method.
If you want to keep your Main
method, you can. Don't change the Build Action on App.xaml (actually, I think you can delete it), and do something like this:
[STAThread]
public static void Main()
{
App app = new App();
app.StartupUri = new System.Uri("/Project1;component/Class1.xaml", System.UriKind.Relative);
app.Run();
}
Replace the /Project1
with your namespace.
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.