UI元素对x:Name不可见,它们只对Name可见.如何修复

本文关键字:Name 可见 何修复 元素 UI | 更新日期: 2023-09-27 17:49:24

我无法从使用x:Name的代码访问UI元素。它们只有在我使用简单的Name时才可用。我需要通过x:Name恢复访问,因为必须使用FindName方法。这个项目是在我开始工作之前创建的,这就是为什么我不知道之前可能会出错的原因。

在窗口的顶部有如下的命名空间:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

同样,我认为代码隐藏是自动生成的,因为它有以下消息:

此代码和信息由自动代码生成器生成

我需要访问图钉。我添加了x:Name="push1"。但是在代码后面是不可见的。

<maps:Map ZoomLevel="18" Height="575" Center="{Binding CurrentTopIssuesSchema.Coords, TargetNullValue=null}" Name="map1" Width="415" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,-10,0">
    <toolkit1:MapExtensions.Children>
        <toolkit1:Pushpin x:Name="push1" Background="{Binding CurrentTopIssuesSchema.StatusColor}" GeoCoordinate="{Binding CurrentTopIssuesSchema.Coords, TargetNullValue=null}" Content="{Binding CurrentTopIssuesSchema.Name}" ContentTemplate="{StaticResource Template_Content}">
        </toolkit1:Pushpin>
    </toolkit1:MapExtensions.Children> 
</maps:Map>

以下是来自XAML的名称空间:

<phone:PhoneApplicationPage 
x:Class="WPAppStudio.View.TopIssues_Detail"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctl="clr-namespace:WPAppStudio.Controls"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:mytoolkit="clr-namespace:MyToolkit.Controls;assembly=MyToolkit.Extended"
xmlns:mytoolkitpaging="clr-namespace:MyToolkit.Paging;assembly=MyToolkit.Extended"
xmlns:helpers="clr-namespace:WPAppStudio.Helpers"
xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:toolkit1="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource CustomApplicationTextBrush}"
xmlns:syncfusion="clr-namespace:Syncfusion.WP.Controls.Layout;assembly=Syncfusion.SfTileView.WP8"
xmlns:Notification="clr-namespace:Syncfusion.WP.Controls.Notification;assembly=Syncfusion.SfHubTile.WP8"
SupportedOrientations="Portrait" Orientation="Portrait"
DataContext="{Binding Path=TopIssues_DetailViewModel, Source={StaticResource ViewModelLocator}}">

来自代码后置的元数据:


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Maps.Controls;
    using Microsoft.Phone.Maps.Toolkit;
    using Microsoft.Phone.Shell;
    using MyToolkit.Paging;
    using WPAppStudio.Ioc;
    using WPAppStudio.Localization;
    using WPAppStudio.Services.Interfaces;
    using WPAppStudio.ViewModel;
    using WPAppStudio.ViewModel.Interfaces;
    using WPAppStudio.Repositories.Interfaces;
    using Syncfusion.WP.Controls.Layout;
    using WPAppStudio.Entities;
    using WPAppStudio.Repositories.Base;
    using System.Windows.Media.Imaging;
    using System.IO;
    using WPAppStudio.Helpers;
    using Microsoft.Phone.Maps.Services;
    using System.Device.Location;
    using System.Text;
    using System.Xml.Linq;
    using System.IO.IsolatedStorage;
    namespace WPAppStudio.View
    {
        /// 
        /// Phone application page for TopProducts_Detail.
        /// 
        [System.Runtime.CompilerServices.CompilerGenerated]
        [System.CodeDom.Compiler.GeneratedCode("Radarc", "4.0")]
        public partial class TopIssues_Detail : PhoneApplicationPage
        {
            /// 
            /// Initializes the phone application page for TopProducts_Detail and all its components.
            /// 
            public TopIssues_Detail()
            {
                InitializeComponent();
                if (Resources.Contains("PanoramaTopIssues_Detail0AppBar"))
                    PhonePage.SetApplicationBar(this, Resources["PanoramaTopIssues_Detail0AppBar"] as BindableApplicationBar);
            }
        }
    }

UI元素对x:Name不可见,它们只对Name可见.如何修复

确保下面的命名空间位于窗口的顶部:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

另外,请确保您没有指定Name x:Name值…只能选其一

如果你想同时使用:

<DataGrid Name="dg" x:Name="dg2"></DataGrid>

那么在代码隐藏中,您将只能通过指定的第一个访问它。(本例中为"dg".)

(尝试同时使用将阻止项目编译。)