亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

屬性初始化后,有沒有辦法在 Xamarin ViewModel 中設置臟標志?

屬性初始化后,有沒有辦法在 Xamarin ViewModel 中設置臟標志?

C#
飲歌長嘯 2023-08-20 15:26:34
我想為視圖模型中的任何必需屬性設置臟標志。我IsDirty在構造函數中初始化為 false。不幸的是,我的屬性中的所有設置器都是在構造函數之后調用的。有沒有辦法IsDirty在所有設置器之后將其設置為 false?二傳手都有一條線IsDirty=true;我將 Prism 框架與 Xamarin 4.0 一起使用,但 Prism 文檔沒有有關 ViewModel 生命周期的任何內容。我的編輯構造函數如下所示:public SomeDetailsViewModel(INavigationService navigationService) : base(navigationService){    Sample = new SampleDTO();    InitializeLookupValues();    _samplesService = new SampleService(BaseUrl);    TextChangedCommand = new Command(() => OnTextChanged());    AddSampleCommand = new Command(() => AddCurrentSample());    CancelCommand = new Command(() => Cancel());    IsDirty = false;}編輯3:構造函數調用InitializeLookupValues(). 這些似乎是罪魁禍首。private async Task InitializeLookupValues()        {            App app = Prism.PrismApplicationBase.Current as App;            string baseUrl = app.Properties["ApiBaseAddress"] as string;            _lookupService = new LookupDataService(baseUrl);            int TbId = app.CurrentProtocol.TbId;            int accessionId = CollectionModel.Instance.Accession.AccessionId;            Parts = await _lookupService.GetParts(accessionId);//HACK            Containers = await _lookupService.GetSampleContainers(TbId);            Additives = await _lookupService.GetAdditives(TbId);            UnitsOfMeasure = await _lookupService.GetUnitsOfMeasure();                        // with a few more awaits not included.        }退出構造函數后,每個屬性都會被設置。他們看起來像這個。public ObservableCollection<PartDTO> Parts{    get    {        return parts;    }    set    {        SetProperty(ref parts, value);    }}private PartDTO part;public PartDTO SelectedPart{    get    {        return part;    }    set    {        SetProperty(ref part, value);                IsDirty = true;    }}其中 IsDirty 定義如下:private bool isDirty;public bool IsDirty{    get    {        return isDirty;    }    set    {        SetProperty(ref isDirty, value);        Sample.DirtyFlag = value;    }}
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

有沒有辦法IsDirty在所有設置器之后將其設置為 false?


setter 不是自己調用的,必須有人調用他們。您應該確定是誰在這樣做,并且要么阻止他在沒有充分理由的情況下設置內容(首選),要么讓他在完成后重置臟標志。


正如評論中所建議的,在設置器中添加斷點并查看堆棧跟蹤是查找設置來源的一個很好的起點......如果我不得不猜測,我會懷疑一些與導航相關的回調。


但是您應該嘗試確保視圖模型在構造函數之后初始化,這IsDirty實際上意味著“已通過視圖更改”而不是“可能由用戶更改,也可能只是延遲初始化的一部分”。


經過多次編輯后,我的編輯:


您應該修改架構以考慮視圖模型的異步初始化。僅僅并行運行所有事情并希望得到最好的結果很少會奏效。


例如,您可以將屬性設置為只讀,直到初始化完成,然后IsDirty在.falseInitializeLookupValues


偽代碼:


Constructor()

{

    Task.Run( async () => await InitializeAsync() );

}


string Property

{

    get => _backingField;

    set

    {

        if (_isInitialized && SetProperty( ref _backingField, value ))

            _isDirty = true;

    }

}


private async Task InitializeAsync()

{

    await SomeAsynchronousStuff();

    _isInitialized = true;

}


private bool _isInitialized;

private bool _isDirty;

也許,您想將_isInitialized其作為屬性公開給視圖以顯示一些沙漏,并使用 aManualResetEvent而不是簡單的bool... 但您明白了。


查看完整回答
反對 回復 2023-08-20
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

由于這些SetProperty方法是可重寫的,因此您可以注入一些自定義邏輯。當您需要驗證對象是否已被更改時,這可能非常有用。


public class StatefulObject : Prism.Mvvm.BindableBase

{

    private bool _isDirty;

    public bool IsDirty

    {

        get => _isDirty;

        private set => SetProperty(ref _isDirty, value);

    }


    protected override bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)

    {

        var isDirty = base.SetProperty(ref storage, value, onChanged, propertyName);

        if(isDirty && propertyName != nameof(isDirty))

        {

            IsDirty = true;

        }


        return isDirty;

    }


    public void Reset() => IsDirty = false;

}

請記住,當您初始化字段時,此IsDirty值為 true,因此在綁定之前,您需要調用該Reset方法將其設置IsDirty回 false,這樣您就可以可靠地知道字段何時已更改。


請注意,如何處理這個問題在某種程度上取決于您。例如,您可以使用 Linq 執行此操作...


var fooDTOs = someService.GetDTOs().Select(x => { x.Reset(); return x; });

您還可以強制執行如下模式:


public class FooDTO : StatefulObject

{

    public FooDTO(string prop1, string prop2)

    {

        // Set the properties...

        Prop1 = prop1;


        // Ensure IsDirty is false;

        Reset(); 

    }

}


查看完整回答
反對 回復 2023-08-20
  • 2 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號