Reputation: 2107
I can't get access to UI elements from code using x:Name
. They are only available if I use simple Name
. I need to restore the access by x:Name
because it is necessary to use FindName
method. The project was created before I started the work with it, that's why I can't know what could be done wrong before.
It's got the following namespace at the top of the window:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Also, I suppose that code-behind was generated automatically, because it has the following message:
THIS CODE AND INFORMATION ARE GENERATED BY AUTOMATIC CODE GENERATOR
I need to get access to pushpin. I added x:Name="push1" to it. But it is not visible from code-behind.
<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>
Here is namespaces from 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}}">
Metadata from code-behind:
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);
}
}
}
Upvotes: 1
Views: 376
Reputation: 66469
Make sure the following namespace is located at the top of your window:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Also, make sure you're not specifying a Name
and an x:Name
value... only one or the other.
If you try to use both:
<DataGrid Name="dg" x:Name="dg2"></DataGrid>
Then in the code-behind, you will only be able to access it by the first one specified. (In this case, "dg".)
(Trying to use both will prevent the project from compiling too.)
Upvotes: 2