简体   繁体   English

引用Windows 8.1项目中xaml.cs文件中的xaml元素

[英]Refer to a xaml element in xaml.cs file in Windows 8.1 project

Hello I am currently learning Windows 8.1 development with Visual Studio 2015. 您好,我目前正在学习使用Visual Studio 2015进行Windows 8.1开发。
How can I refer to a xaml element in a .xaml file from the associated .xaml.cs file . 如何从关联的.xaml.cs文件引用.xaml文件中的xaml元素

MainPage.xaml file: MainPage.xaml文件:

<Page
x:Class="project.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:project"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

        <HubSection Width="600" x:Uid="Section1Header" Header="Map">
        <DataTemplate>
            <Grid>
                <Button x:Name="mapButton" Content="Find my location"/>
            </Grid>
        </DataTemplate>
    </HubSection>
//...

MainPage.xaml.cs file: MainPage.xaml.cs文件:

namespace project
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            mapButton.Click += mapButton_Click;
        }
}

On mapButton I get the error : The name 'mapButton' doesn't exist in the actual context. mapButton我收到错误消息:名称'mapButton'在实际上下文中不存在。
I thought x:Name was a way to give a name with wich I can access the xaml element from the .xaml.cs file. 我以为x:Name是一种使用来x:Name的方法,我可以从.xaml.cs文件访问xaml元素。

The problem here is that you are trying to access the name of a button from a generated content. 这里的问题是您试图从生成的内容访问按钮的名称。 mapButton is not in the scope of the Page but in the scope of HubSection . mapButton不在Page范围内,而在HubSection范围内。 What you really have to do, if you want to access the button element is to use the VisualTreeHelper to get the button at runtime. 如果要访问button元素,实际上要做的是使用VisualTreeHelper在运行时获取按钮。

Here is an example. 这是一个例子。

Helper function: 辅助功能:

internal static void FindChildren<T>(List<T> results, DependencyObject startNode) where T : DependencyObject
{
    int count = VisualTreeHelper.GetChildrenCount(startNode);
    for (int i = 0; i < count; i++)
    {
        DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
        if ((current.GetType()).Equals(typeof(T)) || (current.GetType().GetTypeInfo().IsSubclassOf(typeof(T))))
        {
            T asType = (T)current;
            results.Add(asType);
        }
        FindChildren<T>(results, current);
    }
}

Accessing the button: 访问按钮:

public MainPage()
{
    this.InitializeComponent();
    Loaded += (sender, e) =>
    {
        List<Button> results = new List<Button>();
        FindChildren(results, Hub);
        var mapButton = results.Find(item => item.Name.Equals("mapButton"));
        mapButton.Click += mapButton_Click;
    };
}

private void mapButton_Click(object sender, RoutedEventArgs arg)
{
    // Do something...
}

Although if you really wanted to map a command to Click , you should consider doing it in the XAML through binding . 尽管如果您确实想将命令映射到Click ,则应该考虑通过binding在XAML中进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM