如何实现界面

时间:2019-07-05 14:30:15

标签: c# wpf interface

我想更改我的代码,我也想使用接口,这样如果有人希望将来,那么可以很容易地实现它,而无需更改太多代码。

因此,我成功实现了我的界面IPalindromeChecker。但是问题出在MainWindow中。 所以我不确定,但是我会使用public void Output(string text)方法创建另一个接口。我尝试在IPalindromeChecker public void Output(string text)中添加一个方法,但是没有用。

 interface ICheckPalindrome
    {
        bool IsPalindrome(string text);
    }
public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (a != b)
                {
                    return false;
                }

                min++;
                max--;
            }
        }
    }
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            lblInput.Foreground = Brushes.ForestGreen;
            lblResult.Foreground = Brushes.ForestGreen;
            lblTitel.Foreground = Brushes.ForestGreen;

        }

        /// <summary>
        /// User input and checking the input if the word a palindrome is.
        /// </summary>
        private void InputText_TextChanged(object sender, TextChangedEventArgs e)
        {
            string text = InputText.Text;

            bool isPalindrome = TextChecker.PalindromeChecker(text); // HERE IS THE PROBLEM

            OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");

            if (InputText.Text == string.Empty)
            {
                OutputText.Clear();
            }
        }
 public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (a != b)
                {
                    return false;
                }

                min++;
                max--;
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

目前还不清楚TextChecker.PalindromeChecker是什么,但是如果您希望能够在无需修改ICheckPalindrome的情况下切换MainWindow接口的实现,则应在窗口中插入的实现ICheckPalindrome,然后在界面上编写您的代码。

例如,您可以使用属性来执行此操作:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        lblInput.Foreground = Brushes.ForestGreen;
        lblResult.Foreground = Brushes.ForestGreen;
        lblTitel.Foreground = Brushes.ForestGreen;

        //use the PalindromeChecker as the default implementation
        PalindromeChecker = new PalindromeChecker();

    }

    public ICheckPalindrome PalindromeChecker { get; set; } //<--

    private void InputText_TextChanged(object sender, TextChangedEventArgs e)
    {
        string text = InputText.Text;

        bool isPalindrome = PalindromeChecker.IsPalindrome(text);

        OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");

        if (InputText.Text == string.Empty)
        {
            OutputText.Clear();
        }
    }
}

那么切换到另一种实现就像将PalindromeChecker的{​​{1}}属性设置为实现相同的MainWindow接口的另一个类的实例一样简单。