如何在代码中从XAML TextBlock检索数据?

如何在代码中从XAML TextBlock检索数据?,xaml,Xaml,我已经阅读了所有的帖子,它们可能是我问题的答案,但我不太理解这个概念,所以我问我的问题是希望有一个简单的答案。我想从文本块中检索文本,并将其提供给我的文本到语音代码 <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <

我已经阅读了所有的帖子,它们可能是我问题的答案,但我不太理解这个概念,所以我问我的问题是希望有一个简单的答案。我想从文本块中检索文本,并将其提供给我的文本到语音代码

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>
需要帮助的代码如下:

if (DataContext == null)
{
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
   `enter code here`         {
                int index = int.Parse(selectedIndex);
                DataContext = App.ViewModel.Items[index];
                string saythis = "*here is where my question comes in - how do I get the string from the TextBlock in ControlPanel?*"
                Speaker(saythis);
                saythis = "here is where my question comes in - how do I get the string from the TextBlock in ControlPanel2?"
                Speaker(saythis);
            }
        }
    }
    async void Speaker(string words)
    {
        SpeechSynthesizer synth = new SpeechSynthesizer();
        await synth.SpeakTextAsync(words);
    }
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>
来自数据绑定模型的XAML是:

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>

我不确定我是否真正理解了这个问题,但如果只是关于如何从这些特定的文本块中获取文本,为什么不简单地设置
x:Name
,然后访问代码中的相应成员:

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>
<Grid ...>
    <TextBlock x:Name="textBlock1" Text="{Binding Login}" ..../>
</Grid>
<Grid ...>
    <TextBlock x:Name="textBlock2" Text="{Binding Password}" .../>
</Grid>

但是,由于这两个文本块的
Text
属性绑定到一些属性
Login
Password
,为什么不直接从这些属性获取文本?

您的文本有什么问题?
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Gr8Oz software" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="{Binding Heading}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel contains details text. Place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Text="{Binding Login}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>

    </Grid>
    <Grid x:Name="ContentPanel2" Margin="12,123,12,10" Grid.RowSpan="2">

        <TextBlock Text="{Binding Password}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,10,12,0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
    </Grid>