首页 > 解决方案 > 方法不将结果传递回主程序

问题描述

在理解我在这里出错的地方时遇到了一些真正的麻烦。我已经在代码中标记了内容和位置。我正在使用 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
    }
}

标签: c#visual-studioxamlmethods

解决方案


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

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

正如 TheGeneral 在评论中建议的那样,您应该能够在查看变量和文本字段的值时使用断点和单步执行代码(使用 F8 键)来识别这一点。如果您将鼠标悬停在变量和.Text任何shortFormCodec.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 = "..";
        }
    }

通过在方法的 else 块中将最终结果设置为“..”,mergeShortCode()并为 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;
    }

}

推荐阅读