简体   繁体   English

WPF组合框无响应

[英]WPF Combo box Unresponsive

I am calling data from a WCF service and populating a combo box in my WPF application. 我正在从WCF服务调用数据,并在WPF应用程序中填充一个组合框。 As soon as I navigate to the location of the Combo box the page freezes or sometimes I get to open the combo box but then it also freezes? 一旦导航到“组合框”的位置,页面就会冻结,或者有时我可以打开组合框,但是它也冻结了?

WPF WPF

 private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            using (TruckServiceClient TSC = new TruckServiceClient())
            {
                await LoadPhase(TSC, cmbtest);

            }

    private async Task LoadPhase (TruckServiceClient TSC, ComboBox combobox)
    {
        List<Phasetems> phasetems = new List<Phasetems>();
        foreach (var item in await TSC.GetphaseAsync())
            phasetems.Add(new Phasetems { Phase = item.Phase });
        combobox.ItemsSource = (phasetems.ToArray());
        combobox.IsEnabled = true;
        combobox.SelectedIndex = 1;
    }

    public class Phasetems
    {
        public int Id { get; set; }

        public string Phase { get; set; }

        public override string ToString()
        {
            return Phase;
        }
    }

WCF 世界足球联合会

public List<RPhases> Getphase()
        {
            List<RPhases> phases = new List<RPhases>();
            List<RPhases> dbphase;

            using (TruckDb db = new TruckDb())
                dbphase = db.RPhases.Where(x => x.Id != null).ToList();
            foreach (var a in dbphase)
            {
                var items = new RPhases
                {
                   Id = a.Id,
                   Phase = a.Phase 
                };
                phases.Add(items);
            }
            return phases;
        }

The code you posted is correct and doesn´t create that ui blocking behaviour (as far as I can see). 您发布的代码是正确的,并且不会创建该ui阻止行为(据我所知)。

Make sure nothing else is blocking the ui thread. 确保没有其他东西阻塞ui线程。 Try it without the service call to see whether it still blocks it. 在没有服务调用的情况下尝试它,以查看它是否仍然阻止它。

You can use Visual Studio break feature to step into running code and find out where it is. 您可以使用Visual Studio中断功能进入运行代码并找出其位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM