c #windows phone 8.1获取按钮内容为字符串

时间:2014-07-03 12:40:34

标签: c# xaml button windows-phone-8.1

我是c#和wp开发的新手。

On" Holding"我想将按钮的Content值作为c#代码中的字符串。

<Grid>
    <Pivot>
        <PivotItem>
            <Grid>
                <Button x:Name="Venue1" Content="Venue1" HorizontalAlignment="Left" Margin="30,0,0,0"VerticalAlignment="Top" Width="342" Height="102" Grid.Row="1" Holding="Holding"/>
            </Grid>
        </PivotItem>
    </Pivot>
</Grid>

我认为这很简单并且这样做但是没有用。 TextToSpeech是一种接收字符串并将其读出的方法。它读出&#34; Windows.UI.Xaml.Controls.Grid&#34; ...为什么?

private async void Holding(object sender, HoldingRoutedEventArgs) 
{
    await TextToSpeech(this.Content.ToString());
}

2 个答案:

答案 0 :(得分:1)

试试这个: -

private async void Holding(object sender, HoldingRoutedEventArgs) 
{
    var obj = sender as Button;
    var content = obj.Content;
    await TextToSpeech(content);
}

你做错了是用这个。这里这个不属于按钮,而是属于页面本身。代替 this ,您必须使用我已经展示过如何使用的相应按钮对象。 如需进一步参考,请参阅This Keyword

您还可以使用按钮的名称来获取稍微硬编码的内容。

答案 1 :(得分:0)

这很简单:)

await TextToSpeech(this.Venue1.Content.ToString());