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

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

使用 MVVM 模式關(guān)閉打開的窗口,產(chǎn)生 System.NullReferenceException

使用 MVVM 模式關(guān)閉打開的窗口,產(chǎn)生 System.NullReferenceException

C#
動(dòng)漫人物 2023-05-13 16:03:49
我正在嘗試使用 WPF C# 學(xué)習(xí) MVVM 模式。在將信息保存到 sqlite 數(shù)據(jù)庫后嘗試關(guān)閉打開的窗口時(shí),我遇到了錯(cuò)誤。當(dāng)發(fā)出保存新聯(lián)系人的命令時(shí),我在 HasAddedContact(this, new EventArgs()); 上收到錯(cuò)誤消息錯(cuò)誤:System.NullReferenceException:“對(duì)象引用未設(shè)置為對(duì)象的實(shí)例?!蔽业囊晥D模型:public class NewContactViewModel : BaseViewModel    {        private ContactViewModel _contact;        public ContactViewModel Contact        {            get { return _contact; }            set { SetValue(ref _contact, value); }        }        public SaveNewContactCommand SaveNewContactCommand { get; set; }        public event EventHandler HasAddedContact;        public NewContactViewModel()        {            SaveNewContactCommand = new SaveNewContactCommand(this);            _contact = new ContactViewModel();        }        public void SaveNewContact()        {            var newContact = new Contact()            {                Name = Contact.Name,                Email = Contact.Email,                Phone = Contact.Phone            };            DatabaseConnection.Insert(newContact);            HasAddedContact(this, new EventArgs());        }    }保存新聯(lián)系人命令:    public class SaveNewContactCommand : ICommand    {        public NewContactViewModel VM { get; set; }        public SaveNewContactCommand(NewContactViewModel vm)        {            VM = vm;        }        public event EventHandler CanExecuteChanged;        public bool CanExecute(object parameter)        {            return true;        }        public void Execute(object parameter)        {            VM.SaveNewContact();        }    }NewContactWindow.Xaml.Cs 背后的代碼:public partial class NewContactWindow : Window    {        NewContactViewModel _viewModel;        public NewContactWindow()        {            InitializeComponent();            _viewModel = new NewContactViewModel();            DataContext = _viewModel;            _viewModel.HasAddedContact += Vm_ContactAdded;        }        private void Vm_ContactAdded(object sender, EventArgs e)        {            this.Close();        }    }
查看完整描述

1 回答

?
人到中年有點(diǎn)甜

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

您正在構(gòu)造函數(shù)中創(chuàng)建 NewContactWindow 的視圖模型,正確地將其分配給 DataContext,并正確地向該事件添加處理程序。不幸的是,您還在資源中創(chuàng)建了相同視圖模型的第二個(gè)實(shí)例,并且您手動(dòng)設(shè)置所有綁定的 Source 屬性以使用資源中沒有事件處理程序的那個(gè)。


Window.DataContext您在構(gòu)造函數(shù)中設(shè)置的 是 Window XAML 中任何綁定的默認(rèn)源。讓它做它的事情。Mode=TwoWay我還刪除了Bindings to 中的所有冗余內(nèi)容TextBox.Text,因?yàn)樵搶傩砸讯x為默認(rèn)情況下所有綁定都是 TwoWay。我認(rèn)為也沒有UpdateSourceTrigger=PropertyChanged做任何必要或有幫助的事情:這會(huì)導(dǎo)致 Binding 在每次按下鍵時(shí)更新您的 viewmodel 屬性,而不是僅在 TextBox 失去焦點(diǎn)時(shí)更新。但是我認(rèn)為您沒有對(duì)重要的屬性做任何事情;沒有驗(yàn)證或任何東西。但是 TextBox.Text是為數(shù)不多的實(shí)際使用它的地方之一,所以我把它留在了里面。


您應(yīng)該在其他窗口中刪除類似的視圖模型資源。它沒有任何傷害,但充其量也沒有用。在最壞的情況下,這是一個(gè)有吸引力的麻煩。用火把它燒死,把骨灰埋在半夜寂寞的十字路口下。


<Window x:Class="Contacts_App.View.NewContactWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:Contacts_App.View"

        xmlns:vm="clr-namespace:Contacts_App.ViewModel"

        mc:Ignorable="d"

        Title="New Contact Window" Height="250" Width="350">


    <Grid>

        <StackPanel 

            Margin="10">

            <Label Content="Name" />

            <TextBox 

                Text="{Binding Contact.Name, UpdateSourceTrigger=PropertyChanged}"

                Margin="0,0,0,5"/>

            <Label Content="Email" />

            <TextBox 

                Text="{Binding Contact.Email, UpdateSourceTrigger=PropertyChanged}"

                Margin="0,0,0,5"/>

            <Label Content="Phone Number" />

            <TextBox 

                Text="{Binding Contact.Phone, UpdateSourceTrigger=PropertyChanged}"

                Margin="0,0,0,5"/>

            <Button 

                Content="Save"

                Command="{Binding SaveNewContactCommand}"/>

        </StackPanel>

    </Grid>

</Window>


查看完整回答
反對(duì) 回復(fù) 2023-05-13
  • 1 回答
  • 0 關(guān)注
  • 231 瀏覽

添加回答

舉報(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)