第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

屬性初始化后,有沒有辦法在 Xamarin ViewModel 中設(shè)置臟標(biāo)志?

屬性初始化后,有沒有辦法在 Xamarin ViewModel 中設(shè)置臟標(biāo)志?

C#
飲歌長(zhǎng)嘯 2023-08-20 15:26:34
我想為視圖模型中的任何必需屬性設(shè)置臟標(biāo)志。我IsDirty在構(gòu)造函數(shù)中初始化為 false。不幸的是,我的屬性中的所有設(shè)置器都是在構(gòu)造函數(shù)之后調(diào)用的。有沒有辦法IsDirty在所有設(shè)置器之后將其設(shè)置為 false?二傳手都有一條線IsDirty=true;我將 Prism 框架與 Xamarin 4.0 一起使用,但 Prism 文檔沒有有關(guān) ViewModel 生命周期的任何內(nèi)容。我的編輯構(gòu)造函數(shù)如下所示: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:構(gòu)造函數(shù)調(diào)用InitializeLookupValues(). 這些似乎是罪魁禍?zhǔn)?。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.        }退出構(gòu)造函數(shù)后,每個(gè)屬性都會(huì)被設(shè)置。他們看起來像這個(gè)。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貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

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


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


正如評(píng)論中所建議的,在設(shè)置器中添加斷點(diǎn)并查看堆棧跟蹤是查找設(shè)置來源的一個(gè)很好的起點(diǎn)......如果我不得不猜測(cè),我會(huì)懷疑一些與導(dǎo)航相關(guān)的回調(diào)。


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


經(jīng)過多次編輯后,我的編輯:


您應(yīng)該修改架構(gòu)以考慮視圖模型的異步初始化。僅僅并行運(yùn)行所有事情并希望得到最好的結(jié)果很少會(huì)奏效。


例如,您可以將屬性設(shè)置為只讀,直到初始化完成,然后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而不是簡(jiǎn)單的bool... 但您明白了。


查看完整回答
反對(duì) 回復(fù) 2023-08-20
?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

由于這些SetProperty方法是可重寫的,因此您可以注入一些自定義邏輯。當(dāng)您需要驗(yàn)證對(duì)象是否已被更改時(shí),這可能非常有用。


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;

}

請(qǐng)記住,當(dāng)您初始化字段時(shí),此IsDirty值為 true,因此在綁定之前,您需要調(diào)用該Reset方法將其設(shè)置IsDirty回 false,這樣您就可以可靠地知道字段何時(shí)已更改。


請(qǐng)注意,如何處理這個(gè)問題在某種程度上取決于您。例如,您可以使用 Linq 執(zhí)行此操作...


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

您還可以強(qiáng)制執(zhí)行如下模式:


public class FooDTO : StatefulObject

{

    public FooDTO(string prop1, string prop2)

    {

        // Set the properties...

        Prop1 = prop1;


        // Ensure IsDirty is false;

        Reset(); 

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-08-20
  • 2 回答
  • 0 關(guān)注
  • 160 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)