【问题标题】:Changing a color of a icon from an action on an other page从其他页面上的操作更改图标的颜色
【发布时间】:2019-10-10 19:17:37
【问题描述】:

我是 C# 初学者,我正在 Visual Studio 上编写 UWP 应用程序,主要用于 Windows 10 平台。

这是我的问题: 我在主页上有一个导航视图,其中列出了一些主题。 单击其中一个会带您进入一个页面 (Page1),其中我有单选按钮,它会更改此 Page1 标题的颜色。

我想将此颜色应用于主页上的主题

不知道怎么办...

有什么建议吗?

谢谢!

【问题讨论】:

    标签: c# visual-studio xaml uwp


    【解决方案1】:

    您可以通过创建MainPage 的静态实例并更改颜色来访问MainPage.xaml.cs 中的NavigationView

    解决办法如下:

    MainPage.xaml

    <NavigationView x:Name="nvSample"
                    x:FieldModifier="Public">
        ...
    </NavigationView>
    

    MainPage.xaml.cs

    public static MainPage Current;
    public MainPage()
    {
        this.InitializeComponent();
        Current = this;
    }
    

    Page1.xaml.cs

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        var nav = MainPage.Current.nvSample;
        nav.Foreground = new SolidColorBrush(Colors.Yellow);
    }
    

    最好的问候。

    【讨论】:

      【解决方案2】:

      非常感谢理查德,我做到了:)

      Page1.xaml.cs 代码改变了我页面中的所有文本前景,所以我使用了这个:

          private void RadioButton_Checked(object sender, RoutedEventArgs e)            
          {
              RadioButton radio = (RadioButton)sender;
              {}
      
              if (radio != null)
              {
      
                  String selected = radio.Tag.ToString();
                  switch (selected)
                  {
      
      
                      default:
                      case "Red":
                          Title.Foreground = new SolidColorBrush(Colors.Red);
                          MainPage.Current.Subject1.Foreground = new SolidColorBrush(Colors.Red);
                          break;`
      

      再次感谢您。

      现在我的下一个任务是在应用程序关闭后保存这些颜色,以便在应用程序启动时它们保持不变。如果您有任何建议... :)

      【讨论】:

      • 您好,如果我的回答对您有帮助,请标记我的回答。如果还有其他问题,可以开新帖,这样可以帮助更多有同样问题的人。谢谢
      猜你喜欢
      • 2020-10-19
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 2013-04-08
      • 2020-03-31
      • 1970-01-01
      • 2017-01-06
      • 2014-12-24
      相关资源
      最近更新 更多