x:Name不显示UI元素,它们仅由Name可见。怎么修?

时间:2014-03-11 11:37:33

标签: c# xaml windows-phone-8

我无法使用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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

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

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

此外,请确保您没有指定Name x:Name值...只有一个或另一个。

如果您尝试同时使用两者:

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

然后在代码隐藏中,您将只能通过指定的第一个访问它。 (在这种情况下,“dg”。)

(尝试使用两者都会阻止项目编译。)