0

我有一个名为设置的对象。它有一个名为 BusyDoingAction 的 bool 类型的属性

public class Settings : ObservableObject
    {
        private bool busyDoingAction;
        public bool BusyDoingAction 
        {
            get { return this.busyDoingAction; }
            set
            {
                this.busyDoingAction= value;
                OnPropertyChanged(() => BusyDoingAction);
            }
        }

我有另一个名为 FileAdding 的类,

private readonly string[] files;
private bool busy;
public FileAdding(string[] files, bool busyAction)
        {
            this.files = files;
            this.busy= busyAction;
        }

现在,在我的 ViewModel 中,我有以下内容:

var fileAdding = new FileAdding(files, Settings.BusyDoingAction);            

            if (!fileAdding.Initialize())
                return;

            Task<bool>.Factory.StartNew(() =>
            {
                fileAdding.Execute(CToken);
                return fileAdding.Deinitialize();
            }, CToken);
        }

在 FileAdding Initialize() 函数中我有:

public bool Initialize()
        {
           busy  = true;
           return true;
        }

public bool Deinitialize()
         {
           busy  = false;
           return true;
         }

问题是:BusyDoingAction 在 FileAdding 中没有改变,并且我没有收到该属性的任何值更改通知..

该属性绑定到我的应用程序中的 mainGrid,我使用它来检测应用程序是否繁忙并根据 BusyDoingAction 的值启用/禁用网格

我不知道我做错了什么,但是我需要在那个busyDoingAction.Intialize/Deinitialize函数中设置BusyDoingAction的属性值..

任何帮助都非常感谢..

4

2 回答 2

1

FileAdding的班级应该喜欢这个......你应该有属性......并在UI上Settings绑定到它。Settings.BusyDoingAction

    private readonly string[] files;
    private Settings setting;
    public FileAdding(string[] files, Settings setting)
            {
                this.files = files;
                this.setting= setting;
            }

public Settings Settings
        {
            get { return this.setting; }
            set
            {
                this.setting= value;
                OnPropertyChanged(() => Settings);
            }
        }

public bool Initialize()
        {
           Settings.BusyDoingAction = true;
           return true;
        }

public bool Deinitialize()
         {
           Settings.BusyDoingAction = false;
           return true;
         }
于 2013-09-19T08:44:35.137 回答
1

正在做:

var fileAdding = new FileAdding(files, Settings.BusyDoingAction);

您只是将当前值或复制Settings.BusyDoingActionFileAction.busy.

Settings.BusyDoingAction和之间没有联系FileAdding.busy

要解决您的问题,一种方法是将Settings引用传递给FileAdding

private readonly string[] files;
private readonly Settings settings;
public FileAdding(string[] files, Settings settings)
{
    this.files = files;
    this.settings = settings;
}

然后在:

public bool Initialize()
{
   this.settings.BusyDoingAction = true;
   return true;
}

public bool Deinitialize()
{
   this.settings.BusyDoingAction = false;
   return true;
}
于 2013-09-19T08:46:29.953 回答