简体   繁体   中英

c# windows phone 8.1 get button content as a string

I'm new to c# and wp development.

On "Holding" I want the Content value of the button as a string in my c# code.

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

I thought it was pretty straightforward and did this but doesn't work. TextToSpeech is a method that takes a string and reads it out. It reads out "Windows.UI.Xaml.Controls.Grid"... why?

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

Try this :-

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

What are you doing wrong is by using this. Here this does not belong to button but for the page itself. In place of this you have to use the corresponding button object that i have already shown how to use. For further reference see This Keyword

you can also use name of the button to get its content that is little bit hard coded.

这很简单:)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM