具有事件处理程序的UWP模板控件

Sud*_*eer 1 uwp

我是UWP的新手,正在尝试。如果链接太基础,请指向我。我正在开发带有一些文本框和按钮的自定义控件(UWP Template Control)。理想情况下,我想在MainPage中将此控件用作页眉控件,根据TemplateControl中每个按钮的单击,我要呈现不同的页面。现在来解决基本问题,如何在CustomControl中连接事件处理程序,这是我的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>
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我想访问的视图在Project1中可用,因此我想在MainPage.xaml.cs上编写代码以加载那些“内容”或“框架”。

Stu*_*ith 5

如何将按钮单击事件添加到模板控件

为按钮添加一个名称,以便您可以在后面的代码中访问该对象

  <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 Name:"_BtnGo" Content="Go!" Click="MyCustomControl_Click" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

添加了指向按钮控件的指针,并将按钮事件传递给控件中的事件处理程序

private  Button _BtnGo;

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;

    public event EventHandler<RoutedEventArgs> GoClicked;


     protected override void OnApplyTemplate()
        {
        _BtnGo = GetTemplateChild(nameof(_BtnGo)) as Button;
        _BtnGo.Click += (s, e) => GoClicked?.Invoke(s, e);
    }
Run Code Online (Sandbox Code Playgroud)