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

时间:2014-07-20 02:11:36

标签: c# wpf xaml

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

我在另一个网格中创建了一个网格元素,并在其中添加了一个文本块和一个Web浏览器。但是,我无法使用他们的名称(例如this.LastName)在MainPage.xaml.cs中访问它。在进一步调试时,我看到它们未在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>

1 个答案:

答案 0 :(得分:0)

我认为问题在于你的文本块在xaml代码中太深了。我认为数据绑定应该足够在这种情况下,但有一种方法可以解决这个问题。您必须将此元素订阅到某个事件(如已加载)并将发件人保存到后面的代码中的某些属性:

<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;
}

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