无法访问Windows Phone 8.1中集线器内的元素

时间:2015-04-19 19:07:23

标签: c# xaml windows-phone-8.1

我一直在尝试访问我的应用中Hub内创建的一些元素,但IDE说当前上下文中不存在名称user_Imageuser_Image是我尝试在集线器内访问的元素。

我该怎么做?

XAML:

<Hub x:Name="centralHub" Header="bacpac" HorizontalAlignment="Left" Height="658" Margin="-7,27,-55,-18" VerticalAlignment="Top" Width="462">
            <Hub.Background>
                <ImageBrush Stretch="UniformToFill" ImageSource="ms-appx:///Images/Panorama.png"/>
            </Hub.Background>
            <HubSection x:Name="homeSection" Header="Home">
                <DataTemplate>
                    <Grid HorizontalAlignment="Left" Height="518" Margin="-9,-32,0,-3" VerticalAlignment="Top" Width="425">
                        <TextBlock x:Name="Hello_Label" HorizontalAlignment="Left" Height="109" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="218" FontSize="60" RenderTransformOrigin="-0.894,0.523" FontFamily="Segoe WP Light">
                        <Run Text="Hello"/>

                        </TextBlock>
                        <Image x:Name="user_Image" HorizontalAlignment="Left" Height="100" Margin="152,109,0,0" VerticalAlignment="Top" Width="100"/>
                        <TextBlock x:Name="user_Name" HorizontalAlignment="Left" Margin="10,223,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" FontSize="26" Width="370"/>

C#代码:

Task.Factory.StartNew(() =>
                {
                    var profilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture?type={1}&access_token={2}", fbSession.Token.FacebookId, "square", fbSession.Token.AccessToken);
                    user_Image.Source = new BitmapImage(new Uri(profilePictureUrl));
                    user_Name.Text = String.Format("{0} {1}", (string)result["first_name"], (string)result["last_name"]);
                }, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, UISyncContext);

我该如何解决这个问题?提前谢谢!

1 个答案:

答案 0 :(得分:2)

不幸的是,Microsoft决定您必须通过HubSection定义DataTemplate的内容,而不是直接将其设置为Content。这意味着您无法使用控件的名称直接访问它。

在此处查看更多内容:How to access any control inside Hubsection Datatemplate in Windows 8.1 store