我刚开始接触UWP并进行尝试。如果有太基础的内容,请给我提供链接。 我正在开发一个自定义控件(UWP模板控件),其中包含一些文本框和按钮。理想情况下,我希望将该控件用作我的主页面中的标题控件,并根据模板控件中每个按钮的单击事件来呈现不同的页面。 现在进入基本问题,如何在自定义控件中挂钩事件处理程序? 这是我的Generic.xaml:(Project1.Library)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP.CustomControls.Library">
<Style TargetType="local:MyCustomControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyCustomControl">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding FirstName}" Margin="8 8 2 8" />
<Button Content="Go!" Click="MyCustomControl_Click" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MyCustomContro.cs:
public string FirstName
{
get { return (string)GetValue(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
public static readonly DependencyProperty FirstNameProperty =
DependencyProperty.Register("FirstName", typeof(string), typeof(MyCustomControl), new PropertyMetadata(string.Empty));
public event EventHandler Click;
MainPage.xaml: ( Project1)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<controls:MyCustomControl Width="400" Height="35" Background="Orange"
Margin="20" FirstName="MyFName" LastName="MyLName" Click="MyCustomControl_Click"/>
<Button Content="Show!" x:Name="Text1" />
</Grid>
我想要访问的视图在Project1中可用,因此我想在MainPage.xaml.cs上编写代码来加载这些内容或框架。