【问题标题】:Method not passing result back to main program方法不将结果传递回主程序
【发布时间】:2020-06-18 04:03:04
【问题描述】:

很难理解我在哪里出错了。我已经在代码中标记了内容和位置。我正在使用 XAML 接口,并且这里的所有内容都有对象。代码可以编译,但 TextBlock 不会使用 updateVTCShortCode 的结果进行更新 感谢您的帮助!

主程序

namespace VTCPT
{
    /// <summary>
    /// 
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();


        }

public void shortFormCodec_SelectionChanged(object sender, RoutedEventArgs e)
        {
            //UPDATE THE SHORTCODE TEXTBLOCK
            updateVTCShortCode display = new updateVTCShortCode();
            display.mergeShortCode(longFormCodec.SelectedItem.ToString());
            if (String.IsNullOrEmpty(display.finalResult()))
            { shortFormCodec.Text = ".."; }
            else { shortFormCodec.Text = display.finalResult();
                shortFormCodec.Text = "test";
            }  /////THIS IS NOT ACTUALLY GETTING A RETURN 
        }

        public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


        private void updateShortForm(object sender, SelectionChangedEventArgs e)
        {

        }


        private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
        {


        }

        private void fsSiteBuild_SelectionChanged(object sender, RoutedEventArgs e)
        {
        }


        private void updateSiteBuild(object sender, TextChangedEventArgs e)
        {
            int index = fsRoomDesig.Text.IndexOf(".");

            if (index > 0)
            { fsSiteBuild.Text = fsRoomDesig.Text.Substring(0, index); }
            else { fsSiteBuild.Text = ".."; }
        }

        private void vtcSystemName_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }
    }
}

更新VTCShortCode CLASS

namespace VTCPT
{
    class updateVTCShortCode
    {
        String result = "";
        public void mergeShortCode(String longFormCodec)
    {      if (longFormCodec.Equals("Cisco SX80"))
            {
                String sendShortForm = "SX80";
                result = "V-T" + sendShortForm;

            }
            if (longFormCodec.Equals("Cisco Webex Codec Plus"))
            {
                String sendShortForm = "SRK";
                result = "V-T" + sendShortForm;
            }
            if (longFormCodec.Equals("Cisco Webex Codec Pro"))
            {
                String sendShortForm = "SRK";
                result = "V-T" + sendShortForm;
            }
 }
        public String finalResult()
                 { return result; } //////SHOULD BE GETTING SENT BACK TO MAIN PROGRAM
    }
}

【问题讨论】:

  • 断点,单步执行代码,途中检查变量,不要pass-go。那么你应该拥有描述问题所在的所有弹药。
  • @TheGeneral Breakpoints 向我显示了任何问题,并且我已经跟踪了变量...您有提示吗?哈哈。我很擅长这些

标签: c# visual-studio xaml methods


【解决方案1】:

我认为问题在于以下代码取自您的 shortFormCodec_SelectionChanged 方法。您设置shortFormCodec.Text = display.finalResult(); 紧跟shortFormCodec.Text = "test";。最终结果将永远不可见,因为它会立即被“test”覆盖。

if (String.IsNullOrEmpty(display.finalResult()))
{
    shortFormCodec.Text = "..";
}
else
{
    shortFormCodec.Text = display.finalResult();
    shortFormCodec.Text = "test";
}

正如 TheGeneral 在 cmets 中建议的那样,您应该能够使用断点并单步执行代码(使用 F8 键)来识别这一点,同时观察变量和文本字段的值。如果您将鼠标悬停在变量和任何shortFormCodec.Text 行的.Text 部分上,它将在程序中显示该点的值。

但是,如果您调整代码以使用if {} else if {} else {} 结构,我认为您可能会发现它会有所帮助。我还将finalResult() 方法更改为一个属性,因为它除了返回一个字符串之外什么都不做。例如:

class updateVTCShortCode
{
    // You could set the default value to an empty string I.e. = ""
    // but having it set to "Not set" may help you spot any problems for now.
    // As long as you remember to call mergeShortCode() first, you would never
    // see "Not set" returned anyway. But this would help you spot that mistake.
    public string FinalResult { get; set; } = "Not set";

    public void mergeShortCode(String longFormCodec)
    {
        if (longFormCodec.Equals("Cisco SX80"))
        {
            String sendShortForm = "SX80";
            FinalResult = "V-T" + sendShortForm;

        } 
        else if (longFormCodec.Equals("Cisco Webex Codec Plus"))
        {
            String sendShortForm = "SRK";
            FinalResult = "V-T" + sendShortForm;
        }
        else if (longFormCodec.Equals("Cisco Webex Codec Pro"))
        {
            String sendShortForm = "SRK";
            FinalResult = "V-T" + sendShortForm;
        } else
        {
            // If longFormCodec is not matched, set the result to ".."
            FinalResult = "..";
        }
    }

通过在 mergeShortCode() 方法的 else 块中将最终结果设置为“..”并为 FinalResult 属性设置默认值(即使它是空字符串,即“”)。您正在阻止 FinalResult 成为 null 并从一个函数中提供所有可能的结果。这意味着您可以将shortFormCodec_SelectionChanged() 方法简化为以下内容,并轻松地在其他地方重用mergeShortCode() 方法:

    public void shortFormCodec_SelectionChanged(object sender, RoutedEventArgs e)
    {
        //UPDATE THE SHORTCODE TEXTBLOCK
        updateVTCShortCode display = new updateVTCShortCode();
        display.mergeShortCode(longFormCodec.SelectedItem.ToString());
        shortFormCodec.Text = display.FinalResult;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2016-02-21
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多