Can';在WinRT XAML中找不到元素

Can';在WinRT XAML中找不到元素,xaml,windows-runtime,custom-controls,Xaml,Windows Runtime,Custom Controls,我有以下CustomControl,我想用作横幅的基础: XAML: C#: 公共密封部分类BannerPanel:UserControl { /// ///横幅名称 /// 公共字符串标题 { 获取{return\u Title.Text;} 设置{u Title.Text=value;} } /// ///“确定”按钮的可见性 /// 公众能见度 { 获取{return\u OK.Visibility;} 设置{u OK.Visibility=value;} } /// ///

我有以下CustomControl,我想用作横幅的基础:

XAML:


C#:

公共密封部分类BannerPanel:UserControl
{
/// 
///横幅名称
/// 
公共字符串标题
{
获取{return\u Title.Text;}
设置{u Title.Text=value;}
}
/// 
///“确定”按钮的可见性
/// 
公众能见度
{
获取{return\u OK.Visibility;}
设置{u OK.Visibility=value;}
}
/// 
///取消按钮的可见性
/// 
公众能见度取消能见度
{
获取{return\u Cancel.Visibility;}
设置{u Cancel.Visibility=value;}
}
/// 
///面板的内部内容
/// 
公共框架元素内部内容
{
获取{return(FrameworkElement)\u Content.Content;}
设置{u Content.Content=value;}
}
/// 
///单击“确定”按钮时激发
/// 
公共活动路线EventHandler OkClick;
/// 
///单击“取消”按钮时激发
/// 
公共事件路由EventHandler取消单击;
};
但是当我使用它时(请参见下面的XAML),自动生成的代码在内部内容中找不到元素:

XAML:


C#:

公共密封的部分类TermsBanner:UserControl
{
/// 
///建造师
/// 
公共术语班纳()
{
this.InitializeComponent();
//为什么我必须像这样手动查找TermsText??????
TermsText=(Banner.InnerContent作为ScrollViewer)。内容作为TextBlock;
Text=TermsOfUse;
}
};
为什么我必须手动将变量TermsText指向正确的对象?为什么它不能像往常一样用FindName()自动找到它呢

  • 您应该定义
    dependencProperty
    属性,而不是常规属性
  • ContentControl
    派生,而不是从
    UserControl
    派生
  • 将XAML放入Themes/Generic.XAML中的
    Style/Setter/Property=“Template”Value=“…
    。如果使用模板化控件与项目模板创建控件,则会自动执行此操作
  • 使用TemplateBinding将内部模板元素的属性绑定到控件属性
  • FindName可能无法跨名称范围工作。我从未使用过它

    请原谅我的打字错误,是用电话打的

    这里有一个解决方案:

    创建了模板化控件(也称为美国自定义控件-不要与用户控件混淆),并将其修改为从
    内容控件
    派生而非
    控件

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    // The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235
    
    namespace App124
    {
        [TemplatePart(Name = "_OK", Type = typeof(Button))]
        [TemplatePart(Name = "_Cancel", Type = typeof(Button))]
        public sealed class BannerPanel : ContentControl
        {
            #region Title
            /// <summary>
            /// Title Dependency Property
            /// </summary>
            public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register(
                    "Title",
                    typeof(string),
                    typeof(BannerPanel),
                    new PropertyMetadata(null, OnTitleChanged));
    
            /// <summary>
            /// Gets or sets the Title property. This dependency property 
            /// indicates ....
            /// </summary>
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the Title property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnTitleChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                string oldTitle = (string)e.OldValue;
                string newTitle = target.Title;
                target.OnTitleChanged(oldTitle, newTitle);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the Title property.
            /// </summary>
            /// <param name="oldTitle">The old Title value</param>
            /// <param name="newTitle">The new Title value</param>
            private void OnTitleChanged(
                string oldTitle, string newTitle)
            {
            }
            #endregion
    
            #region OKVisibility
            /// <summary>
            /// OKVisibility Dependency Property
            /// </summary>
            public static readonly DependencyProperty OKVisibilityProperty =
                DependencyProperty.Register(
                    "OKVisibility",
                    typeof(Visibility),
                    typeof(BannerPanel),
                    new PropertyMetadata(Visibility.Visible, OnOKVisibilityChanged));
    
            /// <summary>
            /// Gets or sets the OKVisibility property. This dependency property 
            /// indicates ....
            /// </summary>
            public Visibility OKVisibility
            {
                get { return (Visibility)GetValue(OKVisibilityProperty); }
                set { SetValue(OKVisibilityProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the OKVisibility property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnOKVisibilityChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                Visibility oldOKVisibility = (Visibility)e.OldValue;
                Visibility newOKVisibility = target.OKVisibility;
                target.OnOKVisibilityChanged(oldOKVisibility, newOKVisibility);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the OKVisibility property.
            /// </summary>
            /// <param name="oldOKVisibility">The old OKVisibility value</param>
            /// <param name="newOKVisibility">The new OKVisibility value</param>
            private void OnOKVisibilityChanged(
                Visibility oldOKVisibility, Visibility newOKVisibility)
            {
            }
            #endregion
    
            #region CancelVisibility
            /// <summary>
            /// CancelVisibility Dependency Property
            /// </summary>
            public static readonly DependencyProperty CancelVisibilityProperty =
                DependencyProperty.Register(
                    "CancelVisibility",
                    typeof(Visibility),
                    typeof(BannerPanel),
                    new PropertyMetadata(Visibility.Visible, OnCancelVisibilityChanged));
    
            /// <summary>
            /// Gets or sets the CancelVisibility property. This dependency property 
            /// indicates ....
            /// </summary>
            public Visibility CancelVisibility
            {
                get { return (Visibility)GetValue(CancelVisibilityProperty); }
                set { SetValue(CancelVisibilityProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the CancelVisibility property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnCancelVisibilityChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                Visibility oldCancelVisibility = (Visibility)e.OldValue;
                Visibility newCancelVisibility = target.CancelVisibility;
                target.OnCancelVisibilityChanged(oldCancelVisibility, newCancelVisibility);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the CancelVisibility property.
            /// </summary>
            /// <param name="oldCancelVisibility">The old CancelVisibility value</param>
            /// <param name="newCancelVisibility">The new CancelVisibility value</param>
            private void OnCancelVisibilityChanged(
                Visibility oldCancelVisibility, Visibility newCancelVisibility)
            {
            }
            #endregion
    
            /// <summary>
            /// Fires when the Ok button is clicked
            /// </summary>
            public event RoutedEventHandler OkClick;
    
            /// <summary>
            /// Fires when the Cancel button is clicked
            /// </summary>
            public event RoutedEventHandler CancelClick;
    
            public BannerPanel()
            {
                this.DefaultStyleKey = typeof(BannerPanel);
            }
    
            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
    
                var cancelButton = (Button)GetTemplateChild("_Cancel");
                var okButton = (Button)GetTemplateChild("_OK");
                cancelButton.Click += CancelClick;
                okButton.Click += OkClick;
            }
        }
    }
    
    使用Windows.UI.Xaml;
    使用Windows.UI.Xaml.Controls;
    //模板化控制项模板记录在http://go.microsoft.com/fwlink/?LinkId=234235
    名称空间App124
    {
    [TemplatePart(Name=“_OK”,Type=typeof(按钮))]
    [TemplatePart(Name=“\u Cancel”,Type=typeof(按钮))]
    公共密封类BannerPanel:ContentControl
    {
    #地区名称
    /// 
    ///标题依赖属性
    /// 
    公共静态只读从属属性TitleProperty=
    从属属性。寄存器(
    “头衔”,
    类型(字符串),
    类型(横幅板),
    新的PropertyMetadata(null,OnTitleChanged));
    /// 
    ///获取或设置标题属性。此依赖项属性
    ///表示。。。。
    /// 
    公共字符串标题
    {
    获取{return(string)GetValue(TitleProperty);}
    set{SetValue(TitleProperty,value);}
    }
    /// 
    ///处理对标题属性的更改。
    /// 
    /// 
    ///上面的
    ///属性的值已更改。
    /// 
    /// 
    ///由以下事件发布的事件数据:
    ///跟踪对此属性的有效值所做的更改。
    /// 
    私有静态void OnTitleChanged(
    DependencyObject d,DependencyPropertyChangedEventArgs e)
    {
    var目标=(BannerPanel)d;
    字符串oldTitle=(字符串)e.OldValue;
    字符串newTitle=target.Title;
    target.OnTitleChanged(旧标题,新标题);
    }
    /// 
    ///为派生类提供处理更改的机会
    ///到所有权属性。
    /// 
    ///旧标题的价值
    ///新的标题值
    私有void OnTitleChanged(
    字符串oldTitle,字符串newTitle)
    {
    }
    #端区
    #区域能见度
    /// 
    ///OKVisibility依赖属性
    /// 
    公共静态只读从属属性OKVisibilityProperty=
    从属属性。寄存器(
    “OKVisibility”,
    类型(可见性),
    类型(横幅板),
    新的PropertyMetadata(Visibility.Visible,OnOKVisibilityChanged));
    /// 
    ///获取或设置OKVisibility属性。此依赖项属性
    ///表示。。。。
    /// 
    公众能见度
    {
    获取{return(可见性)GetValue(OKVisibilityProperty);}
    设置{SetValue(OKVisibilityProperty,value);}
    }
    /// 
    ///处理对OKVisibility属性的更改。
    /// 
    /// 
    ///上面的
    ///属性的值已更改。
    /// 
    /// 
    ///由以下事件发布的事件数据:
    ///跟踪对此属性的有效值所做的更改。
    /// 
    私有静态无效
    
    public sealed partial class BannerPanel : UserControl
    {
        /// <summary>
        /// Title of the Banner
        /// </summary>
        public string Title
        {
            get { return _Title.Text; }
            set { _Title.Text = value; }
        }
    
        /// <summary>
        /// The visibility of the OK button
        /// </summary>
        public Visibility OKVisibility
        {
            get { return _OK.Visibility; }
            set { _OK.Visibility = value; }
        }
    
        /// <summary>
        /// The visibility of the Cancel button
        /// </summary>
        public Visibility CancelVisibility 
        {
            get { return _Cancel.Visibility; }
            set { _Cancel.Visibility = value; }
        }
    
        /// <summary>
        /// The inner content of the panel
        /// </summary>
        public FrameworkElement InnerContent 
        {
            get { return (FrameworkElement)_Content.Content; }
            set { _Content.Content = value; }
        }
    
        /// <summary>
        /// Fires when the Ok button is clicked
        /// </summary>
        public event RoutedEventHandler OkClick;
    
        /// <summary>
        /// Fires when the Cancel button is clicked
        /// </summary>
        public event RoutedEventHandler CancelClick;
    };
    
    <Controls:BannerPanel x:Name="Banner" Title="Terms and Policy" CancelVisibility="Collapsed" OkClick="OnTermsAccepted">
        <Controls:BannerPanel.InnerContent>
            <ScrollViewer Width="500">
                <TextBlock x:Name="TermsText" TextWrapping="Wrap" FontSize="12" />
            </ScrollViewer>
        </Controls:BannerPanel.InnerContent>
    </Controls:BannerPanel>
    
    public sealed partial class TermsBanner : UserControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public TermsBanner()
        {
            this.InitializeComponent();
    
            // Why do I have to find TermsText Manually like this??????
            TermsText = (Banner.InnerContent as ScrollViewer).Content as TextBlock;
            TermsText.Text = TermsOfUse;
        }
    };
    
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    // The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235
    
    namespace App124
    {
        [TemplatePart(Name = "_OK", Type = typeof(Button))]
        [TemplatePart(Name = "_Cancel", Type = typeof(Button))]
        public sealed class BannerPanel : ContentControl
        {
            #region Title
            /// <summary>
            /// Title Dependency Property
            /// </summary>
            public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register(
                    "Title",
                    typeof(string),
                    typeof(BannerPanel),
                    new PropertyMetadata(null, OnTitleChanged));
    
            /// <summary>
            /// Gets or sets the Title property. This dependency property 
            /// indicates ....
            /// </summary>
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the Title property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnTitleChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                string oldTitle = (string)e.OldValue;
                string newTitle = target.Title;
                target.OnTitleChanged(oldTitle, newTitle);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the Title property.
            /// </summary>
            /// <param name="oldTitle">The old Title value</param>
            /// <param name="newTitle">The new Title value</param>
            private void OnTitleChanged(
                string oldTitle, string newTitle)
            {
            }
            #endregion
    
            #region OKVisibility
            /// <summary>
            /// OKVisibility Dependency Property
            /// </summary>
            public static readonly DependencyProperty OKVisibilityProperty =
                DependencyProperty.Register(
                    "OKVisibility",
                    typeof(Visibility),
                    typeof(BannerPanel),
                    new PropertyMetadata(Visibility.Visible, OnOKVisibilityChanged));
    
            /// <summary>
            /// Gets or sets the OKVisibility property. This dependency property 
            /// indicates ....
            /// </summary>
            public Visibility OKVisibility
            {
                get { return (Visibility)GetValue(OKVisibilityProperty); }
                set { SetValue(OKVisibilityProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the OKVisibility property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnOKVisibilityChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                Visibility oldOKVisibility = (Visibility)e.OldValue;
                Visibility newOKVisibility = target.OKVisibility;
                target.OnOKVisibilityChanged(oldOKVisibility, newOKVisibility);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the OKVisibility property.
            /// </summary>
            /// <param name="oldOKVisibility">The old OKVisibility value</param>
            /// <param name="newOKVisibility">The new OKVisibility value</param>
            private void OnOKVisibilityChanged(
                Visibility oldOKVisibility, Visibility newOKVisibility)
            {
            }
            #endregion
    
            #region CancelVisibility
            /// <summary>
            /// CancelVisibility Dependency Property
            /// </summary>
            public static readonly DependencyProperty CancelVisibilityProperty =
                DependencyProperty.Register(
                    "CancelVisibility",
                    typeof(Visibility),
                    typeof(BannerPanel),
                    new PropertyMetadata(Visibility.Visible, OnCancelVisibilityChanged));
    
            /// <summary>
            /// Gets or sets the CancelVisibility property. This dependency property 
            /// indicates ....
            /// </summary>
            public Visibility CancelVisibility
            {
                get { return (Visibility)GetValue(CancelVisibilityProperty); }
                set { SetValue(CancelVisibilityProperty, value); }
            }
    
            /// <summary>
            /// Handles changes to the CancelVisibility property.
            /// </summary>
            /// <param name="d">
            /// The <see cref="DependencyObject"/> on which
            /// the property has changed value.
            /// </param>
            /// <param name="e">
            /// Event data that is issued by any event that
            /// tracks changes to the effective value of this property.
            /// </param>
            private static void OnCancelVisibilityChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = (BannerPanel)d;
                Visibility oldCancelVisibility = (Visibility)e.OldValue;
                Visibility newCancelVisibility = target.CancelVisibility;
                target.OnCancelVisibilityChanged(oldCancelVisibility, newCancelVisibility);
            }
    
            /// <summary>
            /// Provides derived classes an opportunity to handle changes
            /// to the CancelVisibility property.
            /// </summary>
            /// <param name="oldCancelVisibility">The old CancelVisibility value</param>
            /// <param name="newCancelVisibility">The new CancelVisibility value</param>
            private void OnCancelVisibilityChanged(
                Visibility oldCancelVisibility, Visibility newCancelVisibility)
            {
            }
            #endregion
    
            /// <summary>
            /// Fires when the Ok button is clicked
            /// </summary>
            public event RoutedEventHandler OkClick;
    
            /// <summary>
            /// Fires when the Cancel button is clicked
            /// </summary>
            public event RoutedEventHandler CancelClick;
    
            public BannerPanel()
            {
                this.DefaultStyleKey = typeof(BannerPanel);
            }
    
            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
    
                var cancelButton = (Button)GetTemplateChild("_Cancel");
                var okButton = (Button)GetTemplateChild("_OK");
                cancelButton.Click += CancelClick;
                okButton.Click += OkClick;
            }
        }
    }
    
    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App124">
    
        <Style
            TargetType="local:BannerPanel">
            <Setter
                Property="HorizontalContentAlignment"
                Value="Left" />
            <Setter
                Property="VerticalContentAlignment"
                Value="Top" />
            <Setter
                Property="Title"
                Value="Title" />
            <Setter
                Property="OKVisibility"
                Value="Visible" />
            <Setter
                Property="CancelVisibility"
                Value="Visible" />
            <Setter
                Property="Template">
                <Setter.Value>
                    <ControlTemplate
                        TargetType="local:BannerPanel">
                        <Grid
                            Background="#BF8B8B8B">
                            <Grid
                                Height="400"
                                Background="#FF050A7C"
                                VerticalAlignment="Center">
                                <Grid.RowDefinitions>
                                    <RowDefinition
                                        Height="50" />
                                    <RowDefinition
                                        Height="*" />
                                    <RowDefinition
                                        Height="50" />
                                </Grid.RowDefinitions>
                                <TextBlock
                                    x:Name="_Title"
                                    Text="{TemplateBinding Title}"
                                    FontSize="30"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center" />
                                <ContentPresenter
                                    Grid.Row="1"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    ContentTransitions="{TemplateBinding ContentTransitions}"
                                    Content="{TemplateBinding Content}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Margin="{TemplateBinding Padding}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                <StackPanel
                                    Orientation="Horizontal"
                                    Grid.Row="2"
                                    HorizontalAlignment="Center">
                                    <Button
                                        x:Name="_OK"
                                        Content="OK"
                                        Visibility="{TemplateBinding OKVisibility}"
                                        HorizontalAlignment="Center"
                                        FontSize="18"
                                        Width="100"
                                        Margin="20,0" />
                                    <Button
                                        x:Name="_Cancel"
                                        Content="Cancel"
                                        Visibility="{TemplateBinding CancelVisibility}"
                                        HorizontalAlignment="Center"
                                        FontSize="18"
                                        Width="100"
                                        Margin="20,0" />
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    
    <Page
        x:Class="App124.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App124"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid
            Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <local:BannerPanel
                x:Name="Banner"
                Title="Terms and Policy"
                CancelVisibility="Collapsed"
                OkClick="OnTermsAccepted">
                <ScrollViewer
                    Width="500">
                    <TextBlock
                        x:Name="TermsText"
                        Text="Terms and Conditions"
                        TextWrapping="Wrap"
                        FontSize="12" />
                </ScrollViewer>
            </local:BannerPanel>
        </Grid>
    </Page>
    
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace App124
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void OnTermsAccepted(object sender, RoutedEventArgs e)
            {
                new MessageDialog(TermsText.Text).ShowAsync();
            }
        }
    }