[英]Hello World [WPF]
我正在嘗試在WPF中創建我的hello world Windows應用程序。
我應該怎么做才能運行此窗口?
<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();
}
}
我創建了一個空白的sln並添加了這三個文件。 我還添加了WindowsBase,PresentationBase,PresentationFramework引用。
但是該應用程序未運行。
問題是什么?
創建新的WPF應用程序將是解決之道,但我可能知道修復它的一種方法。
您的項目文件當前可能具有如下所示的部分:
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
您需要將其更改為使用ApplicationDefinition
而不是Page
,類似於:
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
這樣,您的項目應該知道在運行應用程序時實際開始什么。
我從中得到的解決方案是VS2010解決方案,但我認為在VS2008中是相同的。
使用WPF項目?
如果您要使用Main
方法,則您的App.xaml文件將被忽略。
僅當將Build Action的Build Action設置為ApplicationDefinition
才使用App.xaml。 執行此操作時,您會注意到遇到編譯器錯誤,因為程序中有兩個入口點,因此您將不得不丟失Main
方法。
如果要保留Main
方法,可以。 不要更改App.xaml上的“構建操作”(實際上,我認為您可以刪除它),並執行以下操作:
[STAThread]
public static void Main()
{
App app = new App();
app.StartupUri = new System.Uri("/Project1;component/Class1.xaml", System.UriKind.Relative);
app.Run();
}
用您的名稱空間替換/Project1
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.