簡體   English   中英

WPF和C#:類和接口問題

[英]WPF and C#: Trouble with Classes and Interfaces

我正在開展銀行賬戶計划。 除了主窗口類之外,我還在使用基類,接口和兩個派生類。 我正在制作一個WPF應用程序,到目前為止,我能夠在該應用程序中使用類對象的ArrayList填充ListBox就好了。 但是,當我去修改ArrayList中的任何對象時,我很難正確地重新填充ListBox。 任何幫助將不勝感激!

這是我的MainWindow代碼:

 public partial class MainWindow : Window
 {
    ArrayList bankAccountList = new ArrayList();

    BankAccount savingsAccount = new SavingsAccount("New", "Account", "newaccount");
    BankAccount checkingAccount = new CheckingAccount("New", "Account", "newaccount");
    IAccount iAccount;
    string typeOfAccount = "";

    public MainWindow()
    {
        InitializeComponent();
    }

    //When the user pushes the "Add A Saving Account" button, a new savings account is added to the ArrayList and displayed in the app.
    private void btnAddAccount_Click(object sender, RoutedEventArgs e)
    {
        iAccount = (IAccount)savingsAccount;

        savingsAccount.Deposit(0.00m);

        bankAccountList.Add(savingsAccount);
        lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
        typeOfAccount = "savings";
    }

    //When the user pushes the "Add A Checking Account" button, a new checking account is added to the ArrayList and displayed in the app.
    private void btnAddCheckingAccount_Click(object sender, RoutedEventArgs e)
    {
        iAccount = (IAccount)checkingAccount;

        checkingAccount.Deposit(0.00m);

        bankAccountList.Add(checkingAccount);
        lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
        typeOfAccount = "checking";
    }

    //When the user pushes the "Delete Account" button, the account is removed, and this change is shown in the app.
    private void btnDeleteAccount_Click(object sender, RoutedEventArgs e)
    {
        lbxExistingAccounts.Items.RemoveAt(lbxExistingAccounts.Items.IndexOf(lbxExistingAccounts.SelectedItem));
    }

    //The user can push the "Submit Changes" button to submit his or her changes to the number and name of the account.
    private void btnSubmitChanges_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            for (int index = 0; index < bankAccountList.Count; index++)
            {
                if (index == lbxExistingAccounts.SelectedIndex)
                {
                    if (typeOfAccount == "savings")
                    {
                        savingsAccount.AccountNumber = tbxAccountNumber.Text;
                        savingsAccount.AccountOwnerFirstName = tbxFirstName.Text;
                        savingsAccount.AccountOwnerLastName = tbxLastName.Text;
                    }

                    else if (typeOfAccount == "checking")
                    {
                        checkingAccount.AccountNumber = tbxAccountNumber.Text;
                        checkingAccount.AccountOwnerFirstName = tbxFirstName.Text;
                        checkingAccount.AccountOwnerLastName = tbxLastName.Text;
                    }
                }
            }
            lbxExistingAccounts.Items.Clear();
            foreach (object accountObject in bankAccountList)
            {
                lbxExistingAccounts.Items.Add(accountObject);
            }
        }
        catch (FormatException)
        {
            MessageBox.Show("You may enter changes as letters, numbers, or both.");
        }

    }

這是我的接口代碼:

interface IAccount
{
    void SetAccountBalance(decimal accountBalance);
    string AccountInformation();
}

這是我的基類代碼:

    abstract class BankAccount
{
    public string AccountNumber { get; set; }
    public string AccountOwnerFirstName { get; set; }
    public string AccountOwnerLastName { get; set; }
    public decimal AccountBalance { get; set; }
    public decimal AnnualInteresetRate { get; set; }
    public string TypeOfAccount { get; set; }

    public BankAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
    {
        AccountOwnerFirstName = accountOwnerFirstName;
        AccountOwnerLastName = accountOwnerLastName;
        AccountNumber = accountNumber;
    }

    public abstract void Deposit(decimal amount);

    public abstract void Withdraw(decimal amount);

    public decimal CalculateInterest()
    {
        return (this.AccountBalance * this.AnnualInteresetRate) / 100;
    }
} 

這是我的派生類之一。 我兩個都差不多。

    class SavingsAccount : BankAccount, IAccount
{
    public SavingsAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
        : base(accountOwnerFirstName, accountOwnerLastName, accountNumber)
    {
        AnnualInteresetRate = 0.95m;
    }

    public override void Deposit(decimal amount)
    {
        AccountBalance = AccountBalance + amount;
    }

    public override void Withdraw(decimal amount)
    {
        AccountBalance = AccountBalance - amount;
    }

    public void SetAccountBalance(decimal accountBalance)
    {
        AccountBalance = accountBalance;
    }

    public string AccountInformation()
    {
        return "Savings Account \n  " + AccountOwnerFirstName + " " + AccountOwnerLastName + ", Account#: " + AccountNumber + ", Balance: $" + AccountBalance;
    }
}

看起來你剛剛開始,這很好,因為你想要重做一些事情。

  1. 您不需要將派生對象轉換為其基類型。 這種類型的鑄造被稱為“向上鑄造”並且無需任何鑄造即可自動工作。

  2. 您發布的代碼高度“WinForms-ish”,這在WPF中不是一個好方法。 首先使您的帳戶列表成為ObservableCollection並將ListBox的ItemsSource綁定到它。

該屬性看起來像:

public ObservableCollection<BankAccount> Accounts {get; set;}

實際上應該省略INotifyPropertyChanged以簡化和綁定:

<ListBox "ItemsSource={Binding Accounts}"/>

當然,這應該放在視圖模型類中,但您可以通過設置以下代碼開始:

DataContext = this;

一旦工作,所有文本框都應綁定到數據對象或視圖本身的屬性。

這是一個使用MVVM和WPF( MSDN )的好教程。 相信我,使用這種設計模式將使您使用WPF的工作變得更加容易,更具可擴展性,並且您會發現WPF基本上是為使用它而設計的。 使用WinForms方法只會讓你感到痛苦。

如果我能進一步協助或澄清任何事情,請告訴我!

我建議您為集合使用ObservableCollection ,這樣對項目所做的任何更改都將在視覺上反映出來,而無需任何額外的工作。 它不需要任何重大改變。 您要做的就是切換ArrayList以獲取ObservableCollection

http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

我還強烈建議為您的對象實現INotiftyPropertyChanged ,以便在更改帳戶信息時通知相應的訂閱者。

http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.inotifypropertychanged

我在WPF應用程序中廣泛使用這兩種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM