我的运行环境是xp+sp2+.net 3.0
1 创建几个文件
app.xaml
文件内容
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="HomePage.xaml">
</Application>
主要是要注意StartupUri,对应的是其将要载入的资源文件
HomePage.xaml
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ExpenseIt.HomePage"
WindowTitle="物品费用"
Title="物品费用 - 主页"
WindowWidth="550" WindowHeight="350">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<DockPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0">
<Canvas DockPanel.Dock="Left" Width="230" Height="100">
<Image Source="watermark.png" />
</Canvas>
<TextBlock VerticalAlignment="Center" FontFamily="黑体" FontWeight="Bold" FontSize="18" Foreground="#0066cc">查看价格报表</TextBlock>
</DockPanel>
<Grid Margin="10" Grid.Column="1" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- 姓名列表-->
<Border Grid.Column="0" Grid.Row="0" Height="30" Padding="5" Background="#4E87D4">
<TextBlock VerticalAlignment="Center" Foreground="White">姓名列表</TextBlock>
</Border>
<ListBox Name="peopleListBox" Grid.Column="0" Grid.Row="1">
<ListBoxItem>Mike</ListBoxItem>
<ListBoxItem>Lisa</ListBoxItem>
<ListBoxItem>John</ListBoxItem>
<ListBoxItem>Mary</ListBoxItem>
</ListBox>
<!-- 显示报表的按钮-->
<Button Grid.Column="0" Grid.Row="2" Margin="0,10,0,0" Width="125" Height="25" HorizontalAlignment="Right" Click="viewButton_Click">显示报表</Button>
</Grid>
</Grid>
</Page>
关注点:
1对应的命名空间和类名x:Class="ExpenseIt.HomePage"
2 页面的宽度、高度WindowWidth="550" WindowHeight="350"
3 窗口标题,WindowTitle="物品费用"
4 Grid的一些定义
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
column需要从0开始,Row也是从0开始
Grid.Column="0" Grid.Row="0"
HomePage.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace ExpenseIt
{
public partial class HomePage : Page
{
public HomePage()
{
InitializeComponent();
}
private void viewButton_Click(object sender, RoutedEventArgs args)
{
// Create a new expense report page and pass it the selected person
// by using the non-default constructor.
MessageBox.Show("hello world");
}
}
}
ExpenseIt.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>ExpenseIt1</AssemblyName>
<TargetType>winexe</TargetType>
<OutputPath>bin\$(Configuration)\</OutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Xml"/>
<Reference Include="System.Data"/>
<Reference Include="WindowsBase"/>
<Reference Include="PresentationCore"/>
<Reference Include="PresentationFramework"/>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml"/>
<Page Include="HomePage.xaml"/>
<Compile Include="HomePage.xaml.cs" />
<Resource Include="watermark.png"/>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/>
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets"/>
</Project>
用msbuild在对应目录下执行,将正常编译文件ExpenseIt1.exe