C# 变量更改时文本框不更新

C# 变量更改时文本框不更新,c#,uwp,C#,Uwp,目前我的项目中有一个文本框字段存在问题 目前,我有一个NavigationViewItem,它是一个SaveButton\u ClickAsync。它打开“保存文件”对话框,允许用户输入文件名并将其保存为.txt或.csv文件 我有一个全局变量,用于存储保存为字符串的文档的名称和文件位置。然后在整个项目中引用此变量 一个例子是显示文件名和文件路径的文本框 当我从设置页面导航到第一次保存后的设置页面时,我得到了这个更新。它也会在保存文档时更新,然后再次保存,但在第一次保存时不会更新,除非导航到Se

目前我的项目中有一个
文本框
字段存在问题

目前,我有一个
NavigationViewItem
,它是一个
SaveButton\u ClickAsync
。它打开“保存文件”对话框,允许用户输入文件名并将其保存为
.txt
.csv
文件

我有一个全局变量,用于存储保存为字符串的文档的名称和文件位置。然后在整个项目中引用此变量

一个例子是显示
文件名和
文件路径的
文本框

当我从设置页面导航到第一次保存后的设置页面时,我得到了这个更新。它也会在保存文档时更新,然后再次保存,但在第一次保存时不会更新,除非导航到
SettingsPage
或再次保存

当我当前处于
设置页面
并首次保存文档时,它不会更新。我需要它来做这个

我相信这与下面的
设置页面
部分中使用
私有void GlobalVariables\u property changed
的方式有关。我已经包括了所有相关的代码,所以你可以看到它是如何工作的,以及程序所经历的步骤

我需要实现的是,当第一次保存文件时,必须触发
GlobalVariebles\u PropertyChanged
。我觉得我需要在程序加载时自动将字符串加载到_fileName/_filePath中,这将允许
PropertyChanged
将其确定为字符串更改。不过我可能是在抓救命稻草。对此有任何想法/想法都很好

GlobalVariableStorage
中的
Get Set
是一个占位符,允许我查看类何时检测到更改

代码如下:

代码隐藏

using BS.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace BS.Content_Pages

    public sealed partial class SettingsPage : Page, INotifyPropertyChanged
    {
        public SettingsPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            GlobalVariables.PropertyChanged += GlobalVariables_PropertyChanged;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            GlobalVariables.PropertyChanged -= GlobalVariables_PropertyChanged;
        }

        private void GlobalVariables_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case nameof(GlobalVariables.FilePath):
                    NotifyPropertyChanged(nameof(TextBoxFilePath));
                    break;
                case nameof(GlobalVariables.FileName):
                    NotifyPropertyChanged(nameof(TextBoxFileName));
                    break;
            }
        }

        public string TextBoxFilePath => GlobalVariables.FilePath;
        public string TextBoxFileName => GlobalVariables.FileName;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName) =>
            PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }
}
全球变量存储

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;


namespace BS.Data
{
    public static class GlobalVariables
        {
            private static string _filePath;
            public static string FilePath
            {
            get
            {
                if (_filePath is null)
                {
                    return "Document not saved yet";
                }
                else
                {
                    return _filePath; }
                }
                set { _filePath = value; NotifyPropertChanged(); }
            }

            private static string _fileName;
            public static string FileName
            {
            get
            {
                if (_fileName is null)
                {
                    return "Document not saved yet";
                }
                else
                {
                    return _fileName;                
                }
            }
                set { _fileName = value; NotifyPropertChanged(); }
            }

            public static event PropertyChangedEventHandler PropertyChanged;
            private static void NotifyPropertChanged([CallerMemberName]string propertyName = " ") =>
                PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));

    }
}
这是一个可怕的“黑客”解决方案,但我现在所做的是在设置页面的情况下,做一个框架。导航刷新页面。它起作用了。真恶心。但它是有效的。这仅在当前处于设置页面并保存文件时有效

设置页面(属性更改案例)


请删除不必要的代码。我相信这与保存文件的整个过程有关。主要关注的是CodeBehind和GlobalVariablesStorage,我相信在这里会发生错误。我为那些需要了解整个程序在保存文件时所做的工作的人提供了代码。希望这会更好。这里有一个问题,x:Bind有时会在使用静态字段时失败。您使用静态类来维护这些字段的原因是什么?可能您可以在setingpage.xaml.cs中设置这些字段,并将它们公开,以便从其他地方访问它们。顺便说一下,这里的问题是:当然,这是最好的方法,但是既然 > X:BION/COD>对静态字段有一些问题,请考虑使用另一种方法。顺便说一句,如果我提到的问题确实是导致绑定失败的原因,那么我认为你是幸运的,因为当我遇到这个问题时,我不得不浪费很多时间来遇到这个问题:)
private void GlobalVariables_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case nameof(GlobalVariables.FilePath):
                    NotifyPropertyChanged(nameof(TextBoxFilePath));
                    Frame.Navigate(this.GetType());
                    break;
                case nameof(GlobalVariables.FileName):
                    NotifyPropertyChanged(nameof(TextBoxFileName));
                    Frame.Navigate(this.GetType());
                    break;
            }
        }