【问题标题】:How to save/load Properties from/to a View/Singleton-Class如何从视图/单例类保存/加载属性
【发布时间】:2016-12-21 18:52:33
【问题描述】:

大家好,

我的程序中出现了一个小问题,这让我非常烦恼。

这里是我在程序中使用的单例类。

public class Resources
{
    private static Resources _instance;

    public static Resources Instance
    {
        get { return _instance ?? (_instance = new Resources()); }
    }

    #region Properties

    public Candidate Candidate { get; set; }
    public Enterprise Enterprise { get; set; }
    public WikomContact WikomContact { get; set; }
    public AMSContact AMSContact { get; set; }

    #endregion
}

我使用这些 Candidate/Enterprise/...-Objects 作为我的 ComboBoxes 的 ItemSource,这非常有效。 为了在其他视图模型中使用这些对象,我想将它们保存到单例类中,从那里我想将它们加载到所有其他视图模型中。

public _0InfoTalkViewModel()
{
    _context = new WikomContext();
    RefreshViewCommand = new RelayCommand(RefreshView);
    PrintViewCommand = new RelayCommand(PrintView);
    SaveViewCommand = new RelayCommand(SaveView);
    EnterpriseIsEnabled = false;
    WikomContactIsEnabled = true;

    Candidate = Resources.Instance.Candidate;
    Enterprise = Resources.Instance.Enterprise;
    WikomContact = Resources.Instance.WikomContact;
}

例如,这是我在程序中使用的组合框之一。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Content="Enterprise" Margin="3" Foreground="White" Grid.Column="0"/>
    <ComboBox ItemsSource="{Binding EnterpriseList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                              SelectedItem="{Binding Enterprise, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                              DisplayMemberPath="Name" 
                              IsEditable="True"
                              IsEnabled="{Binding EnterpriseIsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                              Margin="3" Grid.Column="1">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding RefreshViewCommand, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>
</Grid>

SaveView 方法如下所示

public void SaveView()
{
    Resources.Instance.Candidate = Candidate;
    Resources.Instance.Enterprise = Enterprise;
    Resources.Instance.WikomContact = WikomContact;
}

RefreshView 方法也会在表单加载时触发。 问题是,组合框从数据库中获得了正确的输入,但是如果我选择其中一个,然后更改视图并再次加载它,则 SelectedValue 不是我之前输入的...

有谁知道我做错了什么?或者谁能​​告诉我为什么我的“想法/方法”不起作用?

抱歉英语不好,我通常说德语! ;)

【问题讨论】:

  • 我不明白:你从来没有使用过Resources 的单例实例,只是它的静态属性。为什么不直接将Resources 设为静态?
  • 哦,是的,我明白了……我对单例并不熟悉,这就是我在这里的原因;)我现在将其更改为单例的实例,但它仍然无法正常工作。 ..还有什么想法吗?
  • 你根本不需要public static Resource Instance。将 Resource 声明为静态类并像以前一样访问属性:例如,Resource.Candidate
  • 您的代码不会像编写的那样编译,因为Candidate 是一个类名,而不是Candidate 类型的值(Resources.Instance.Candidate = Candidate)。更新代码后,请编辑您的帖子。
  • 我的房产也被称为“候选人”! ;) 这很糟糕吗?这可能是我的错误吗?等待我尝试重命名它们! :o //edit: 试图重命名我的属性,但效果不佳 -> 这不是我的错误...还有什么想法吗? :(@Kjata30

标签: c# wpf view singleton viewmodel


【解决方案1】:

斯奈勒;

你的属性不应该是静态的,它们应该是实例属性:

public Candidate Candidate { get; set; }
public Enterprise Enterprise { get; set; }
public WikomContact WikomContact { get; set; }
public AMSContact AMSContact { get; set; }

当您引用单例时,Resources 应该是对实例的引用:

Candidate = Resources.Instance.Candidate;
Resources.Instance.Candidate = Candidate;

【讨论】:

  • 啊,很高兴知道,是的,我对单例不太熟悉...我现在将其更改为实例,但它仍然不起作用...还有什么想法吗? :o @Ibrahim Malluf
  • 您是否像我在代码中向您展示的那样将您的属性从静态更改为实例?
  • 抱歉回复晚了,没看到你的帖子,是的,我这样做了,还是不行……
  • 我在这里重读了您的帖子,如果我现在正确理解您的意思,您希望您的单例类能够从一个请求到另一个请求继续存在。这不会发生,因为没有保存状态。您需要一种在请求之间保持状态的方法。
  • 是的,我昨天已经解决了这个问题,我遇到了从我的视图模型中的单例类加载对象的事件。我已经将您的答案标记为“已接受”!谢谢您的帮助! :)
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
相关资源
最近更新 更多