2013-06-05 15 views
0

我已閱讀所有帖子,他們可能是我的問題的答案,但我不太瞭解這個概念,所以我問我的問題,希望有一個簡單的答案。我想從文本塊中檢索文本並將其提供給我的文本到語音代碼。如何從代碼中檢索XAML TextBlock中的數據?

需要幫助的代碼是這樣的:

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

這些來自數據綁定模型的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> 
+0

你有什麼問題? –

回答

0

不知道我是否真正瞭解這個問題是正確的,但如果它只是關於如何從這些規範中獲取文本IC的TextBlocks,爲什麼不簡單地設置x:Name,然後訪問相應的成員中的代碼:

<Grid ...> 
    <TextBlock x:Name="textBlock1" Text="{Binding Login}" ..../> 
</Grid> 
<Grid ...> 
    <TextBlock x:Name="textBlock2" Text="{Binding Password}" .../> 
</Grid> 

在代碼:

var firstText = textBlock1.Text; 
var secondText = textBlock2.Text; 

但是,因爲這兩個的TextBlocks的Text屬性綁定到一些性能LoginPassword,爲什麼不直接從這些文本中獲取?

+0

完全正確。感謝您的支持。 –