Silverlight和Csharp关于图像的困境.if语句和这个有关系吗

本文关键字:语句 if 困境 有关系 Csharp 于图像 图像 Silverlight | 更新日期: 2023-09-27 18:15:01

我有一个这样的查询,它给我一个错误,它说imgSortUp不存在于当前上下文中。这是臭虫吗?什么好主意吗?在我把图像声明放入if语句之前,它工作得很好。如有任何帮助,不胜感激。

ContentPresenter cp = (sender as Grid).GetVisualDescendants().OfType<ContentPresenter>().SingleOrDefault();
        if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status")
        {
            Image imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
            Image imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
        }
        else
            return;
        if (clearOldSortIcons != null)
        {
            Image oldSortIconUp = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
            Image oldSortIconDown = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
            oldSortIconUp.Visibility = System.Windows.Visibility.Collapsed;
            oldSortIconDown.Visibility = System.Windows.Visibility.Collapsed;
        }
    start:
        if (!string.IsNullOrEmpty(cp.Content.ToString()) && (string.IsNullOrEmpty(sortCheck) || sortCheck == cp.Content.ToString()))
        {
            if (hasSorted == false)
            {
                switch (cp.Content.ToString())
                {
                    case "Work Order #":
                        imgSortUp.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "Status":
                        imgSortUp.Visibility = System.Windows.Visibility.Visible;
                        break;
                }
                hasSorted = true;
            }
            else
            {
                switch (cp.Content.ToString())
                {
                    case "Title":
                        imgSortDown.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "Status":
                        imgSortDown.Visibility = System.Windows.Visibility.Visible;
                        break;
                }
                hasSorted = false;
            }
        }
        else
        {
            sortCheck = cp.Content.ToString();
            hasSorted = false;
            goto start;
        }
        sortCheck = cp.Content.ToString();
        clearOldSortIcons = sender;
        SortItems(cp.Content.ToString(), hasSorted);
        dpVideos.Source = grdVideos.ItemsSource;

Silverlight和Csharp关于图像的困境.if语句和这个有关系吗

这是因为您在自己的作用域中声明了图像,并在以后使用它们:

    // declare this image within the outer scope
    Image imgSortDown = null;
    if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status") 
    { 
        // declare image within the inner scope
        Image imgSortUp;
        imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault(); 
        imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault(); 
    } 
    // do more work
    imgSortUp.Visibility = true; // This wont work since the image is out of scope.
    imgSortDown.Visibility = true; // This will work since the images is still in scope.

noooooooo,而不是goto

是的,当你声明一个作用域{}时,通常在其中声明的任何变量都不能从外部访问。

为了解决这个问题,你可以简单地在if语句外声明Image imgSortUp;,然后在if语句内赋值。只是要注意,imgSortUp将等于null,直到您赋值。在初始化之前调用它将导致运行时错误。

   Image imgSortUp;
   Image imgSortDown;
   if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status")
    {
        imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
        imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
    }
    else
        return;