简体   繁体   中英

Modify UI Foreground Color after Navigating to Another Page and Back

I want to modify the font color of the header of a panorama control:

    <phone:Panorama Name="MainPagePanorama"
                            Title="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
                            Background="{StaticResource QuotePaperBackground}"
                            SelectionChanged="MainPagePanorama_SelectionChanged">
                <phone:Panorama.Foreground>
                    <SolidColorBrush x:Name="TitleColor" Color="{Binding Red, Source={StaticResource WP8AccentColors}}"/>
                </phone:Panorama.Foreground>
           ...
    </phone:Panorama>

And my event handler:

        private void MainPagePanorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedItem = MainPagePanorama.SelectedItem as PanoramaItem;

            Deployment.Current.Dispatcher.BeginInvoke(async () =>
                {
                    await Task.Delay(500);
                    TitleColor.Color = (selectedItem.Foreground as SolidColorBrush).Color;
                });
        }

This works perfectly fine until I navigate to another page and navigate back. I used the debugger to see that TitleColor.Color is still being changed everytime I swipe the screen, but the UI is not updated somehow.

Any help is appreciated. Thanks!

-Dan

Try with this code it could help you:

 private async void MainPagePanorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                selectedItem = MainPagePanorama.SelectedItem as PanoramaItem;
                await Task.Run(() =>
                {
                   Thread.Sleep(100);
                   TitleColor.Color = (selectedItem.Foreground as SolidColorBrush).Color;
                });
            }

I think your code works (remarks below) but with the line:

TitleColor.Color = (selectedItem.Foreground as SolidColorBrush).Color;

you are changing the TitleColor to the same as it is - selectedItem.Foreground as SolidColorBrush is nothong more than <SolidColorBrush x:Name="TitleColor"... .

To check it just repleace your code to:

TitleColor.Color = Colors.Brown;

and see what happens.

Remarks:

  • when you set a color in your code, you destroy your binding defined in xaml
  • I assume that you delay your color change on purpose
  • look that your code is already running on the Main thread so there is no need to invoke it from Dispatcher - it will be the same. So you can run your code like this:


private async void MainPagePanorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    PanoramaItem selectedItem = MainPagePanorama.SelectedItem as PanoramaItem;

    await Task.Delay(500);
    TitleColor.Color = Colors.Brown;
}

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