在加载控件之前初始化UWP视图,因为另一个视图线程没有完全关闭

时间:2019-01-15 14:46:45

标签: c# mvvm uwp uwp-xaml

更新

ShowDetailPage末端的枢轴的IsLocked属性由于某种原因而干扰了控件的加载。 我有2个父选项卡,可以为每个选项卡创建一个子选项卡,并且在创建子选项卡时将其锁定。子选项卡添加在父选项卡索引旁边。

我有一个UWP应用程序,它具有枢轴控制菜单系统。我们有一个顶层和一个第二层。在第二级中,某些屏幕将通过按钮命令创建一个视图,以进入添加/编辑视图,该视图被锁定,直到单击关闭的按钮后,该视图才返回到其父菜单屏幕。

由于某种原因,当我关闭添加/编辑视图时,某些线程没有立即关闭,因此需要花费一些时间来关闭一些线程。尝试进入另一个父选项卡的“添加/编辑”视图时,这会产生问题。基本上,视图是在控件加载之前初始化的。

ShowDetailPage方法是我在视图的按钮命令中用来打开添加/编辑视图的方法。 CloseDetailPage方法是我们离开屏幕的方式。该指示器告诉我这是一个线程问题,页面转移错误仅在进入屏幕之前将控件属性“初始化”设置为true时才会发生。 (正确传输时,该属性为false)。另外,如果我观看诊断输出,如果我等待一小段时间,则在关闭视图后,我将看到一些线程关闭,然后初始化的属性始终为false,并且我可以正确地移动。

这是我在创建错误并解决该错误时注意到的模式。 https://imgur.com/a/osgRUCx 我关闭添加/编辑屏幕后不久,这些主题就会关闭。只有关闭这些窗口,我才能正确转移到另一个添加/编辑视图。

我该怎么做才能找出导致该问题的线程?(我可以看到内存位置,但在线程关闭时看不到线程)。

如果我可以找到线程,该如何做才能等待线程关闭,然后再将屏幕转移到其中?

NavigationUtility,用于处理新屏幕的显示和关闭。

 internal static void ShowDetailPage(long customerId, BaseControl control, string parentPageTitle, string detailPageTitle)
    {
        try
        {
            Pivot p = MainPage.GetPivot();
            Utility u = new Utility();
            for (int i = 1; i < p.Items.Count; i++)
            {
                long customerIdFromPivot = (((p.Items[i] as PivotItem).Content) as Menus).ParentCustomer.CustomerId;
                if (CustomerIdFromPivot == CustomerId)
                {
                    var topMenuItem = (((p.Items[i] as PivotItem).Content) as Menus).GetPivot();
                    if (TopLevelPivots.ContainsKey(CustomerId))
                    {
                        TopLevelPivots[CustomerId] = topMenuItem;
                    }
                    else
                    {
                        TopLevelPivots.Add(CustomerId, topMenuItem);
                    }
                    TopLevelPivots[CustomerId].IsLocked = true;
                    SearchInVisualTree(topMenuItem, CustomerId, parentPageTitle);
                    var submenus = SecondLevelPivots[CustomerId].ItemsSource;
                    ObservableCollection<MenuItem> addedMenus = new ObservableCollection<MenuItem>();
                    int index = 0;
                    foreach (var subMenuItem in submenus as IEnumerable<MenuItem>)
                    {
                        addedMenus.Add(subMenuItem);
                        if (subMenuItem.HeaderTitle == parentPageTitle)
                        {
                            if (DetailScreenParentIndex.ContainsKey(CustomerId))
                                DetailScreenParentIndex.Remove(CustomerId);
                            DetailScreenParentIndex.Add(CustomerId, index);
                            addedMenus.Add(new MenuItem() { HeaderTitle = detailPageTitle, Content = u.GetInitializedControl(control, control.ParentCustomer, addedMenus.Count), ParentCustomer = control.ParentCustomer });
                        }
                        index++;
                    }

                    SecondLevelPivots[CustomerId].ItemsSource = addedMenus;
                    //if (DetailScreenParentIndex.ContainsKey(CustomerId))
                    SecondLevelPivots[CustomerId].SelectedIndex = DetailScreenParentIndex[CustomerId] + 1;
                    //else
                    //    SecondLevelPivots[CustomerId].SelectedIndex = 0;
                    SecondLevelPivots[CustomerId].IsLocked = true;
                }
            }
        }
        catch (Exception e)
        {

        }

    }

 internal static void CloseDetailPage(long CustomerId, string currentPage, string gotoPage)
    {
        try
        {
            Pivot p = MainPage.GetPivot();
            Utility u = new Utility();
            for (int i = 1; i < p.Items.Count; i++)
            {
                long CustomerIdFromPivot = (((p.Items[i] as PivotItem).Content) as Menus).ParentCustomer.CustomerId;
                if (CustomerIdFromPivot == CustomerId)
                {
                    var topMenuItem = (((p.Items[i] as PivotItem).Content) as Menus).GetPivot();
                    if (TopLevelPivots.ContainsKey(CustomerId))
                    {
                        TopLevelPivots[CustomerId] = topMenuItem;
                    }
                    else
                    {
                        TopLevelPivots.Add(CustomerId, topMenuItem);
                    }
                    TopLevelPivots[CustomerId].IsLocked = false;
                    SearchInVisualTree(topMenuItem, CustomerId, gotoPage);
                    var submenus = SecondLevelPivots[CustomerId].ItemsSource;
                    ObservableCollection<MenuItem> addedMenus = new ObservableCollection<MenuItem>();
                    foreach (var submenuItem in submenus as IEnumerable<MenuItem>)
                    {
                        if (submenuItem.HeaderTitle != currentPage)
                        {
                            addedMenus.Add(submenuItem);
                        }
                    }
                    SecondLevelPivots[CustomerId].ItemsSource = addedMenus;
                    SecondLevelPivots[CustomerId].SelectedIndex = DetailScreenParentIndex[claimantId];
                    SecondLevelPivots[CustomerId].IsLocked = false;
                    SecondLevelPivots.Remove(CustomerId);
                    TopLevelPivots.Remove(CustomerId);
                    DetailScreenParentIndex.Remove(CustomerId);
                }
            }
        }
        catch (Exception ex)
        {
            Utility.LogError(ex);
        }
    }

ViewModel (单击按钮的命令)转到新屏幕。

private void AddCustomerCommandExecute(object obj)
    {
        bool bEditing = false;
        AddEditCustomer control = new AddEditCustomer();
        control.ParentCustomer = ParentCustomer;
        CustomerDetails customerDetail = obj as CustomerDetails ;
        if (customerDetail == null)
        {
            customerDetail = new CustomerDetails() { };
        }
        customerDetail.Customer = ParentCustomer;
        control.CustomerDetailsForAddEdit = customerDetail;
        customerDetail.IsEdit = bEditing;
        control.PropertyChanged += CustomersForAddEdit_PropertyChanged;
        Utility u = new Utility();
        NavigationUtility.ShowDetailPage(ParentCustomer.customerId, u.GetInitializedControl(control, ParentCustomer, 0), "Customers", "Add Customer");

    }

查看背后的代码以获取添加/修改的屏幕

public sealed partial class AddEditCustomer : BaseControl
{
    public AddEditCustomerVM Model { get; set; }
    public CustomerDetails CustomerDetailsForAddEdit
    {
        get { return (CustomerDetails )GetValue(CustomerDetailsForAddEditProperty); }
        set
        {
            SetValue(CustomerDetailsForAddEditProperty, value);
            OnPropertyChanged("CustomerDetailsForAddEdit");
        }
    }

    public static readonly DependencyProperty CustomerDetailsForAddEditProperty =
      DependencyProperty.Register("CustomerDetailsForAddEdit", typeof(CustomerDetails), typeof(AddEditCustomer), null);

    public AddEditCustomer()
    {
        this.InitializeComponent();
        var resource = Resources.FirstOrDefault(aa => aa.Key.ToString() == "AddEditCustomerVM");
        if (resource.Value != null)
        {
            Binding b = new Binding();
            b.Mode = BindingMode.TwoWay;
            b.Path = new PropertyPath("CustomerDetailsForAddEdit");
            b.Source = (resource.Value as AddEditCustomerVM);
            Model = (resource.Value as AddEditCustomerVM);
            this.SetBinding(CustomerDetailsForAddEditProperty, b);
            (resource.Value as AddEditCustomerVM).PropertyChanged += AddEditView_PropertyChanged;
            Binding bindVal = new Binding();
            bindVal.Mode = BindingMode.TwoWay;
            bindVal.Path = new PropertyPath("ParentCustomer");
            bindVal.Source = (resource.Value as AddEditCustomerVM);
            this.SetBinding(ParentCustomerProperty, bindVal);
        }
    }
    private void AddEditView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        OnPropertyChanged(e.PropertyName);
    }

    private void dpStartDate_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
    {
        if (dpEndDate.Date.HasValue)
        {
            if (args.NewDate.Value > dpEndDate.Date.Value)
                dpEndDate.Date = null;
        }

    }
}

0 个答案:

没有答案