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

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

MVVM 事件處理程序從何而來(lái)

MVVM 事件處理程序從何而來(lái)

我正在編寫(xiě)一個(gè)簡(jiǎn)單的 MVVM 應(yīng)用程序來(lái)研究正確的代碼設(shè)計(jì)。完成它需要一段時(shí)間,但事情進(jìn)展順利。我有一個(gè)關(guān)于如何處理事件的問(wèn)題,以及代碼應(yīng)該放在 ViewModel 中還是代碼隱藏中。首先,綁定事件有兩種技術(shù),一種是使用 Blend Interactivity DLL 綁定到命令,另一種是使用MethodBindingExtension類(lèi)。使用交互 DLL,它允許使用 EventArgs 轉(zhuǎn)換器將事件參數(shù)轉(zhuǎn)換為只包含我們需要的數(shù)據(jù)的 UI 不可知類(lèi)型。我不認(rèn)為 MethodBindingExtension 這樣做,但它更靈活。但是,當(dāng)您需要設(shè)置事件 args 值時(shí),這個(gè)事件 args 轉(zhuǎn)換器將無(wú)濟(jì)于事?(或者它可能允許將值轉(zhuǎn)換回來(lái),還沒(méi)有檢查這些類(lèi))我喜歡使用 MethodBindingExtension,現(xiàn)在我的 ViewModel 中有這段代碼。我不喜歡它的是我使用了特定的 UI 類(lèi)型,這現(xiàn)在沒(méi)什么大不了的,但從理論上講,也許可以改進(jìn)。我該怎么辦?將其移動(dòng)到代碼隱藏中?將它留在 ViewModel 中并使用 args 轉(zhuǎn)換器?就這樣放著?public void Window_DropFile(DragEventArgs e) {    if (e.Data.GetDataPresent(DataFormats.FileDrop)) {        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);        foreach (string file in files) {            ReadScriptFile(file);        }    }}public void Window_PreviewDragOver(DragEventArgs e) {    e.Effects = DragDropEffects.All;    e.Handled = true;}public void Header_PreviewLeftMouseButtonDown(IScriptViewModel sender, MouseButtonEventArgs e) {    if (sender == SelectedItem && sender.CanEditHeader && !sender.IsEditingHeader) {        sender.IsEditingHeader = true;        e.Handled = true;    }}
查看完整描述

3 回答

?
不負(fù)相思意

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

一般來(lái)說(shuō):

你的 MVVM 視圖模型應(yīng)該只包含數(shù)據(jù)和命令。當(dāng)然有很多例外,但請(qǐng)記住,基本上,它應(yīng)該只包含與視圖相關(guān)的命令和項(xiàng)目。

當(dāng)事件處理程序應(yīng)用于組件時(shí),我看到了很多關(guān)于這一點(diǎn)的困惑。事情是; 當(dāng)您在視圖模型中放置 UI 組件的事件處理程序時(shí),視圖模型綁定到視圖的實(shí)際實(shí)現(xiàn)(使用 UI 組件),而不是一般的“視圖”。

根據(jù)經(jīng)驗(yàn); 你應(yīng)該能夠復(fù)制你的視圖模型并在另一個(gè)實(shí)現(xiàn)中使用它,它應(yīng)該只是編譯。它不應(yīng)該包含對(duì) UI 元素本身的引用,或者通過(guò)事件處理等間接引用。


所以,是的,您應(yīng)該將這些事件處理程序放在代碼隱藏中,或者找到一個(gè)能夠在視圖/XAML 中處理它的框架或組件。從這樣的事件中調(diào)用命令完全沒(méi)問(wèn)題。更實(shí)用的方法會(huì)說(shuō):將它們放在最有效的地方,并使您的代碼最易讀/易維護(hù)。如果你理解了 MVVM 的概念,你就會(huì)盡量減少這些混合模型的出現(xiàn),這通常就足夠了。


查看完整回答
反對(duì) 回復(fù) 2021-10-09
?
青春有我

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

我會(huì)用附加屬性做這樣的事情。例如,對(duì)于 FileDrop,我將實(shí)現(xiàn)一個(gè)附加屬性,如下所示:


public static class WindowExtensions

{

    public static readonly DependencyProperty ReadScriptFilesCommandProperty = DependencyProperty.RegisterAttached(

        "ReadScriptFilesCommand",

        typeof(ICommand),

        typeof(WindowExtensions),

        new PropertyMetadata(default(ICommand), OnReadScriptFilesCommandChanged));


    private static void OnReadScriptFilesCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

    {

        Window window = d as Window;

        if (window == null)

            return;


        if (e.NewValue is ICommand)

        {

            window.Drop += WindowOnDrop;

        }

        if (e.OldValue != null)

        {

            window.Drop -= WindowOnDrop;

        }

    }


    private static void WindowOnDrop(object sender, DragEventArgs e)

    {

        Window window = sender as Window;

        if (window == null)

            return;

        if (e.Data.GetDataPresent(DataFormats.FileDrop))

        {

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            ICommand readScriptFilesCommand = GetReadScriptFilesCommand(window);

            readScriptFilesCommand.Execute(files);

        }

    }


    public static void SetReadScriptFilesCommand(DependencyObject element, ICommand value)

    {

        element.SetValue(ReadScriptFilesCommandProperty, value);

    }


    public static ICommand GetReadScriptFilesCommand(DependencyObject element)

    {

        return (ICommand)element.GetValue(ReadScriptFilesCommandProperty);

    }

}

所以你可以在你Window的視圖中設(shè)置它并將它鏈接到ICommand你的視圖模型中。該命令采用string[]并執(zhí)行邏輯。


查看完整回答
反對(duì) 回復(fù) 2021-10-09
  • 3 回答
  • 0 關(guān)注
  • 167 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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