变量更改时刷新文本框

时间:2018-09-07 11:06:04

标签: c# uwp

当前,为了更新我的文本框,我需要离开我的SettingsPage,然后回到其中以查看TextBoxes中的更改。

当全局变量更改时,您是否可以帮助更新这些TextBoxes?我已经研究过使用INotifyPropertyChanged。我只是不确定如何最好地实施它

这是我当前拥有的代码。它非常基础。

设置页面XAML

<Frame Background="{StaticResource CustomAcrylicDarkBackground}">
   <StackPanel>
       <TextBox Width="500" Header="File Name" IsReadOnly="True" Foreground="White" Text="{x:Bind TextBoxFileName}"/>
       <TextBox Width="500" Header="File Location" IsReadOnly="True" Foreground="White" Text="{x:Bind TextBoxFilePath}"/>
   </StackPanel>
</Frame>

隐藏代码

using static BS.Data.GlobalVariableStorage;

namespace BS.Content_Pages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class SettingsPage : Page
    {
        public SettingsPage()
        {
            this.InitializeComponent();
        }
            public string TextBoxFilePath = GlobalVariables.FilePath;
            public string TextBoxFileName = GlobalVariables.FileName;

        }


    }

}        

GlobalVariablesStorage类

namespace BS.Data
{
    class GlobalVariableStorage
    {
        public static class GlobalVariables
        {
            public static string FilePath { get; set; }
            public static string FileName { get; set; }
        }

    }
}

MainPage.XAML.cs中的保存文件功能(将保存名称解析为GlobalVariableStorage)

public async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
   {
       SaveFileClass instance = new SaveFileClass();
       IStorageFile file = await instance.SaveFileAsync();
       if (file != null)
         {
           GlobalVariables.FileName = file.Name;
            GlobalVariables.FilePath = file.Path;

                // Debugging the output file paths
                // Remember to REMOVE
                Debug.WriteLine(GlobalVariables.FileName);
                Debug.WriteLine(GlobalVariables.FilePath);

                WriteFile.WriteFileData();
        }            
   }

1 个答案:

答案 0 :(得分:1)

主要问题在于,您需要以某种方式告诉视图何时刷新数据绑定值。为了使您能够执行此操作,您需要知道何时发生。

换句话说,只要将任何属性设置为新值,GlobalVariables类都应引发一个事件。例如,它可以引发内置的PropertyChanged事件:

public static class GlobalVariables
{
    private static string _filePath;
    public static string FilePath
    {
        get { return _filePath; }
        set { _filePath = value; NotifyPropertyChanged(); }
    }

    private static string _fileName;
    public static string FileName
    {
        get { return _fileName; }
        set { _fileName = value; NotifyPropertyChanged(); }
    }

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

在您的视图中,您可以随后订阅此事件并引发该视图处理的另一个事件。您通过实现INotifyPropertyChanged接口告诉视图更新数据绑定值,并引发PropertyChanged事件以更新要更新的属性。像这样:

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));
}

还请注意,x:Bind的默认模式是OneTime,因此您应该在视图中将Mode设置为OneWay,例如:

Text="{x:Bind TextBoxFilePath, Mode=OneWay}"