我有一个Windows手机应用程序,需要在其中的一个页面中添加一个用户控件。我想像在ASP.NET页面中那样添加它,而不是作为弹出窗口。如何将此用户控件添加到页面中?
我有一个Windows手机应用程序,需要在其中的一个页面中添加一个用户控件。我想像在ASP.NET页面中那样添加它,而不是作为弹出窗口。如何将此用户控件添加到页面中?
<UserControl x:Class="UserControlExample.NameReporter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<!-- Controls here -->
</UserControl>
当您创建一个新的UserControl并且代码后台类似于时,默认情况下应该这样做
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace UserControlExample
{
public partial class NameReporter : UserControl
{
public NameReporter()
{
InitializeComponent();
}
// your custom methods here
}
}
您应该能够通过类似于以下代码的方式将其添加到页面中:
<Grid xmlns:src="clr-namespace:UserControlExample"
Background="White" Margin="0,50,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<src:NameReporter Grid.Row="0"/>
<src:NameReporter Grid.Row="1" Margin="0,15,0,0"/>
</Grid>
clr-namespace:
之后更改命名空间,在src:
之后更改控件名称即可。xmlns:
标记放入<phone:PhoneApplicationPage>
标记中,以便在整个表单中使用该控件(而不仅在网格中),并且您可以将src
更改为任何您希望引用它的名称。参考文献:
http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol%28v=VS.95%29.aspx