【问题标题】:Variables which need to be live across the application in WPF需要在 WPF 中的应用程序中存在的变量
【发布时间】:2014-03-04 17:18:53
【问题描述】:

我正在开发一个 WPF 应用程序。在用户关闭该应用程序之前,我需要一些变量/信息不会被破坏。哪种方法最好? Static 带有 static 变量的类?此外,这种情况下的最佳做法是什么?

【问题讨论】:

  • 静态类中的静态变量
  • 可能是单例类?这可能是个坏主意,尤其是在您开发多线程应用程序时。

标签: c# wpf


【解决方案1】:

我相信你能做的就是编写一个类来为你保存变量,就像 ASP .net 中的会话对象一样。你可以做类似的事情

public static class ApplicationState
{
    private static Dictionary<string, object> _values =
               new Dictionary<string, object>();
    public static void SetValue(string key, object value)
    {
        if (_values.ContainsKey(key))
        {
            _values.Remove(key);
        }
        _values.Add(key, value);
    }
    public static T GetValue<T>(string key)
    {
        if (_values.ContainsKey(key))
        {
            return (T)_values[key];
        }
        else
        {
            return default(T);
        }
    }
}

保存变量:

ApplicationState.SetValue("MyVariableName", "Value");

读取变量:

MainText.Text = ApplicationState.GetValue<string>("MyVariableName");

这将是通过您的应用程序进行的所有访问,并将始终保留在内存中。

【讨论】:

    【解决方案2】:

    在这种情况下,您可以使用 static Class 和静态字段。他永远不会被释放,它没有任何析构函数,也不参与垃圾回收。

    如果你想让一个正常的类保持活力,你可以使用GC.KeepAlive()的方法:

    SampleClass sample = new SampleClass();
    
    //... Somewhere in the end ...
    
    GC.KeepAlive(sample);
    

    在这里,KeepAlive() 创建一个对您的类实例的引用,以便垃圾收集认为他仍在您的应用程序中使用。 KeepAlive() 的目的是确保存在对有可能被 GC 过早回收的对象的引用。

    引用MSDN:

    此方法引用 obj 参数,使该对象从例程开始到按执行顺序调用此方法的位置都没有资格进行垃圾收集。 Code this method at the end, not the beginning, of the range of instructions where obj must be available.

    KeepAlive 方法除了延长作为参数传入的对象的生命周期之外,不执行任何操作并且不会产生任何副作用。

    或者,信息可以存储在 WPF 应用程序设置中。特别是如果信息很重要,并且在系统故障或重新启动后不应丢失。

    设置大约位于此处Project -&gt; Properties -&gt; Parameters。设置新值的示例:

    MyProject.Properties.Settings.Default.MyButtonColor = "Red";
    

    保存如下:

    MyProject.Properties.Settings.Default.Save();
    

    也可以将Binding与属性一起使用,表示Source中设置的类:

    xmlns:properties="clr-namespace:MyProject.Properties"
    
    <TextBlock Text="{Binding Source={x:Static properties:Settings.Default},
                              Path=MyButtonColor,
                              Mode=TwoWay}" />
    

    有关在 WPF 中使用设置的更多信息,请参阅:

    User settings in WPF

    A configurable Window for WPF

    Saving user color settings of a clicked Button in WPF

    【讨论】:

      【解决方案3】:

      您也可以像这样创建静态类并在 xaml 中引用它:

      namespace MyNamespace
      {
          public static class Globals
          {
              public static double SomeVariable { get { return 1.0; } }
          }
      }
      

      然后像这样从 xaml 访问它:

      <UserControl Width="{x:Static globals:Globals.SomeVariable}" />
      

      globals 在您的 xaml 顶部定义,如下所示:

      <Window x:Class="MyNamespace.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:globals="clr-namespace:MyNamespace">
      </Window>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-04
        • 2014-01-25
        • 2011-02-15
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 2015-08-25
        • 1970-01-01
        相关资源
        最近更新 更多