无法在MainPage.xaml的MainPage.g.cs中看到我的控件

本文关键字:MainPage cs 我的 控件 xaml | 更新日期: 2023-09-27 18:13:47

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

我在另一个网格中创建了一个网格元素,并在其中添加了一个文本块和一个web浏览器。然而,我无法访问它在mainpage . example .cs使用他们的名字(例如this.LastName)不工作。在进一步的调试中,我看到它们没有在MainPage.g.cs中声明。由于MainPage.g.cs是自动定义的,我不知道如何修复它。有人能帮忙吗?下面是我的c#和XAML代码。谢谢!========================================================

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;
    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    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>

无法在MainPage.xaml的MainPage.g.cs中看到我的控件

我认为问题是你的textblock在xaml代码中太深了。我认为数据绑定在这种情况下应该足够了,但是有一种方法可以绕过这个问题。您必须将此元素订阅到某些事件(如Loaded),并将sender保存到代码后面的某些属性中:

<TextBlock Loaded="LastName_Loaded" Grid.Row="0" Name="LastName" Text="{Binding Name}" TextWrapping="Wrap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
public TextBlock lastName;
private void LastName_Loaded(object sender, RoutedEventArgs e)
{
   lastName= (TextBlock)sender;
}

现在您可以在后面的代码中访问这个元素。