C# 当没有来自web服务的数据时,如何在组合框中添加消息? 使用系统; 使用System.Collections.Generic; 使用System.Collections.ObjectModel; 使用System.Linq; Net系统; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Animation; 使用System.Windows.Shapes; 使用Microsoft.Phone.Controls; 库兰宁德克斯 { 公共部分类Kategori:PhoneApplicationPage { 列表kat; 公共Kategori() { 初始化组件(); kat=新列表(); //使用WEB服务中的数据// 尝试 { var ws=new ServiceReference1.Service1SoapClient(); ws.getIndexSumCompleted+=新事件处理程序(ws_getIndexSumCompleted); ws.getIndexSumAsync(); } 捕获(例外情况除外) { Show(“Err:+ex.ToString()); } 最后 { } } void ws_GetIndexSumCompleted(对象发送方,ServiceReference1.GetIndexSumCompletedEventArgs) { var结果=”; 尝试 { result=e.result.ToString(); if(result.ToString().Length>1) { MessageBox.Show(“数据加载…”); 字符串[]行1=result.ToString().Split(“|”); 字符串[]lsMob; 对于(int i=0;i1) { MessageBox.Show(“数据加载…”); 字符串[]行1=result.Split(“|”); 字符串[]lsMob; for(int i=0;i

C# 当没有来自web服务的数据时,如何在组合框中添加消息? 使用系统; 使用System.Collections.Generic; 使用System.Collections.ObjectModel; 使用System.Linq; Net系统; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Animation; 使用System.Windows.Shapes; 使用Microsoft.Phone.Controls; 库兰宁德克斯 { 公共部分类Kategori:PhoneApplicationPage { 列表kat; 公共Kategori() { 初始化组件(); kat=新列表(); //使用WEB服务中的数据// 尝试 { var ws=new ServiceReference1.Service1SoapClient(); ws.getIndexSumCompleted+=新事件处理程序(ws_getIndexSumCompleted); ws.getIndexSumAsync(); } 捕获(例外情况除外) { Show(“Err:+ex.ToString()); } 最后 { } } void ws_GetIndexSumCompleted(对象发送方,ServiceReference1.GetIndexSumCompletedEventArgs) { var结果=”; 尝试 { result=e.result.ToString(); if(result.ToString().Length>1) { MessageBox.Show(“数据加载…”); 字符串[]行1=result.ToString().Split(“|”); 字符串[]lsMob; 对于(int i=0;i1) { MessageBox.Show(“数据加载…”); 字符串[]行1=result.Split(“|”); 字符串[]lsMob; for(int i=0;i,c#,web-services,combobox,windows-phone,C#,Web Services,Combobox,Windows Phone,我不太确定我的答案,我只是根据我对你问题的理解来回答的 不需要从已经是String类型的对象调用.ToString()(例如result是String,所以result.Length就足够了,而不是result.ToString().Length) 不要过度使用try/catch/finally,尤其是在早期开发阶段。这将对开发人员自己隐藏异常 您可以尝试以下方法: acBox.Items.Add("There is no data"); acBox.DataBind(); void ws_G

我不太确定我的答案,我只是根据我对你问题的理解来回答的

  • 不需要从已经是String类型的对象调用
    .ToString()
    (例如
    result
    是String,所以
    result.Length
    就足够了,而不是
    result.ToString().Length
  • 不要过度使用
    try/catch/finally
    ,尤其是在早期开发阶段。这将对开发人员自己隐藏异常
  • 您可以尝试以下方法:

    acBox.Items.Add("There is no data");
    acBox.DataBind();
    
    void ws_GetIndexSumCompleted(对象发送方,ServiceReference1.GetIndexSumCompletedEventArgs)
    {
    var结果=”;
    result=e.result.ToString();
    如果(result.Length>1)
    {
    MessageBox.Show(“数据加载…”);
    字符串[]行1=result.Split(“|”);
    字符串[]lsMob;
    for(int i=0;i
    您没有以任何方式处理错误,因此请删除那些try/catch/finally语句,以停止接受异常。不清楚您在问什么。您只是想显示MessageBox还是向ComboBox添加一些内容?我对您的代码也有一些建议,但这超出了这个问题的范围。“在这里我想显示MESSAGEBOX”在AutoboBox中“没有数据”。所以你想在combobox或MESSAGEBOX中显示“没有数据”?我的回答是假设你想在combobox中显示消息。pertanyaannya agak ambigu bro:p
    acBox.Items.Add("There is no data");
    acBox.DataBind();
    
    void ws_GetIndeksUmumCompleted(object sender, ServiceReference1.GetIndeksUmumCompletedEventArgs e)
    {
        var result = "";
        result = e.Result.ToString();
        if (result.Length > 1)
        {
            MessageBox.Show("Data Loaded...");
            string[] rows1 = result.Split('|');
            string[] lsMob;
            for (int i = 0; i < rows1.Length; i++)
            {
                lsMob = rows1[i].Split('#');
                kat.Add(lsMob[1]);
            }
        }
        else
        {
            //if no data returned from web service, 
            //add string "THERE IS NO DATA" to list
            kat.Add("THERE IS NO DATA");
        }
        this.acBox.ItemsSource = kat;
    }