无法在MainPage.g.cs中看到控件 [英] Unable to see controls in MainPage.g.cs

查看:187
本文介绍了无法在MainPage.g.cs中看到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于XAML的简单问题。



我在另一个网格中创建了一个网格元素,并在其中添加了一个文本块和一个Web浏览器。但是,我无法使用他们的名称(例如this.LastName)在MainPage.xaml.cs中访问它。在进一步调试时,我看到它们未在MainPage.g.cs中声明。由于MainPage.g.cs是自动定义的,我不知道如何修复它。有人可以帮忙吗?下面是我的C#和XAML代码。谢谢!

======================================== ================

公共部分类MainPage:Microsoft.Phone.Controls.PhoneApplicationPage {

内部System.Windows。 Controls.Grid LayoutRoot;

内部System.Windows.Controls.Grid ContentPanel;

内部Microsoft.Phone.Controls.LongListSelector MainLongListSelector;

内部System.Windows.Controls.Image RefreshIcon;

private bool _contentLoaded;



public void InitializeComponent(){

if(_contentLoaded){

返回;

}

_contentLoaded = true;

System.Windows.Application.LoadComponent(this,new System.Uri (/ Suod ;component/MainPage.xaml,System.UriKind.Relative));

this.LayoutRoot =((System.Windows.Controls.Grid)(this.FindName(LayoutRoot) )));

this.ContentPanel =((System.Windows.Controls.Grid)(this.FindName(ContentPanel)));

this.MainLongListSelector = ((Microsoft.Phone.Controls.LongListSelector)(this.FindName(MainLongListSelector)));

this.RefreshIcon =((System.Windows.Controls.Image)(this.FindName( RefreshIcon)));

}

=========================== =============================

I have a simple question about XAML.

I created a grid element inside another grid and added a textblock and a web browser in it. However, I am unable to access it in MainPage.xaml.cs using their name (e.g. this.LastName) doesn't work. On further debugging, I saw that they are not declared in MainPage.g.cs. Since MainPage.g.cs is auto defined, I don't know how to fix it. Can someone help please? Below is my C# and XAML code. Thanks!
========================================================
public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage {
internal System.Windows.Controls.Grid LayoutRoot;
internal System.Windows.Controls.Grid ContentPanel;
internal Microsoft.Phone.Controls.LongListSelector MainLongListSelector;
internal System.Windows.Controls.Image RefreshIcon;
private bool _contentLoaded;

public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/Suod;component/MainPage.xaml", System.UriKind.Relative));
this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
this.ContentPanel = ((System.Windows.Controls.Grid)(this.FindName("ContentPanel")));
this.MainLongListSelector = ((Microsoft.Phone.Controls.LongListSelector)(this.FindName("MainLongListSelector")));
this.RefreshIcon = ((System.Windows.Controls.Image)(this.FindName("RefreshIcon")));
}
========================================================

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <phone:LongListSelector x:Name="MainLongListSelector" Margin="-12,-97,0,97" ItemsSource="{Binding Items}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="12,100,0,45">
                    <Grid x:Name="CompanyContentGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Name="LastName" Text="{Binding Name}" TextWrapping="Wrap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
                        <phone:WebBrowser Grid.Row="5" Height="400" Name="WebBrowser" Margin="12,-6,24,0" FontFamily="Portable User Interface"/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</Grid>
<Grid Grid.Row="2" >
    <Image Source="/Assets/Refresh.png" Name="RefreshIcon"  Width="80" Height="80" Tap="Image_Tap"/>
</Grid>

推荐答案

我是很抱歉告诉你这个,但 Grid 的名字是在模板中声明的。该模板将用于创建新的 Grid ,模板中的名称仅供模板参考。同样的事情是 TextBlock WebBrowser 项目。您可以访问的唯一命名项是 ContentPanel MainLongListSelector RefreshIcon ,假设 ContentPanel item和另一个 Grid 不在其他模板中。



另一种看待它的方法就是这样。该模板是一个cookie切割器,将应用于 MainLongListSelector 中的每个项目。因此,每个项目都有 Grid 名为 ContentPanel TextBlock 名为 LastName 等等..



对不起,我不能给你一个解决问题的实际方法,我不知道知道除了访问这些项目之外你还想做什么。您尝试使用关键字来引用这些项的事实让我认为您只希望存在一个命名项的实例,但我不知道如果那是因为你只希望选择器中有一个项目。
I'm sorry to tell you this, but the Grid's name was declared within a template. The template is what will be used to create a new Grid, and the name within the template is for template reference only. The same thing goes for that TextBlock and WebBrowser items. The only named items you can access are the ContentPanel, the MainLongListSelector, and the RefreshIcon that's assuming that the ContentPanel item and the other Grid are not inside an other template.

Another way to look at it is this. That template is a cookie cutter that will be applied to each item in MainLongListSelector. So, each item would have a Grid named ContentPanel, a TextBlock named LastName, etc., etc..

I'm sorry that I can't give you an actual solution to your problem, I don't know what you are trying to do besides access those items. The fact that you are trying to use the this keyword to reference the items makes me think that you only expect one instance of the named items to exist, but I don't know if that's because you only expect there to be one item in the selector.


这篇关于无法在MainPage.g.cs中看到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆