简体   繁体   中英

UWP/C# Applying selected Colour to NavigationView

Finally managed to get the Colorpickers down and sorted and they currently work on a OnNavigateTo basis.

When I pick a color from color picker I would like to to be applied eith instantly to the Foreground of my NavigationViewItems OR apply once i click the button within the settings page called TextColourApply_Click.

The mentioned color picker is on the settingspage currently and the NavigationViewItems are on the MainPage .

I was looking at doing a UI refresh but this doesnt work with UWP as far as i can tell. As a work around, i was looking at doing a current Frame navigate but this doesnt work

I have the following that allows the selected colour to apply when navigating back to the "MainPage":

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    SolidColorBrush DefaultTextColour = Application.Current.Resources["DefaultTextColour"] as SolidColorBrush;

    if (ColourSelections.TextColour != null)
    {
        DefaultTextColour = ColourSelections.TextColour;
    }

    foreach (var item in NavView.MenuItems.OfType<NavigationViewItem>())
    {
        item.Foreground = DefaultTextColour;
    }       
}          

Any ideas on how to impliment this would be appreciated. Thank you

if your desired behaviour is following :

When I pick a color from color picker it should be applied instantly to the Foreground of my NavigationViewItems and color picker is on settings page.

In that case you do not need OnNavigatedTo on your MainPage and you do not need the Apply as well, So remove your OnNavigatedTo method also remove the Apply button from your settings page and then Just do the following:

Create a public static property within your ShellPage (the page where your NavigationView exists) that will expose your NavigationView, and make sure to initialize it within the constructor of your ShellPage.

public static NavigationView MyNavView;
public ShellPage()
{
    this.InitializeComponent();
    MyNavView = NavView; //here you assign your navigation view to the public static property so you can access it outside this shell page as well.
}

Now within the colorChanged event (in your settings page) of your color picker assign the color to the foreground of your navigationmenuItems.

private void TextColourPicker_ColorChanged(ColorPicker sender, ColorChangedEventArgs args)
{
    SolidColorBrush DefaultTextColour = new SolidColorBrush(TextColourPicker.Color);

    foreach (var item in ShellPage.MyNavView.MenuItems.OfType<NavigationViewItem>())
    {
        item.Foreground = DefaultTextColour;
    } 
}

and just to make sure that whenever your app is loaded for the first time you get the default color set in your resources, assign a Loaded event to your NavigationView and set the default color in there.

add the loaded event in xaml like this :

<NavigationView x:Name="NavView" Loaded="NavView_Loaded">

and the event in your backend will be :

private void NavView_Loaded(object sender, object args)
{
    SolidColorBrush DefaultTextColour = Application.Current.Resources["DefaultTextColour"] as SolidColorBrush;    

    foreach (var item in NavView.MenuItems.OfType<NavigationViewItem>())
    {
        item.Foreground = DefaultTextColour;
    } 
}

Please note that now you do not even need the public static class you were using before for saving the colors, so you can remove that class as well.

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