简体   繁体   English

UWP / C#将选定的颜色应用于NavigationView

[英]UWP/C# Applying selected Colour to NavigationView

Finally managed to get the Colorpickers down and sorted and they currently work on a OnNavigateTo basis. 最终设法使选色器下降并进行了排序,它们当前在OnNavigateTo的基础上工作。

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. 当我从颜色选择器中选择一种颜色时,我想立即应用到我的NavigationViewItems的前景中,或者单击一次设置页面中名为TextColourApply_Click的按钮后,将其应用。

The mentioned color picker is on the settingspage currently and the NavigationViewItems are on the MainPage . 提到的颜色选择器当前在settingspage上 ,NavigationViewItems在MainPage上

I was looking at doing a UI refresh but this doesnt work with UWP as far as i can tell. 我正在查看UI刷新,但据我所知,这不适用于UWP。 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": 我具有以下允许选择的颜色导航回“ 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. 当我从颜色选择器中选择一种颜色时,应立即将其应用于我的NavigationViewItems的前景,并且颜色选择器位于设置页面上。

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: 在这种情况下你不需要的OnNavigatedTo您和的MainPage你不需要的应用一样,所以删除您的OnNavigatedTo方法也从设置页面中删除应用按钮,然后只要做到以下几点:

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. 在ShellPage(NavigationView所在的页面)内创建一个公共静态属性,该属性将公开NavigationView,并确保在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. 现在,在颜色选择器的colorChanged事件中(在您的设置页面中),将颜色分配给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. 并确保每次首次加载应用程序时,都会在资源中获得默认颜色设置,请将Loaded事件分配给NavigationView并在其中设置默认颜色。

add the loaded event in xaml like this : 像这样在xaml中添加已加载的事件:

<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. 请注意,现在您甚至不需要以前使用的公共静态类来保存颜色,因此您也可以删除该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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